mybatisplus 批量插入与修改

保留原mybatisplus自有的方法

1:重写injectMappedStatement,生成拼接批量新增/修改sql的脚本

/*** @Description 重写injectMappedStatement,生成拼接批量新增sql的脚本* @Author WangKun* @Date 2024/2/26 16:55* @Version*/
public class InsertBatchMethod extends AbstractMethod {@Overridepublic MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {final String sql = "<script>insert into %s %s values %s</script>";final String fieldSql = prepareFieldSql(tableInfo);final String valueSql = prepareValuesSql(tableInfo);final String sqlResult = String.format(sql, tableInfo.getTableName(), fieldSql, valueSql);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("<foreach collection=\"list\" item=\"item\" index=\"index\" open=\"(\" separator=\"),(\" close=\")\">");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("</foreach>");return valueSql.toString();}}
/*** @Description 重写injectMappedStatement,生成拼接批量更新sql的脚本* @Author WangKun* @Date 2024/2/26 16:51* @Version*/
public class UpdateBatchMethod extends AbstractMethod {@Overridepublic MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {final String sql = "<script>\n update %s %s \n where id in \n <foreach collection=\"list\" item=\"item\" separator=\",\" open=\"(\" close=\")\">\n #{item.id} </foreach> \n </script>";final String valueSql = prepareValuesSql(tableInfo);final String sqlResult = String.format(sql, tableInfo.getTableName(), valueSql);SqlSource sqlSource = languageDriver.createSqlSource(configuration, sqlResult, modelClass);return this.addUpdateMappedStatement(mapperClass, modelClass, "updateBatch", sqlSource);}private String prepareValuesSql(TableInfo tableInfo) {final StringBuilder valueSql = new StringBuilder();valueSql.append("<trim prefix=\"set\" suffixOverrides=\",\">\n");tableInfo.getFieldList().forEach(x -> {valueSql.append("<trim prefix=\"").append(x.getColumn()).append(" =(case id\" suffix=\"end),\">\n");valueSql.append("<foreach collection=\"list\" item=\"item\" >\n");valueSql.append("<if test=\"item.").append(x.getProperty()).append("!=null\">\n");valueSql.append("when #{item.id} then #{item.").append(x.getProperty()).append("}\n");valueSql.append("</if>\n");valueSql.append("</foreach>\n");valueSql.append("else ").append(x.getColumn());valueSql.append("</trim>\n");});valueSql.append("</trim>\n");return valueSql.toString();}
}

2:注入器注入

/*** @Description 批量插入 SQL 注入器 在Mapper中生成insertBatchSomeColumn(必须是这个方法名)方法*              不使用自带的伪批量* @Author WangKun* @Date 2023/12/8 14:49* @Version*/
public class BatchSqlInjector extends DefaultSqlInjector {@Overridepublic List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {// super.getMethodList() 保留 Mybatis Plus 自带的方法List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo);// 添加自定义方法:批量插入,方法名为 insertBatchSomeColumnmethodList.add(new InsertBatchSomeColumn());methodList.add(new InsertBatchMethod());methodList.add(new UpdateBatchMethod());return methodList;}}

3:在mybatisplus配置中加入注入器

/*** @Description mybatis-plus配置* @Author WangKun* @Date 2023/4/4 15:01* @Version*/
@Configuration
public class MybatisPlusConfig {/*** @param* @Description 分页* @Throws* @Return com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor* @Date 2023-04-11 16:46:11* @Author WangKun*/@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();PaginationInnerInterceptor innerInterceptor = new PaginationInnerInterceptor();innerInterceptor.setDbType(DbType.MYSQL);innerInterceptor.setOverflow(true);interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());interceptor.addInnerInterceptor(innerInterceptor);return interceptor;}/*** @param* @Description 新增是否以自增列为主键* @Throws* @Return com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer* @Date 2023-04-11 16:45:41* @Author WangKun*/@Beanpublic ConfigurationCustomizer configurationCustomizer() {return configuration -> configuration.setUseGeneratedShortKey(false);}/*** @param* @Description 开启返回map结果集的下划线转驼峰* @Throws* @Return com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer* @Date 2023-04-11 16:45:06* @Author WangKun*/@Beanpublic ConfigurationCustomizer mybatisConfigurationCustomizer() {return configuration -> configuration.setObjectWrapperFactory(new MybatisMapWrapperFactory());}/*** @Description 批量插入数据* @param* @Throws* @Return com.harmonywisdom.common.mybatisplus.BatchSqlInjector* @Date 2023-12-08 14:51:43* @Author WangKun*/@Beanpublic BatchSqlInjector batchSqlInjector() {return new BatchSqlInjector();}
}

4:定义mapper接口

/*** @Description 自定义批量加入* @Author WangKun* @Date 2023/12/8 15:35* @Version*/
public interface BaseBatchMapper<T> extends BaseMapper<T> {/*** 真实批量加入* insertBatchSomeColumn 必须和注入的 InsertBatchSomeColumn 类的id是一样的* 否则容器中找不到对应实体,会报错:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)*/long insertBatchSomeColumn(List<T> entityList);/*** 自定义批量新增,适用于mysql,并且数据已确定*/int insertBatch(@Param("list") List<T> list);/*** 自定义批量更新,适用于mysql,并且数据已确定*/int updateBatch(@Param("list") List<T> list);}

