mybatis-plus添加replace(自定义)方法,添加sql注入器SqlInjector

本文主要是介绍mybatis-plus添加replace(自定义)方法,添加sql注入器SqlInjector,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1. 继承DefaultSqlInjector

import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import org.springframework.stereotype.Component;import java.util.List;/*** 自定义sql注入器** @author yz* @since 2024/08/23*/
@Component
public class AntaiSqlInjector extends DefaultSqlInjector {@Overridepublic List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo);// 添加一个替换方法methodList.add(new Replace());return methodList;}
}

import lombok.Getter;/*** 自定义MybatisPlus 支持 SQL 方法** @author yz* @since 2024/8/23 17:55*/
@Getter
public enum AntaiSqlMethod {REPLACE("replace", "替换一条数据", "<script>\nREPLACE INTO %s %s VALUES %s\n</script>");private final String method;private final String desc;private final String sql;AntaiSqlMethod(String method, String desc, String sql) {this.method = method;this.desc = desc;this.sql = sql;}}

2. 实现Replace 的 sql 方法

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.enums.SqlMethod;
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlInjectionUtils;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
import org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator;
import org.apache.ibatis.executor.keygen.KeyGenerator;
import org.apache.ibatis.executor.keygen.NoKeyGenerator;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;/*** 替换一条数据(选择字段插入)存在则更新,不存在则插入** @author yz* @since 2024/08/23*/
@SuppressWarnings("all")
public class Replace extends AbstractMethod {/*** 自增主键字段是否忽略** @since 3.5.4*/private boolean ignoreAutoIncrementColumn;public Replace() {this(AntaiSqlMethod.REPLACE.getMethod());}/*** @param ignoreAutoIncrementColumn 是否忽略自增长主键字段* @since 3.5.4*/public Replace(boolean ignoreAutoIncrementColumn) {this(AntaiSqlMethod.REPLACE.getMethod());this.ignoreAutoIncrementColumn = ignoreAutoIncrementColumn;}/*** @param name 方法名* @since 3.5.0*/public Replace(String name) {super(name);}/*** @param name                      方法名* @param ignoreAutoIncrementColumn 是否忽略自增长主键字段* @since 3.5.4*/public Replace(String name, boolean ignoreAutoIncrementColumn) {super(name);this.ignoreAutoIncrementColumn = ignoreAutoIncrementColumn;}@Overridepublic MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {KeyGenerator keyGenerator = NoKeyGenerator.INSTANCE;SqlMethod sqlMethod = SqlMethod.INSERT_ONE;String columnScript = SqlScriptUtils.convertTrim(tableInfo.getAllInsertSqlColumnMaybeIf(null, ignoreAutoIncrementColumn),LEFT_BRACKET, RIGHT_BRACKET, null, COMMA);String valuesScript = LEFT_BRACKET + NEWLINE + SqlScriptUtils.convertTrim(tableInfo.getAllInsertSqlPropertyMaybeIf(null, ignoreAutoIncrementColumn),null, null, null, COMMA) + NEWLINE + RIGHT_BRACKET;String keyProperty = null;String keyColumn = null;// 表包含主键处理逻辑,如果不包含主键当普通字段处理if (StringUtils.isNotBlank(tableInfo.getKeyProperty())) {if (tableInfo.getIdType() == IdType.AUTO) {/* 自增主键 */keyGenerator = Jdbc3KeyGenerator.INSTANCE;keyProperty = tableInfo.getKeyProperty();// 去除转义符keyColumn = SqlInjectionUtils.removeEscapeCharacter(tableInfo.getKeyColumn());} else if (null != tableInfo.getKeySequence()) {keyGenerator = TableInfoHelper.genKeyGenerator(methodName, tableInfo, builderAssistant);keyProperty = tableInfo.getKeyProperty();keyColumn = tableInfo.getKeyColumn();}}String sql = String.format(AntaiSqlMethod.REPLACE.getSql(), tableInfo.getTableName(), columnScript, valuesScript);SqlSource sqlSource = super.createSqlSource(configuration, sql, modelClass);return this.addInsertMappedStatement(mapperClass, modelClass, methodName, sqlSource, keyGenerator, keyProperty, keyColumn);}
}

3. 实现一个批量替换的方法,作为调用入口

