mybatis-plus 根据指定字段 批量 删除/修改

mybatis-plus 提供了根据id批量更新和修改的方法,这个大家都不陌生 但是当表没有id的时候怎么办

  • [方案一: 手写SQL](#方案一: 手写SQL)
  • [方案二: 手动获取SqlSessionTemplate 就是把mybatis plus 干的事自己干了](#方案二: 手动获取SqlSessionTemplate 就是把mybatis plus 干的事自己干了)
  • [方案三 : 重写 executeBatch 方法](#方案三 : 重写 executeBatch 方法)
  • 结论:

mybatis-plus 提供了根据id批量更新和修改的方法,这个大家都不陌生 但是当表没有id的时候怎么办)

方案一: 手写SQL

  • 这个就不说了,就是因为不想手写SQL 所以才有这篇博客

方案二: 手动获取SqlSessionTemplate 就是把mybatis plus 干的事自己干了

java 复制代码
// 这种方法就是把mybatis的活在干一遍,还是一条一条处理的.只是共用一个session连接
@Autowired
private SqlSessionTemplate sqlSessionTemplate;

// 新获取一个模式为BATCH,自动提交为false的session
SqlSession session = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH,false);
static final BATCH_SIZE = 1000;
// XxxMapper 为 对应的mapper文件
XxxMapper xxMapper = session.getMapper(XxxMapper.class);
int size = updateList.size();
try {
    for(int i=0; i < size; i++) {
    	// updateByXxx 写好的单条数据的方法
        xxMapper.updateByXxx(updateList.get(i));
        if(i % BATCH_SIZE == 0 || i == size-1){
            //手动每1000个一提交,提交后无法回滚
            session.commit();
            //清理缓存,防止溢出
            session.clearCache();
        }
    }
}catch (Exception e) {
    session.rollback();
} finally {
    session.close();
}

方案三 : 重写 executeBatch 方法

java 复制代码
	// mybatis plus 源码
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateBatchById(Collection<T> entityList, int batchSize) {
        String sqlStatement = getSqlStatement(SqlMethod.UPDATE_BY_ID);
        return executeBatch(entityList, batchSize, (sqlSession, entity) -> {
            MapperMethod.ParamMap<T> param = new MapperMethod.ParamMap<>();
            param.put(Constants.ENTITY, entity);
            sqlSession.update(sqlStatement, param);
        });
    }
  • mybatis plus 的 executeBatch
  • 参考 mybatis plus 的updateBatchById 方法.
  • 调用处:
java 复制代码
//删除方法 deleteList  是要删除的主键list
List<String> deleteList = new ArrayList<>();
dao.batchDelete(deleteList, delete -> new QueryWrapper<String>().eq("xx", delete));

// 修改方法 OBJ 代码表对象
List<OBJ> updateList = new ArrayList<>();
dao.batchUpdate(updateList, update -> new LambdaQueryWrapper<OBJ>()
                        .eq(OBJ::getProductId, update.getProductId()));
  • 接口
java 复制代码
    boolean batchUpdate(List<OBJ> updateList, Function<OBJ, LambdaQueryWrapper> queryWrapperFunction);

    boolean batchDelete(List<String> deleteList, Function<String, QueryWrapper> queryWrapperFunction);
  • 重写方法 实现
java 复制代码
	@Override
    public boolean batchUpdate(List<OBJ> entityList, Function<OBJ, LambdaQueryWrapper> function) {
        return this.executeBatch(entityList, DEFAULT_BATCH_SIZE, (sqlSession, entity) -> {
            ParamMap param = new ParamMap();
            param.put(Constants.ENTITY, entity);
            param.put(Constants.WRAPPER, function.apply(entity));
            sqlSession.update(this.getSqlStatement(SqlMethod.UPDATE), param);
        });
    }

    @Override
    public boolean batchDelete(List<String> deleteList, Function<String, QueryWrapper> function) {
        return this.executeBatch(deleteList, DEFAULT_BATCH_SIZE, (sqlSession, entity) -> {
            ParamMap param = new ParamMap();
            param.put(Constants.ENTITY, entity);
            param.put(Constants.WRAPPER, function.apply(entity));
            sqlSession.delete(this.getSqlStatement(SqlMethod.DELETE), param);
        });
    }

结论:

  • 这种写法其实批量的效率还是比较慢的,如果对性能没有要求,并且还不想手写SQL的,可以试一试.
相关推荐
剑走偏锋o.O15 小时前
Java四大框架深度剖析:MyBatis、Spring、SpringMVC与SpringBoot
java·spring boot·spring·mybatis
风月歌15 小时前
基于springboot校园健康系统的设计与实现(源码+文档)
java·spring boot·后端·mysql·毕业设计·mybatis·源码
剑走偏锋o.O15 小时前
MyBatis框架详解与核心配置解读
java·学习·mybatis
ONEPEICE-ing1 天前
快速入门Springboot+vue——MybatisPlus多表查询及分页查询
前端·vue.js·spring boot·mybatis
wolf犭良1 天前
14、《SpringBoot+MyBatis集成(2)——进阶配置XML与注解的灵活运用》
xml·spring boot·mybatis
seabirdssss1 天前
重构测试项目为spring+springMVC+Mybatis框架
java·spring·重构·mvc·mybatis
火烧屁屁啦1 天前
【JavaEE进阶】MyBatis之动态SQL
java·java-ee·mybatis
嘵奇2 天前
MyBatis框架七:缓存
缓存·mybatis
火烧屁屁啦2 天前
【JavaEE进阶】MyBatis通过注解实现增删改查
java·java-ee·mybatis