mybatisplus添加真正的批量新增、批量更新的实现方法 |
您所在的位置:网站首页 › mybatis-plus批量新增 › mybatisplus添加真正的批量新增、批量更新的实现方法 |
mybatisplus添加真正的批量新增、批量更新的实现方法
发布时间:2021-03-24 12:25:15
来源:亿速云
阅读:22481
作者:小新
栏目:开发技术
这篇文章主要介绍mybatisplus添加真正的批量新增、批量更新的实现方法,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完! 使用mybatis-plus来进行批量新增和更新时,你会发现其实是一条条sql执行,下面进行优化。 1.添加InsertBatchMethod和UpdateBatchMethod类import com.baomidou.mybatisplus.core.injector.AbstractMethod; import com.baomidou.mybatisplus.core.metadata.TableInfo; import lombok.extern.slf4j.Slf4j; import org.apache.ibatis.executor.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 = "insert into %s %s values %s"; 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); 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(); } }import com.baomidou.mybatisplus.core.injector.AbstractMethod; import com.baomidou.mybatisplus.core.metadata.TableInfo; import lombok.extern.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 = "\n\nupdate %s %s where %s=#{%s} %s\n\n"; 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); } }2.添加自定义方法SQL注入器import com.baomidou.mybatisplus.core.injector.AbstractMethod; import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector; import java.util.List; 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; } }3.注入配置@MapperScan("com.xxx.mapper") @Configuration public class MyBatisPlusConfig { @Bean public CustomizedSqlInjector customizedSqlInjector() { return new CustomizedSqlInjector(); } }4.添加通用mapperimport com.baomidou.mybatisplus.core.mapper.BaseMapper; import org.apache.ibatis.annotations.Param; import java.util.List; /** * 根Mapper,给表Mapper继承用的,可以自定义通用方法 * {@link com.baomidou.mybatisplus.core.mapper.BaseMapper} * {@link com.baomidou.mybatisplus.extension.service.IService} * {@link com.baomidou.mybatisplus.extension.service.impl.ServiceImpl} */ public interface RootMapper extends 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); }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 { userInfo.setUserName("更新了" + IdUtil.simpleUUID()); }); return baseMapper.updateBatch(userInfos); } }以上是“mybatisplus添加真正的批量新增、批量更新的实现方法”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道! 推荐阅读: Python批量更新已安装库的方法 使用Mybatis怎么实现批量更新免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:[email protected]进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。 mybatisplus 上一篇新闻:JDBC连接Mysql长时间无动作连接失效怎么办 下一篇新闻:在计算机中网卡属于什么设备 猜你喜欢 linux如何启动docker服务 PHP中ProtoBuf的使用方法 keras实现tensorflow与theano相互转换的方法 如何用Android实现加载效果 PHP中堆排序的原理和应用 Android制作水平圆点加载进度条 Keras怎么实现Theano和TensorFlow切换 Python中select和selectors的用法 Unity制作俄罗斯方块游戏 怎么将tensorflow 2.0的模型转成 tf1.x 版本的pb模型 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |