mybatis自定义插件动态修改sql语句
step1:定义Interceptor实现org.apache.ibatis.plugin.Interceptor
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.ParameterMapping;
import org.apache.ibatis.mapping.SqlSource;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Value; import java.util.Properties; /**
* code by me
* <p>
* Data:2017/8/10 Time:11:28
* User:lbh
*/
@Intercepts
({
@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}),
})
public class FDNSqlInterceptor implements Interceptor {
private static Log logger = LogFactory.getLog(FDNSqlInterceptor.class); @Value("${fdn.open}")
private boolean fdnOpen; static int MAPPED_STATEMENT_INDEX = 0;// 这是对应上面的args的序号
static int PARAMETER_INDEX = 1;
static int ROWBOUNDS_INDEX = 2;
static int RESULT_HANDLER_INDEX = 3;
@Override
public Object intercept(Invocation invocation) throws Throwable {
if(!fdnOpen){
return invocation.proceed();
}
final Object[] queryArgs = invocation.getArgs();
final MappedStatement mappedStatement = (MappedStatement) queryArgs[MAPPED_STATEMENT_INDEX];
final Object parameter = queryArgs[PARAMETER_INDEX];
final BoundSql boundSql = mappedStatement.getBoundSql(parameter); String sql = boundSql.getSql();
if(sql.contains("xx_pro")){
sql = sql.replace("xx_pro","xx_pro_fdn");
} // 重新new一个查询语句对像
BoundSql newBoundSql = new BoundSql(mappedStatement.getConfiguration(), sql, boundSql.getParameterMappings(), boundSql.getParameterObject());
// 把新的查询放到statement里
MappedStatement newMs = copyFromMappedStatement(mappedStatement, new BoundSqlSqlSource(newBoundSql));
for (ParameterMapping mapping : boundSql.getParameterMappings()) {
String prop = mapping.getProperty();
if (boundSql.hasAdditionalParameter(prop)) {
newBoundSql.setAdditionalParameter(prop, boundSql.getAdditionalParameter(prop));
}
}
queryArgs[MAPPED_STATEMENT_INDEX] = newMs;
return invocation.proceed();
} @Override
public Object plugin(Object target) {
return Plugin.wrap(target, this);
} @Override
public void setProperties(Properties properties) { } private MappedStatement copyFromMappedStatement(MappedStatement ms, SqlSource newSqlSource) {
MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), ms.getId(), newSqlSource, ms.getSqlCommandType());
builder.resource(ms.getResource());
builder.fetchSize(ms.getFetchSize());
builder.statementType(ms.getStatementType());
builder.keyGenerator(ms.getKeyGenerator());
if (ms.getKeyProperties() != null && ms.getKeyProperties().length > 0) {
builder.keyProperty(ms.getKeyProperties()[0]);
}
builder.timeout(ms.getTimeout());
builder.parameterMap(ms.getParameterMap());
builder.resultMaps(ms.getResultMaps());
builder.resultSetType(ms.getResultSetType());
builder.cache(ms.getCache());
builder.flushCacheRequired(ms.isFlushCacheRequired());
builder.useCache(ms.isUseCache());
return builder.build();
} public static class BoundSqlSqlSource implements SqlSource {
private BoundSql boundSql;
public BoundSqlSqlSource(BoundSql boundSql) {
this.boundSql = boundSql;
}
public BoundSql getBoundSql(Object parameterObject) {
return boundSql;
}
}
}
step2:在mybatis的配置文件中加入plugin
<plugins>
<plugin interceptor="xx.interceptor.FDNSqlInterceptor"></plugin>
</plugins>
mybatis自定义插件动态修改sql语句的更多相关文章
- 自定义函数动态执行SQL语句
Oracle 动态SQL有两种写法:用 DBMS_SQL 或 execute immediate,建议使用后者. DDL 和 DML Sql代码 收藏代码 /*** DDL ***/ begin EX ...
- Mybatis之动态构建SQL语句
今天一个新同事问我,我知道如何利用XML的方式来构建动态SQL,可是Mybatis是否能够利用注解完成动态SQL的构建呢?!!答案是肯定的,MyBatis 提供了注解,@InsertProvider, ...
- MyBatis学习 之 二、SQL语句映射文件(2)增删改查、参数、缓存
目录(?)[-] 二SQL语句映射文件2增删改查参数缓存 select insert updatedelete sql parameters 基本类型参数 Java实体类型参数 Map参数 多参数的实 ...
- MyBatis学习 之 二、SQL语句映射文件(1)resultMap
目录(?)[-] 二SQL语句映射文件1resultMap resultMap idresult constructor association联合 使用select实现联合 使用resultMap实 ...
- mybatis 自定义插件的使用
今天看了别人的mybatis的教学视频,自己手写了一个简单的自定义的插件,有些细节记录一下. 先看下mybatis的插件的一些说明: MyBatis 允许你在已映射语句执行过程中的某一点进行拦截调用. ...
- springBoot + mybatis实现执行多条sql语句出错解决方法
在Idea中执行多条sql语句的修改(mybatis默认的是执行sql语句是执行单条,所以要执行多条的时候需要进行配置) 需要在连接字符串中添加上&allowMultiQueries=true ...
- 如何根据实体动态生成sql语句
该文章同时解决了,如何向数据库中添加Null值,以及如何处理“参数化查询未提供参数”的错误.解决方案请看第二段折叠的代码. 背景: 在项目开发的过程中,往往需要根据实体的值来修改sql语句,比如说,有 ...
- Mybatis框架(9)---Mybatis自定义插件生成雪花ID做为表主键项目
Mybatis自定义插件生成雪花ID做为主键项目 先附上项目项目GitHub地址 spring-boot-mybatis-interceptor 有关Mybatis雪花ID主键插件前面写了两篇博客作为 ...
- 模拟Hibernate动态生成SQL语句
这里有一个xml配置文件,也就是Hibernate框架中会用到的POJO和数据库的映射文件 <?xml version="1.0" encoding="utf-8& ...
随机推荐
- FancyBox-经典的jQuery Lightbox插件
在线演示 本地下载 FancyBox 是一款非常优秀的弹窗插件,能够为图片.HTML内容和其它任务的多媒体内容提供优雅的弹出缩放效果.作为是最流行的 Lightbox 插件之一,可以通过 fitToV ...
- Office办公 Winrar如何批量把每个文件夹分别压缩成不同的压缩包
右击很多文件夹,添加到压缩文件,然后在文件中勾选把每个文件放到单独的压缩文件中即可
- 解决 IllegalArgumentException: Could not resolve placeholder in string value "${XXXXXX}"
如题: 导致这一问题的原因:使用了重复的property-placeholder 如一个配置文件中使用了 <context:property-placeholder location=" ...
- 求两个有序数组的中位数或者第k小元素
问题:两个已经排好序的数组,找出两个数组合并后的中位数(如果两个数组的元素数目是偶数,返回上中位数). 设两个数组分别是vec1和vec2,元素数目分别是n1.n2. 算法1:最简单的办法就是把两个数 ...
- TQ2440开发板挂载U盘出现乱码
解决方法:配置内核 make menuconfig File Systems ---> DOS/FAT/NT Filesystems ---> (utf8) D ...
- tempermonkey相关
@match不支持参数和端口配置,瞬间软了
- scpclient使用报错fchmod无法找到问题解决
因为这个函数时linux下专用的,Windows下无法使用,所以会导致提示这个函数不能使用,解决的方法如下: 1. import platform 2. if platform.system() == ...
- 使用迭代器遍历List的时候修改List报ConcurrentModificationException异常原因分析
在使用Iterator来迭代遍历List的时候如果修改该List对象,则会报java.util.ConcurrentModificationException异常,下面看一个例子演示: package ...
- go语言生成uuid
操作系统: CentOS 6.9_x64 go语言版本: 1.8.3 问题描述 golang没有提供生成uuid的接口,但开发中确实需要uuid. 这里把看到的代码记录下,也方便我以后查阅. 解决方案 ...
- 使用VTK与Python实现机械臂三维模型可视化
三维可视化系统的建立依赖于三维图形平台, 如 OpenGL.VTK.OGRE.OSG等, 传统的方法多采用OpenGL进行底层编程,即对其特有的函数进行定量操作, 需要开发人员熟悉相关函数, 从而造成 ...