5:业务层使用

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/501319.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

基于java+springboot景区行李寄存管理系统设计和实现

基于javaspringboot景区行李寄存管理系统设计和实现 博主介绍&#xff1a;多年java开发经验&#xff0c;专注Java开发、定制、远程、文档编写指导等,csdn特邀作者、专注于Java技术领域 作者主页 央顺技术团队 Java毕设项目精品实战案例《1000套》 欢迎点赞 收藏 ⭐留言 文末获取…

Android 签名机制

V1是内部文件单个签 但是增加apk文件目录下面随意增加文件并不会有影响,它只关心meta-info文件 mf汇总清单的各个文件sha256 V2 整个APK文件,按文件进行hash 那么便不能随便在这里面增加文件了,增加了签名分块&#xff08;不然签名信息存哪里&#xff09;这里涉及一个文件概念…

门店纵深不足、入口有遮挡影响客流准确率?近景客流帮你搞定!

为了优化运营策略、提升门店营收&#xff0c;很多店铺和商场都会安装客流摄像机。但是在实际应用中&#xff0c;由于门店纵深受限等原因&#xff0c;导致无法使用之前的常规客流产品。 针对这种情况&#xff0c;悠络客最新研发了近景客流产品&#xff0c;即使存在入口被遮挡或门…

如何添加极狐GitLab Runner 信任域名证书

本文作者 徐晓伟 极狐Gitlab Runner 信任实例域名证书&#xff0c;用于注册注册极狐 GitLab Runner。 问题 参见 极狐gitlab-runner-host.md 说明 解决方案是使用颁发给域名 gitlab.test.helm.xuxiaowei.cn 的证书&#xff0c;可以使用自己的域名去各大云厂商免费申请&#…

初学Vue总结

0 Vue概述 问题&#xff1a;我们已经学过了htmlCssjavascript,可以开发前端页面了&#xff0c;但会发现&#xff0c;效率太低了。那么&#xff0c;有没有什么工具可以提高我们的开发效率&#xff0c;加快开发速度呢&#xff1f; 他来了&#xff0c;他来了&#xff0c;他大佬似…

C++小记 - 二叉树

文章目录 二叉树一、二叉树理论基础篇二叉树的种类满二叉树完全二叉树二叉搜索树平衡二叉搜索树 二叉树的存储方式链式存储&#xff1a;顺序存储&#xff1a;遍历规则&#xff1a;构造实现&#xff1a; 二叉树的遍历方式二叉树的定义 二、二叉树的递归遍历递归算法的三个要素:递…

护眼台灯哪个口碑最好?五大刷爆全网护眼台灯推荐

护眼台灯现在已经逐渐成为现代家庭不可或缺的一部分&#xff0c;然而市面上的产品质量良莠不齐&#xff0c;许多家长一不小心就选到质量不好的护眼台灯&#xff0c;可能就会面临光照不适、对视力造成影响等困扰。那么&#xff0c;究竟护眼台灯哪个最好&#xff1f;作为一名灯具…

Pytorch 复习总结 4

Pytorch 复习总结&#xff0c;仅供笔者使用&#xff0c;参考教材&#xff1a; 《动手学深度学习》Stanford University: Practical Machine Learning 本文主要内容为&#xff1a;Pytorch 深度学习计算。 本文先介绍了深度学习中自定义层和块的方法&#xff0c;然后介绍了一些…

Ps:索引颜色模式

Ps菜单&#xff1a;图像/模式/索引颜色 Image/Mode/Indexed Color 索引颜色 Indexed Color模式可生成最多 256 种颜色的 8 位图像文件。 这种颜色的限制使得索引颜色模式的图像文件相比于全彩图像&#xff08;如 RGB 颜色模式下的图像&#xff09;具有更小的文件大小&#xff0…

CentOS 7开启Web服务

之前有写过用kali开启web服务方法&#xff0c;这次写个用cendos7开启服务的步骤&#xff01; 1、安装httpd yum install -y httpd 若显示安装失败&#xff0c;报错原因为找不到httpd的安装包&#xff0c;可参考这篇文件更新yum源&#xff1a;CentOS 7更换yum源|详细步骤-CSDN…

云呐智能运维包含哪些内容?运维未来的发展方向是什么?

智能运维&#xff08;AIOps&#xff09;是一种使用人工智能应用程序来调节IT操作和维护的实践方式。它结合了大数据和机器学习技术&#xff0c;旨在自动化和改进IT操作和维护任务&#xff0c;如故障检测、因果分析和自动故障修复。以下是智能操作和维护的具体内容、挑战和解决方…

【leetcode】相交链表

大家好&#xff0c;我是苏貝&#xff0c;本篇博客带大家刷题&#xff0c;如果你觉得我写的还不错的话&#xff0c;可以给我一个赞&#x1f44d;吗&#xff0c;感谢❤️ 点击查看题目 思路: struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *he…