Mybatis-plus批量插入更新

Mybatis-plus批量插入更新

需求背景

最近有一个开发需求:需要通过接口对接别的系统批量推送的数据,根据唯一键判断是插入还是更新数据到数据库中。由于是批量推送数据,如果使用遍历更新的方式会因为重复与数据库进行连接、断开,导致性能较差,因此考虑采用批量插入、更新的方式。

方案

项目用的mybatis-plus,提供了saveBatch()、updateBatchById()、saveOrUpdateBatch()批量操作的方法,但是updateBatchById()、saveOrUpdateBatch()在进行更新操作时是根据主键ID进行比较的,无法直接使用。
根据上述背景,有以下三种方案

  1. 先批量删除再批量插入
    • 按唯一键查询得到全部已有数据
    • 通过主键id删除原有的数据
    • 再全量插入数据
    • 删除、插入操作控制在同一个事务中,防止数据不一致
  2. 分别批量更新、插入数据
    • 按唯一键查询得到全部已有数据
    • 先将1中得到的主键id和新接入数据匹配,再通过主键id批量更新数据
    • 最后全量插入余下数据
    • 更新、插入操作控制在同一个事务中,防止数据不一致
  3. 重写mybatis-plus的saveOrUpdateBatch()方法,根据非主键唯一键进行更新
    • 详细见下方代码实现

代码实现

个人比较推荐方案三,感觉代码更优雅一点(手动狗头),这里也只实现方案三的代码,重写saveOrUpdateBatch()方法,通过构建updateWrapper来自定义更新数据的相关字段和更新条件,同时通过@Transactional管理事务,保证操作前后的数据一致性。
例如:我们需要批量插入或更新员工信息,员工编号code是唯一键,通过code实现员工数据的新增和更新

@Override
@Transactional(rollbackFor = Exception.class)
public boolean saveOrUpdateBatchByCondition(List<Employee> employees, int batchSize) {
    TableInfo tableInfo = TableInfoHelper.getTableInfo(this.entityClass);
    Assert.notNull(tableInfo, "error: can not execute. because can not find cache of TableInfo for entity!");
    // 根据员工编码更新数据
    String updateProperty = "code";
    return this.executeBatch(employees, batchSize, ((sqlSession, employee) -> {
        Object codeVal = ReflectionKit.getMethodValue(this.entityClass, employee, updateProperty);
        LambdaUpdateWrapper<Employee> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.eq(Employee::getCode, employee.getCode());
        if (!StringUtils.checkValNull(codeVal) && !Objects.isNull(this.getOne(queryWrapper))) {
            MapperMethod.ParamMap<Object> param = new MapperMethod.ParamMap<>();
            param.put(Constants.WRAPPER, getUpdateWrapper(employee));
            param.put(Constants.ENTITY, employee);
            sqlSession.update(tableInfo.getSqlStatement(SqlMethod.UPDATE.getMethod()), param);
        } else {
            sqlSession.insert(tableInfo.getSqlStatement(SqlMethod.INSERT_ONE.getMethod()), employee);
        }
    }));
}

private LambdaUpdateWrapper<Employee> getUpdateWrapper(Employee employee) {
    final LambdaUpdateWrapper<Employee> updateWrapper = new LambdaUpdateWrapper<>();
    updateWrapper.set(Employee::getAge, employee.getAge())
            .set(Employee::getSubCompany, employee.getSubCompany())
            .set(Employee::getJob, employee.getJob())
            .set(Employee::getDepartment, employee.getDepartment())
            .set(Employee::getSalary, employee.getSalary())
            .eq(Employee::getCode, employee.getCode());
    return updateWrapper;
}

推送过来的数据:

[
    {
        "name": "大壮",
        "code": "DEV001",
        "age": 28,
        "subCompany": "上海公司",
        "job": "研发",
        "department": "研发一部",
        "salary": 6000
    },
    {
        "name": "铁柱",
        "code": "DEV003",
        "age": 34,
        "subCompany": "上海公司",
        "job": "研发",
        "department": "研发二部",
        "salary": 6000
    },
    {
        "name": "小明",
        "code": "DEV005",
        "age": 25,
        "subCompany": "南京公司",
        "job": "研发",
        "department": "研发二部",
        "salary": 5000
    }   
]

