SpringBoot3 MybatisPlus 修改
- 前言
- 修改 API
- update 条件
- update 对象+条件
- updateById
- updateById 批量(默认)
- updateById 批量(自定义)
前言
本篇中使用到的项目工程是在《SpringBoot3 MybatisPlus 加入日志功能》基础上,持续功能开发。
修改 API
update 条件
无实体参数的 update(Wrapper updateWrapper) 方法,这个方法是默认方法,底层直接调用 update(null, updateWrapper),核心特点是所有更新字段和 where 条件都通过 Wrapper 统一配置,无需传入实体,同时要注意它不支持字段自动填充(比如创建时间、更新时间的自动赋值)。
官网 API:
/** * 根据 Wrapper 更新记录 * <p>此方法无法进行自动填充,如需自动填充请使用{@link #update(Object, Wrapper)}</p> * * @param updateWrapper {@link UpdateWrapper} or {@link LambdaUpdateWrapper} * @since 3.5.4 */defaultintupdate(@Param(Constants.WRAPPER)Wrapper<T>updateWrapper){returnupdate(null,updateWrapper);}Demo 示例:
@TestpublicvoidtestUpdate_02(){ClientEntityclientEntity=clientMapper.selectById(301L);System.out.println("修改之前:"+JSON.toJSONString(clientEntity));UpdateWrapper<ClientEntity>updateWrapper=newUpdateWrapper<>();updateWrapper.lambda().eq(ClientEntity::getId,301L).set(ClientEntity::getNickName,"无实体更新-昵称-测试");intupdate=clientMapper.update(updateWrapper);System.out.println("--->>> update = "+update);clientEntity=clientMapper.selectById(301L);System.out.println("修改之后==>>>clientEntity = "+JSON.toJSONString(clientEntity));}输出日志:
2026-01-28T22:37:39.009+08:00INFO15368---[springboot3-mybatisplus][main]com.zaxxer.hikari.HikariDataSource:HikariPool-1-Startcompleted.JDBCConnection[HikariProxyConnection@187931144wrappingcom.mysql.cj.jdbc.ConnectionImpl@a1cb94]will not be managed bySpring==>Preparing:SELECTid,client_id,user_no,user_password,nick_name,real_name,created_time,upated_time FROM clientWHEREid=?==>Parameters:301(Long)<==Columns:id,client_id,user_no,user_password,nick_name,real_name,created_time,upated_time<==Row:301,301,null,null,更新后-昵称_测试,null,2026-01-2822:07:21,2026-01-2822:07:21<==Total:1Closingnon transactionalSqlSession[org.apache.ibatis.session.defaults.DefaultSqlSession@2487e20]修改之前:{"clientId":301,"createdTime":"2026-01-28 22:07:21","id":301,"nickName":"更新后-昵称_测试","upatedTime":"2026-01-28 22:07:21"}CreatinganewSqlSessionSqlSession[org.apache.ibatis.session.defaults.DefaultSqlSession@3395c2a7]was not registeredforsynchronization because synchronization is not active JDBCConnection[HikariProxyConnection@1561688223wrappingcom.mysql.cj.jdbc.ConnectionImpl@a1cb94]will not be managed bySpring==>Preparing:UPDATE clientSETnick_name=?WHERE(id=?)==>Parameters:无实体更新-昵称-测试(String),301(Long)<==Updates:1Closingnon transactionalSqlSession[org.apache.ibatis.session.defaults.DefaultSqlSession@3395c2a7]--->>>update=1CreatinganewSqlSessionSqlSession[org.apache.ibatis.session.defaults.DefaultSqlSession@4ebed2b3]was not registeredforsynchronization because synchronization is not active JDBCConnection[HikariProxyConnection@285646508wrappingcom.mysql.cj.jdbc.ConnectionImpl@a1cb94]will not be managed bySpring==>Preparing:SELECTid,client_id,user_no,user_password,nick_name,real_name,created_time,upated_time FROM clientWHEREid=?==>Parameters:301(Long)<==Columns:id,client_id,user_no,user_password,nick_name,real_name,created_time,upated_time<==Row:301,301,null,null,无实体更新-昵称-测试,null,2026-01-2822:07:21,2026-01-2822:07:21<==Total:1Closingnon transactionalSqlSession[org.apache.ibatis.session.defaults.DefaultSqlSession@4ebed2b3]修改之后==>>>clientEntity={"clientId":301,"createdTime":"2026-01-28 22:07:21","id":301,"nickName":"无实体更新-昵称-测试","upatedTime":"2026-01-28 22:07:21"}update 对象+条件
官网 API:
/** * 根据 whereEntity 条件,更新记录 * * @param entity 实体对象 (set 条件值,可以为 null,当entity为null时,无法进行自动填充) * @param updateWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句) */intupdate(@Param(Constants.ENTITY)Tentity,@Param(Constants.WRAPPER)Wrapper<T>updateWrapper);Demo 示例:
@TestpublicvoidtestUpdate(){ClientEntityclientEntity=newClientEntity();clientEntity.setNickName("更新后-昵称_测试");UpdateWrapper<ClientEntity>updateWrapper=newUpdateWrapper<>();updateWrapper.eq("id",301L);intupdate=clientMapper.update(clientEntity,updateWrapper);System.out.println("update = "+update);// 进行查询验证.clientEntity=clientMapper.selectById(301L);System.out.println("clientEntity = "+JSON.toJSONString(clientEntity));}输出日志:
2026-01-28T22:27:48.815+08:00INFO17412---[springboot3-mybatisplus][main]com.zaxxer.hikari.HikariDataSource:HikariPool-1-Startcompleted.JDBCConnection[HikariProxyConnection@1376533963wrappingcom.mysql.cj.jdbc.ConnectionImpl@7791ff50]will not be managed bySpring==>Preparing:UPDATE clientSETnick_name=?WHERE(id=?)==>Parameters:更新后-昵称_测试(String),301(Long)<==Updates:1Closingnon transactionalSqlSession[org.apache.ibatis.session.defaults.DefaultSqlSession@3a70575]update=1CreatinganewSqlSessionSqlSession[org.apache.ibatis.session.defaults.DefaultSqlSession@5d74507c]was not registeredforsynchronization because synchronization is not active JDBCConnection[HikariProxyConnection@800040885wrappingcom.mysql.cj.jdbc.ConnectionImpl@7791ff50]will not be managed bySpring==>Preparing:SELECTid,client_id,user_no,user_password,nick_name,real_name,created_time,upated_time FROM clientWHEREid=?==>Parameters:301(Long)<==Columns:id,client_id,user_no,user_password,nick_name,real_name,created_time,upated_time<==Row:301,301,null,null,更新后-昵称_测试,null,2026-01-2822:07:21,2026-01-2822:07:21<==Total:1Closingnon transactionalSqlSession[org.apache.ibatis.session.defaults.DefaultSqlSession@5d74507c]clientEntity={"clientId":301,"createdTime":"2026-01-28 22:07:21","id":301,"nickName":"更新后-昵称_测试","upatedTime":"2026-01-28 22:07:21"}2026-01-28T22:27:48.996+08:00INFO17412---[springboot3-mybatisplus][ionShutdownHook]com.zaxxer.hikari.HikariDataSource:HikariPool-1-Shutdowninitiated...updateById
官网 API:
/** * 根据 ID 修改 * * @param entity 实体对象 */intupdateById(@Param(Constants.ENTITY)Tentity);Demo 示例:
@TestpublicvoidtestUpdate_03(){ClientEntityclientEntity=newClientEntity();clientEntity.setId(301L);clientEntity.setRealName("根据ID更新-名称");intupdate=clientMapper.updateById(clientEntity);System.out.println("update = "+update);clientEntity=clientMapper.selectById(301L);System.out.println("clientEntity = "+JSON.toJSONString(clientEntity));}输出日志:
2026-01-28T22:41:24.244+08:00INFO13536---[springboot3-mybatisplus][main]com.zaxxer.hikari.HikariDataSource:HikariPool-1-Startcompleted.JDBCConnection[HikariProxyConnection@65194223wrappingcom.mysql.cj.jdbc.ConnectionImpl@362be0cd]will not be managed bySpring==>Preparing:UPDATE clientSETreal_name=?WHEREid=?==>Parameters:根据ID更新-名称(String),301(Long)<==Updates:1Closingnon transactionalSqlSession[org.apache.ibatis.session.defaults.DefaultSqlSession@d8e4250]update=1CreatinganewSqlSessionSqlSession[org.apache.ibatis.session.defaults.DefaultSqlSession@5b59c3d]was not registeredforsynchronization because synchronization is not active JDBCConnection[HikariProxyConnection@110044003wrappingcom.mysql.cj.jdbc.ConnectionImpl@362be0cd]will not be managed bySpring==>Preparing:SELECTid,client_id,user_no,user_password,nick_name,real_name,created_time,upated_time FROM clientWHEREid=?==>Parameters:301(Long)<==Columns:id,client_id,user_no,user_password,nick_name,real_name,created_time,upated_time<==Row:301,301,null,null,无实体更新-昵称-测试,根据ID更新-名称,2026-01-2822:07:21,2026-01-2822:07:21<==Total:1Closingnon transactionalSqlSession[org.apache.ibatis.session.defaults.DefaultSqlSession@5b59c3d]clientEntity={"clientId":301,"createdTime":"2026-01-28 22:07:21","id":301,"nickName":"无实体更新-昵称-测试","realName":"根据ID更新-名称","upatedTime":"2026-01-28 22:07:21"}2026-01-28T22:41:24.417+08:00INFO13536---[springboot3-mybatisplus][ionShutdownHook]com.zaxxer.hikari.HikariDataSource:HikariPool-1-Shutdowninitiated...updateById 批量(默认)
参考官网 API 可知默认 1000 条数量批次提交。(
int DEFAULT_BATCH_SIZE = 1000;)
官网 API:
/** * 根据ID 批量更新 * * @param entityList 实体对象集合 * @since 3.5.7 */defaultList<BatchResult>updateById(Collection<T>entityList){returnupdateById(entityList,Constants.DEFAULT_BATCH_SIZE);}/** * 默认批次提交数量 */intDEFAULT_BATCH_SIZE=1000;updateById 批量(自定义)
官网 API:
/** * 根据ID 批量更新 * * @param entityList 实体对象集合 * @param batchSize 插入批次数量 * @since 3.5.7 */defaultList<BatchResult>updateById(Collection<T>entityList,intbatchSize){MapperProxyMetadatamapperProxyMetadata=MybatisUtils.getMapperProxy(this);MybatisBatch.Method<T>method=newMybatisBatch.Method<>(mapperProxyMetadata.getMapperInterface());SqlSessionFactorysqlSessionFactory=MybatisUtils.getSqlSessionFactory(mapperProxyMetadata.getSqlSession());returnMybatisBatchUtils.execute(sqlSessionFactory,entityList,method.updateById(),batchSize);}Demo 示例:
@TestpublicvoidtestUpdate_04(){// 生成批量测试数据.List<ClientEntity>clientEntityList=newArrayList();for(inti=300;i<=303;i++){ClientEntityclientEntity=newClientEntity();clientEntity.setId(Long.valueOf(i+1));clientEntity.setClientId(Long.valueOf(i+1));// 此处省略其他属性的 set 方法// clientEntity.setXXX;clientEntity.setNickName("批量-测试姓名"+i);clientEntityList.add(clientEntity);}// 默认批量提交数量intbatchSize=2;clientMapper.updateById(clientEntityList,batchSize);}输出日志:
2026-01-28T22:45:40.694+08:00INFO4580---[springboot3-mybatisplus][main]com.zaxxer.hikari.HikariDataSource:HikariPool-1-Startcompleted.JDBCConnection[HikariProxyConnection@1878814375wrappingcom.mysql.cj.jdbc.ConnectionImpl@2833c093]will not be managed bySpring==>Preparing:UPDATE clientSETclient_id=?,nick_name=?WHEREid=?==>Parameters:301(Long),批量-测试姓名300(String),301(Long)==>Parameters:302(Long),批量-测试姓名301(String),302(Long)==>Preparing:UPDATE clientSETclient_id=?,nick_name=?WHEREid=?==>Parameters:303(Long),批量-测试姓名302(String),303(Long)若有转载,请标明出处:https://blog.csdn.net/CharlesYuangc/article/details/157479468