mybatisplus⾼效批量更新_mybatis-plus真正批量新增和更新使⽤mybatis-plus来进⾏批量新增和更新时,你会发现其实是⼀条条sql执⾏,下⾯进⾏优化。
1.添加InsertBatchMethod和UpdateBatchMethod类
import injector.AbstractMethod;
import adata.TableInfo;
slf4j.Slf4j;
import org.utor.keygen.NoKeyGenerator;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;
/**
* 批量插⼊⽅法实现
*/
@Slf4j
public class InsertBatchMethod extends AbstractMethod {
@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, TableName(), fieldSql, valueSql);
//log.debug("sqlResult----->{}", sqlResult);
SqlSource sqlSource = ateSqlSource(configuration, sqlResult, modelClass);
return this.addInsertMappedStatement(mapperClass, modelClass, "insertBatch", sqlSource, new NoKeyGenerator(), null, null);
}
private String prepareFieldSql(TableInfo tableInfo) {
StringBuilder fieldSql = new StringBuilder();
fieldSql.KeyColumn()).append(",");
fieldSql.delete(fieldSql.length() - 1, fieldSql.length());
fieldSql.insert(0, "(");
fieldSql.append(")");
String();
}
private String prepareValuesSql(TableInfo tableInfo) {
final StringBuilder valueSql = new StringBuilder();
valueSql.append("");
valueSql.append("#{item.").KeyProperty()).append("},");
valueSql.delete(valueSql.length() - 1, valueSql.length());
valueSql.append("");
String();
}
}
import injector.AbstractMethod;
import adata.TableInfo;
slf4j.Slf4j;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;
/**
* 批量更新⽅法实现,条件为主键,选择性更新
*/
@Slf4j
public class UpdateBatchMethod extends AbstractMethod {
@Override
public MappedStatement injectMappedStatement(Class> mapperClass, Class> modelClass, TableInfo tableInfo) {
String sql = "";
String additional = tableInfo.isWithVersion() ? VersionFieldInfo().getVersionOli("item", "item.") : "" + LogicDeleteSql(true, true);
String setSql = sqlSet(tableInfo.isWithLogicDelete(), false, tableInfo, false, "item", "item.");
String sqlResult = String.format(sql, TableName(), setSql, KeyColumn(), "item." + KeyProperty(), additional);
//log.debug("sqlResult----->{}", sqlResult);
SqlSource sqlSource = ateSqlSource(configuration, sqlResult, modelClass);
// 第三个参数必须和RootMapper的⾃定义⽅法名⼀致
return this.addUpdateMappedStatement(mapperClass, modelClass, "updateBatch", sqlSource);
}
}
2.添加⾃定义⽅法SQL注⼊器
import injector.AbstractMethod; import injector.DefaultSqlInjector; import java.util.List;
public class CustomizedSqlInjector extends DefaultSqlInjector {
/**
* 如果只需增加⽅法,保留mybatis plus⾃带⽅法,
* 可以先获取MethodList(),再添加add
*/
@Override
public List getMethodList(Class> mapperClass) {
List methodList = MethodList(mapperClass);
methodList.add(new InsertBatchMethod());
methodList.add(new UpdateBatchMethod());
return methodList;
}
}
3.注⼊配置
@MapperScan("apper")
@Configuration
public class MyBatisPlusConfig {
@Bean
public CustomizedSqlInjector customizedSqlInjector() {
return new CustomizedSqlInjector();
}
}
4.添加通⽤mapper
import apper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* 根Mapper,给表Mapper继承⽤的,可以⾃定义通⽤⽅法
* {@link apper.BaseMapper}
* {@link sion.service.IService}
* {@link sion.service.impl.ServiceImpl}
public interface RootMapper extends BaseMapper {
/**
* ⾃定义批量插⼊
* 如果要⾃动填充,@Param(xx) xx参数名必须是 list/collection/array 3个的其中之⼀
批量更新sql语句*/
int insertBatch(@Param("list") List list);
/**
* ⾃定义批量更新,条件为主键
* 如果要⾃动填充,@Param(xx) xx参数名必须是 list/collection/array 3个的其中之⼀
*/
int updateBatch(@Param("list") List list);
}
5.如何使⽤
@Repository
public interface UserInfoMapper extends RootMapper {
}
public interface UserInfoService extends IService {
int saveAll();
int updateAll();
}
@Service
public class UserInfoServiceImpl extends ServiceImpl implements UserInfoService{ @Override
public int saveAll() {
List list = new ArrayList<>();
for (int i = 0; i < 20; i++) {
UserInfo userInfo = new UserInfo();
userInfo.setUserName("厉害" + i);
userInfo.setSalt(RandomStringUtils.randomAlphabetic(6));
userInfo.setPassword(SecureUtil.sha256("123456" + Salt())); userInfo.setSex(0);
userInfo.setAvatar(LoginServiceImpl.AVATAR);
list.add(userInfo);
return baseMapper.insertBatch(list);
}
@Override
public int updateAll() {
List userInfos = baseMapper.selectList(Wrappers.lambdaQuery().between(BaseEntity::getId, 43, 62)); userInfos.forEach(userInfo -> {
userInfo.setUserName("更新了" + IdUtil.simpleUUID());
});
return baseMapper.updateBatch(userInfos);
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论