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);
    }));

}
相关推荐
uppp»19 分钟前
深入理解 Java 反射机制:获取类信息与动态操作
java·开发语言
玩电脑的辣条哥2 小时前
Python如何播放本地音乐并在web页面播放
开发语言·前端·python
m0_748256144 小时前
SpringBoot
java·spring boot·后端
阿华的代码王国4 小时前
【从0做项目】Java搜索引擎(3)
java·搜索引擎·项目
Mr.朱鹏4 小时前
针对Feign客户端请求体参数处理问题
java·jvm·spring boot·spring·spring cloud·maven·intellij-idea
ll7788115 小时前
LeetCode每日精进:20.有效的括号
c语言·开发语言·算法·leetcode·职场和发展
涛粒子6 小时前
Spring Bean 生命周期的执行流程
java·后端·spring
刘_sy6 小时前
使用EasyExcel和多线程实现高效数据导出
java·excel·easyexcel·批量导出excel
梦幻通灵6 小时前
IDEA通过Contince接入Deepseek
java·ide·intellij-idea
钝挫力PROGRAMER7 小时前
SpringBoot中Mybatis记录执行sql日志
spring boot·sql·mybatis