原始数据如下:
在这里插入图片描述
数据更新结果:
在这里插入图片描述
同理,如果只需要批量更新数据,将上述带中的insert逻辑删除即可。

  • 9
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Mybatis-Plus提供了批量插入更新的功能,可以大大提高数据操作的效率。 批量插入可以使用Mybatis-Plus提供的insertBatch方法,将多条记录一次性插入到数据库中。示例代码如下: List<User> userList = new ArrayList<>(); // 添加多条记录到userList中 userMapper.insertBatch(userList); 批量更新可以使用Mybatis-Plus提供的updateBatchById方法,将多条记录一次性更新数据库中。示例代码如下: List<User> userList = new ArrayList<>(); // 修改多条记录的信息 userMapper.updateBatchById(userList); 需要注意的是,批量插入更新的记录数不能太大,否则可能会导致数据库性能下降。建议在实际应用中根据具体情况进行调整。 ### 回答2: Mybatis-plus是一款基于Mybatis的增强工具,它提供了许多实用的功能来简化开发,其中就包括批量插入更新操作。 在实现批量插入更新操作时,我们通常会遵循以下步骤: 1. 创建一个实体类,该实体类需要继承Mybatis-plus提供的Model类,并定义需要插入或更新的字段属性。 2. 创建一个Mapper接口,该接口需要继承Mybatis-plus提供的BaseMapper接口,并定义批量插入更新的方法。 3. 在该Mapper接口中实现批量插入更新操作的SQL语句。 4. 在Service中调用Mapper接口中的批量插入更新方法,传入需要插入或更新的数据集合即可。 下面是一个示例代码: 1. 定义一个实体类: ``` @Data @TableName("user") public class User extends Model<User> { @TableId(value = "id", type = IdType.AUTO) private Long id; private String name; private Integer age; } ``` 2. 创建一个Mapper接口: ``` public interface UserMapper extends BaseMapper<User> { void batchInsert(List<User> userList); void batchUpdate(List<User> userList); } ``` 3. 实现批量插入更新操作的SQL语句: ``` public void batchInsert(List<User> userList) { SqlSession sqlSession = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH); UserMapper mapper = sqlSession.getMapper(UserMapper.class); for (User user : userList) { mapper.insert(user); } sqlSession.flushStatements(); sqlSession.commit(); sqlSession.clearCache(); } ``` ``` public void batchUpdate(List<User> userList) { SqlSession sqlSession = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH); UserMapper mapper = sqlSession.getMapper(UserMapper.class); for (User user : userList) { mapper.updateById(user); } sqlSession.flushStatements(); sqlSession.commit(); sqlSession.clearCache(); } ``` 4. 在Service中调用Mapper接口中的批量插入更新方法,传入需要插入或更新的数据集合即可: ``` @Autowired private UserMapper userMapper; @Transactional public void batchInsert(List<User> userList) { userMapper.batchInsert(userList); } @Transactional public void batchUpdate(List<User> userList) { userMapper.batchUpdate(userList); } ``` 总之,Mybatis-plus提供的批量插入更新操作非常简洁和高效,使用起来也比较方便,可以有效地提高开发效率。 ### 回答3: Mybatis-plus是一个基于Mybatis框架的增强工具,在Mybatis的基础上扩展并简化了一些操作,使得使用者可以更加便捷地进行数据库操作。Mybatis-plus提供了批量插入更新的功能,可以大大提高数据操作的效率和性能。 批量插入数据 使用Mybatis-plus进行批量插入数据的方法是使用mapper对象的batchInsert方法,示例如下: ```java List<User> userList = new ArrayList<>(); // 添加User对象到userList中 userService.batchInsert(userList); ``` 在上述示例代码中,我们将多个User对象添加到了userList中,然后调用userService的batchInsert方法进行批量插入。 批量更新数据 使用Mybatis-plus进行批量更新数据的方法是使用mapper对象的batchUpdate方法,示例如下: ```java List<User> userList = new ArrayList<>(); // 添加要更新的User对象到userList中 userService.batchUpdate(userList); ``` 在上述示例代码中,我们将多个要更新的User对象添加到了userList中,然后调用userService的batchUpdate方法进行批量更新。 需要注意的是,批量更新操作中要更新的字段必须相同,否则会更新失败。 总结 使用Mybatis-plus进行批量插入更新操作可以大大提高数据操作的效率和性能,但是需要注意,在批量更新操作中要更新的字段必须相同,否则会导致更新失败。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值