mybatis批量提交工具类

文章目录

1.获取Bean的工具类

java 复制代码
/**
 * @author Linging
 * @version 1.0.0
 * @since 1.0
 */
@Component
public class SpringUtils implements ApplicationContextAware {

   private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringUtils.applicationContext = applicationContext;
    }

    public static Object getBean(String name){
        return applicationContext.getBean(name);
    }

    public static <T> T getBean(Class<T> tClass){
        return applicationContext.getBean(tClass);
    }
}

2.批量提交工具类

java 复制代码
/**
 * @author Linging
 * @version 1.0.0
 * @since 1.0
 */
public class BatchUtils {

    public final static Integer COUNT = 100;

    public static <T,E> void execute(List<T> list, Class<E> tClass, BiConsumer<T, E> biConsumer){
        SqlSessionTemplate sqlSessionTemplate = SpringUtils.getBean(SqlSessionTemplate.class);
        SqlSession sqlSession = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH);
        E mapper = sqlSession.getMapper(tClass);
        try{
            int n = 0;
            for (T data : list) {
                n++;
                biConsumer.accept(data, mapper);
                if(n >= COUNT){
                    sqlSession.commit();
                    n = 0;
                }
            }
            sqlSession.commit();
            sqlSessionTemplate.clearCache();
        }catch (Exception e){
            sqlSession.rollback();
            throw new RuntimeException("批量执行出错", e);
        }finally {
            sqlSession.close();
        }
    }

    public static <T,E> void executeList(List<T> list, Class<E> tClass, BiConsumer<List<T>, E> biConsumer){
        SqlSessionTemplate sqlSessionTemplate = SpringUtils.getBean(SqlSessionTemplate.class);
        SqlSession sqlSession = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH);
        E mapper = sqlSession.getMapper(tClass);
        try{
            // 使用Google的guaua工具类:Lists
            List<List<T>> allList = Lists.partition(list, COUNT);
            for (List<T> subList : allList) {
                biConsumer.accept(subList, mapper);
                sqlSession.commit();
            }
            sqlSessionTemplate.clearCache();
        }catch (Exception e){
            sqlSession.rollback();
            throw new RuntimeException("批量执行出错", e);
        }finally {
            sqlSession.close();
        }
    }
    
}

3.使用

java 复制代码
@Transactional
public void add() {

    List<Student> students = new ArrayList<>();
    Student st1 = new Student();
    st1.setId(5L);
    st1.setName("赵云");
    st1.setAge(20);
    st1.setAddress("");
    st1.setPhone("");
    Student st2 = new Student();
    st2.setId(6L);
    st2.setName("极效满");
    st2.setAge(30);
    st2.setAddress("");
    st2.setPhone("");
    Student st3 = new Student();
    st3.setId(7L);
    st3.setName("黄棕");
    st3.setAge(99);
    st3.setAddress("");
    st3.setPhone("");
    students.add(st1);
    students.add(st2);
    students.add(st3);

    BatchUtils.execute(students, StudentMapper.class, ((student, studentMapper2) -> {
        studentMapper2.insert(student);
    }));

}
相关推荐
better_liang14 分钟前
每日Java面试场景题知识点之-如何设计分布式锁
java·redis·zookeeper·面试·分布式锁
战族狼魂14 分钟前
集 “自动飞行、智能识别、实时预警、勤务联动” 于一体的高速公路应急车道无人机检测系统方案
java·人工智能·大模型·无人机
一只鹿鹿鹿22 分钟前
信息化项目管理规范(参考Word文件)
java·大数据·运维·开发语言·数据库
Java小白笔记23 分钟前
Linux 手动部署 Oracle JDK 17 完全指南
java·linux·oracle
夕除24 分钟前
实战--2
java·spring boot·spring
XGeFei26 分钟前
python中子线程与主线程的关系
开发语言·python
Chase_______30 分钟前
【Java杂项】final 关键字详解:变量、方法、类限制与引用可变性
java·开发语言·python
ruxingli39 分钟前
Golang iota详解
开发语言·后端·golang
我材不敲代码40 分钟前
Python venv 虚拟环境从入门到精通 + uv 高性能替代工具实战指南
开发语言·python·uv
l1t1 小时前
DeepSeek总结的使用实体-组件-系统和基于存在性处理进行Python编程18-20
开发语言·python