    /*** 批量替换** @param entityList 实体列表* @param batchSize  批量大小* @return {@link Boolean }*/default Boolean replaceBatch(Collection<T> entityList, int batchSize) {if (CollectionUtils.isEmpty(entityList)) {return false;}Class<T> entityClass = getEntityClass(entityList);Class<?> mapperClass = ClassUtils.toClassConfident(getTableInfo(entityClass).getCurrentNamespace());String sqlStatement = mapperClass.getName()+ StringPool.DOT+ AntaiSqlMethod.REPLACE.getMethod();return SqlHelper.executeBatch(entityClass, log, entityList, batchSize, (sqlSession, entity) -> sqlSession.insert(sqlStatement, entity));}/*** 从集合中获取实体类型** @param entityList 实体集合* @param <T>        实体类型* @return 实体类型*/static <T> Class<T> getEntityClass(Collection<T> entityList) {Class<T> entityClass = null;for (T entity : entityList) {if (entity != null && entity.getClass() != null) {entityClass = (Class<T>) entity.getClass();break;}}Assert.notNull(entityClass, "error: can not get entityClass from entityList");return entityClass;}

4. 最终效果

image.png
image.png

这篇关于mybatis-plus添加replace(自定义)方法,添加sql注入器SqlInjector的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1110425

相关文章

SQL BETWEEN 的常见用法小结

《SQLBETWEEN的常见用法小结》BETWEEN操作符是SQL中非常有用的工具,它允许你快速选取某个范围内的值,本文给大家介绍SQLBETWEEN的常见用法,感兴趣的朋友一起看看吧... 在SQL中,BETWEEN是一个操作符,用于选取介于两个值之间的数据。它包含这两个边界值。BETWEEN操作符常用

MySQL索引的优化之LIKE模糊查询功能实现

《MySQL索引的优化之LIKE模糊查询功能实现》:本文主要介绍MySQL索引的优化之LIKE模糊查询功能实现,本文通过示例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 目录一、前缀匹配优化二、后缀匹配优化三、中间匹配优化四、覆盖索引优化五、减少查询范围六、避免通配符开头七、使用外部搜索引擎八、分

Spring 基于XML配置 bean管理 Bean-IOC的方法

《Spring基于XML配置bean管理Bean-IOC的方法》:本文主要介绍Spring基于XML配置bean管理Bean-IOC的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一... 目录一. spring学习的核心内容二. 基于 XML 配置 bean1. 通过类型来获取 bean2. 通过

MySql match against工具详细用法

《MySqlmatchagainst工具详细用法》在MySQL中,MATCH……AGAINST是全文索引(Full-Textindex)的查询语法,它允许你对文本进行高效的全文搜素,支持自然语言搜... 目录一、全文索引的基本概念二、创建全文索引三、自然语言搜索四、布尔搜索五、相关性排序六、全文索引的限制七

基于Python实现读取嵌套压缩包下文件的方法

《基于Python实现读取嵌套压缩包下文件的方法》工作中遇到的问题,需要用Python实现嵌套压缩包下文件读取,本文给大家介绍了详细的解决方法,并有相关的代码示例供大家参考,需要的朋友可以参考下... 目录思路完整代码代码优化思路打开外层zip压缩包并遍历文件:使用with zipfile.ZipFil

Python处理函数调用超时的四种方法

《Python处理函数调用超时的四种方法》在实际开发过程中,我们可能会遇到一些场景,需要对函数的执行时间进行限制,例如,当一个函数执行时间过长时,可能会导致程序卡顿、资源占用过高,因此,在某些情况下,... 目录前言func-timeout1. 安装 func-timeout2. 基本用法自定义进程subp

数据库面试必备之MySQL中的乐观锁与悲观锁

《数据库面试必备之MySQL中的乐观锁与悲观锁》:本文主要介绍数据库面试必备之MySQL中乐观锁与悲观锁的相关资料,乐观锁适用于读多写少的场景,通过版本号检查避免冲突,而悲观锁适用于写多读少且对数... 目录一、引言二、乐观锁(一)原理(二)应用场景(三)示例代码三、悲观锁(一)原理(二)应用场景(三)示例

Python列表去重的4种核心方法与实战指南详解

《Python列表去重的4种核心方法与实战指南详解》在Python开发中,处理列表数据时经常需要去除重复元素,本文将详细介绍4种最实用的列表去重方法,有需要的小伙伴可以根据自己的需要进行选择... 目录方法1:集合(set)去重法(最快速)方法2:顺序遍历法(保持顺序)方法3:副本删除法(原地修改)方法4:

Python中判断对象是否为空的方法

《Python中判断对象是否为空的方法》在Python开发中,判断对象是否为“空”是高频操作,但看似简单的需求却暗藏玄机,从None到空容器,从零值到自定义对象的“假值”状态,不同场景下的“空”需要精... 目录一、python中的“空”值体系二、精准判定方法对比三、常见误区解析四、进阶处理技巧五、性能优化

C++中初始化二维数组的几种常见方法

《C++中初始化二维数组的几种常见方法》本文详细介绍了在C++中初始化二维数组的不同方式,包括静态初始化、循环、全部为零、部分初始化、std::array和std::vector,以及std::vec... 目录1. 静态初始化2. 使用循环初始化3. 全部初始化为零4. 部分初始化5. 使用 std::a