十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
本篇内容介绍了“mybatisplus批量插入修改的方法是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

创新互联建站专注于浑江网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供浑江营销型网站建设,浑江网站制作、浑江网页设计、浑江网站官网定制、小程序制作服务,打造浑江网络公司原创品牌,更为您提供浑江网站排名全网营销落地服务。
IService 的实现类 ServiceImpl 中截取一段代码
/** * 批量插入 * * @param entityList ignore * @param batchSize ignore * @return ignore */ @Transactional(rollbackFor = Exception.class) @Override public boolean saveBatch(CollectionentityList, int batchSize){ String sqlStatement = getSqlStatement(SqlMethod.INSERT_ONE); return executeBatch(entityList, batchSize, (sqlSession, entity) -> sqlSession.insert(sqlStatement, entity)); }
会发现,其实是在循环插入, 那么如果这样我们有两种选择
1 使用mybatis 的xml文件,自己拼接插入,修改语句,就像最原始的那样,通过
第一种不再赘述,现在说明第二种用法
一共需要五步;
第一步: 一般引入mybaits-plus 都会有相应的配置类, MybatisPlusConfig 名字无所谓,作用是一样的,一般都会用自带的分页插件,可以在此基础上,继续添加,给出我的配置
// 分页差距
@Configuration
public class MybatisPlusConfig {
@Bean
@ConditionalOnMissingBean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor paginationInterceptor = new MybatisPlusInterceptor();
PaginationInnerInterceptor paginationInnerInterceptor= new PaginationInnerInterceptor(DbType.MySQL);
paginationInterceptor.addInnerInterceptor(paginationInnerInterceptor);
return paginationInterceptor;
}
/**
* 自动填充功能
* @return
*/
@Bean
@ConditionalOnMissingBean
public GlobalConfig globalConfig(){
GlobalConfig globalConfig = new GlobalConfig();
// globalConfig.setMetaObjectHandler(new MybatisMetaObjectHandler());
return globalConfig;
}
// 自定义sql注入器
@Bean
public CustomizedSqlInjector customizedSqlInjector(){
return new CustomizedSqlInjector();
}
}第二步,创建自定义sql注入器
/**
* 自定义方法SQL注入器
*/
public class CustomizedSqlInjector extends DefaultSqlInjector {
/**
* 如果只需增加方法,保留mybatis plus自带方法,
* 可以先获取super.getMethodList(),再添加add
*/
@Override
public List getMethodList(Class> mapperClass) {
List methodList = super.getMethodList(mapperClass);
methodList.add(new InsertBatchMethod());
methodList.add(new UpdateBatchMethod());
return methodList;
}
} 第三步: 创建一个类似于mybaits-plus 中的 BaseMapper的一个接口,我这里叫做RootMapper ,然后继承BaseMapper ,并新增两个批量操作方法, insertBatch updateBatch
/** * @Description 使用的时候,只需要继承RootMapper即可 * @Author FL * @Date 13:43 2022/5/5 * @Param **/ public interface RootMapperextends BaseMapper { /** * 自定义批量插入 * 如果要自动填充,@Param(xx) xx参数名必须是 list/collection/array 3个的其中之一 */ int insertBatch(@Param("list") List list); /** * 自定义批量更新,条件为主键 * 如果要自动填充,@Param(xx) xx参数名必须是 list/collection/array 3个的其中之一 */ int updateBatch(@Param("list") List list); }
第四步: 分别创建上述两个方法的具体实现类
@Slf4j
public class InsertBatchMethod extends AbstractMethod {
/**
* insert into user(id, name, age) values (1, "a", 17), (2, "b", 18);
*/
@Override
public MappedStatement injectMappedStatement(Class> mapperClass, Class> modelClass, TableInfo tableInfo){
final String sql = "";
final String fieldSql = prepareFieldSql(tableInfo);
final String valueSql = prepareValuesSql(tableInfo);
final String sqlResult = String.format(sql, tableInfo.getTableName(), fieldSql, valueSql);
log.debug("sqlResult----->{}", sqlResult);
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sqlResult, modelClass);
// 第三个参数必须和RootMapper的自定义方法名一致
return this.addInsertMappedStatement(mapperClass, modelClass, "insertBatch", sqlSource, new NoKeyGenerator(), null, null);
}
private String prepareFieldSql(TableInfo tableInfo){
StringBuilder fieldSql = new StringBuilder();
fieldSql.append(tableInfo.getKeyColumn()).append(",");
tableInfo.getFieldList().forEach(x -> {
fieldSql.append(x.getColumn()).append(",");
});
fieldSql.delete(fieldSql.length() - 1, fieldSql.length());
fieldSql.insert(0, "(");
fieldSql.append(")");
return fieldSql.toString();
}
private String prepareValuesSql(TableInfo tableInfo){
final StringBuilder valueSql = new StringBuilder();
valueSql.append("");
valueSql.append("#{item.").append(tableInfo.getKeyProperty()).append("},");
tableInfo.getFieldList().forEach(x -> valueSql.append("#{item.").append(x.getProperty()).append("},"));
valueSql.delete(valueSql.length() - 1, valueSql.length());
valueSql.append(" ");
return valueSql.toString();
}
}
=============================================================================
/**
* 批量更新方法实现,条件为主键,选择性更新
*/
@Slf4j
public class UpdateBatchMethod extends AbstractMethod {
/**
* update user set name = "a", age = 17 where id = 1;
* update user set name = "b", age = 18 where id = 2;
*/
@Override
public MappedStatement injectMappedStatement(Class> mapperClass, Class> modelClass, TableInfo tableInfo){
String sql = "";
String additional = tableInfo.isWithVersion() ? tableInfo.getVersionFieldInfo().getVersionOli("item", "item.") : "" + tableInfo.getLogicDeleteSql(true, true);
String setSql = sqlSet(tableInfo.isWithLogicDelete(), false, tableInfo, false, "item", "item.");
String sqlResult = String.format(sql, tableInfo.getTableName(), setSql, tableInfo.getKeyColumn(), "item." + tableInfo.getKeyProperty(), additional);
log.debug("sqlResult----->{}", sqlResult);
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sqlResult, modelClass);
// 第三个参数必须和RootMapper的自定义方法名一致
return this.addUpdateMappedStatement(mapperClass, modelClass, "updateBatch", sqlSource);
}
}第五步: 使用,将原有的继承BaseMapper的方法,改写为继承RootMapper ,后续批量操作,直接使用新增的两个方法进行处理即可
“mybatisplus批量插入修改的方法是什么”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!