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的,可以试一试.
相关推荐
耀耀_很无聊6 小时前
07_通过 Mybatis 自动填充记录的创建时间和更新时间
mybatis
程序员张37 小时前
SQL分析与打印-p6spy组件
spring boot·sql·mybatis·mybatisplus·p6spy
喜欢敲代码的程序员1 天前
SpringBoot+Mybatis+MySQL+Vue+ElementUI前后端分离版:项目搭建(一)
spring boot·mysql·elementui·vue·mybatis
一只猿Hou2 天前
java分页插件| MyBatis-Plus分页 vs PageHelper分页:全面对比与最佳实践
java·mybatis
java—大象11 天前
基于java SSM的房屋租赁系统设计和实现
java·开发语言·数据库·spring boot·layui·mybatis
Mutig_s11 天前
Spring Boot动态数据源切换:优雅实现多数据源管理
java·数据库·spring boot·后端·mybatis
编程乐学(Arfan开发工程师)11 天前
73、单元测试-断言机制
服务器·数据库·servlet·单元测试·sqlite·log4j·mybatis
小时候的阳光12 天前
MyBatis 的一级缓存导致的数据一致性问题分析
缓存·mybatis·事务·隔离级别
烙印60112 天前
MyBatis原理剖析(三)--加载配置文件
服务器·tomcat·mybatis