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的,可以试一试.
相关推荐
艾菜籽13 小时前
MyBatis动态sql与留言墙联系
java·数据库·sql·spring·mybatis
l0sgAi13 小时前
PageHelper-Cursor:支持游标分页的 PageHelper 修改版,解决 MyBatis 深分页性能问题
spring boot·mybatis
计算机学姐1 天前
基于SpringBoo+Vue的医院预约挂号管理系统【个性化推荐算法+可视化统计】
java·vue.js·spring boot·mysql·intellij-idea·mybatis·推荐算法
计算机学姐1 天前
基于微信小程序的奶茶店点餐平台【2026最新】
java·vue.js·spring boot·mysql·微信小程序·小程序·mybatis
后端小张2 天前
【JAVA 进阶】Mybatis-Plus 实战使用与最佳实践
java·spring boot·spring·spring cloud·tomcat·mybatis·mybatis plus
拜见老天師2 天前
使用mybatis-plus,实现将排序时,字段值为NULL的数据排在最后
java·mybatis
superlls2 天前
(场景题)怎么实现数据的批量插入?
笔记·mybatis
bug攻城狮3 天前
SaaS多租户架构实践:字段隔离方案(共享数据库+共享Schema)
mysql·架构·mybatis·springboot·1024程序员节
艾菜籽3 天前
MyBatis操作数据库入门
java·数据库·mybatis
不平衡的叉叉树5 天前
mybatis-plus的insertBatchSomeColumn方法实现批量插入
java·mybatis