//插件扩展实现类:
org.apache.ibatis.session.Configuration
//具体方法:
protected ExecutorType defaultExecutorType = ExecutorType.SIMPLE;
public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
executorType = executorType == null ? defaultExecutorType : executorType;
executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
Executor executor;
if (ExecutorType.BATCH == executorType) {
executor = new BatchExecutor(this, transaction);
} else if (ExecutorType.REUSE == executorType) {
executor = new ReuseExecutor(this, transaction);
} else {
executor = new SimpleExecutor(this, transaction);
}
if (cacheEnabled) {
executor = new CachingExecutor(executor);
}
executor = (Executor) interceptorChain.pluginAll(executor);
return executor;
}
Executor接口:
public interface Executor {
ResultHandler NO_RESULT_HANDLER = null;
int update(MappedStatement ms, Object parameter) throws SQLException;
List query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException;
List<BatchResult> flushStatements() throws SQLException;
void commit(boolean required) throws SQLException;
void rollback(boolean required) throws SQLException;
CacheKey createCacheKey(MappedStatement ms, Object parameterObject, RowBounds rowBounds);
boolean isCached(MappedStatement ms, CacheKey key);
void clearLocalCache();
void deferLoad(MappedStatement ms, MetaObject resultObject, String property, CacheKey key);
Transaction getTransaction();
void close(boolean forceRollback);
boolean isClosed();
}
插件继承和扩展:
implements Interceptor
@Intercepts( { @Signature(type = Executor.class, method = "query", args = {
MappedStatement.class, Object.class, RowBounds.class,
ResultHandler.class }) })
具体实例:
package com.xuanwu.sms.smstask.webmanager.common.crud.plugins;
import java.lang.reflect.Field;
import java.util.Properties;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
@Intercepts( { @Signature(type = Executor.class, method = "query", args = {
MappedStatement.class, Object.class, RowBounds.class,
ResultHandler.class }) })
public class PaginationInterceptor implements Interceptor {
public Object intercept(Invocation invocation) throws Throwable {
MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
Object parameter = invocation.getArgs()[1];
mappedStatement.getSqlSource().getBoundSql(parameter);
BoundSql boundSql = mappedStatement.getBoundSql(parameter);
if (boundSql == null || boundSql.getSql() == null || "".equals(boundSql.getSql())) {
return null;
}
String newSql = "select * from (" + boundSql.getSql() + ") t";
Class<?> cla =boundSql.getClass();
Field field = cla.getDeclaredField("sql");
field.setAccessible(true);
field.set(boundSql, newSql);
String test = boundSql.getSql();
return invocation.proceed();
}
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}
public void setProperties(Properties properties) {
String dialectClass = properties.getProperty("dialectClass");
try {
Object dialect = (Object) Class.forName(dialectClass).newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
分享到:
相关推荐
这个是mybatis分页插件的源码分享,支持mysql和oracl
idea的mybatisX插件——MyBatisX-1.3.9.jar
Mybatis 脱敏插件 最近在研究 Mybatis 的插件,所以考虑能不能在 ORM 中搞一搞脱敏,所以就尝试了一下,这里分享一下思路。借此也分享一下 Mybatis 插件开发的思路。 2.1 Mybatis 插件接口 Mybatis 中使用...
06实现mybatis分页插件demo06实现mybatis分页插件demo06实现mybatis分页插件demo06实现mybatis分页插件demo06实现mybatis分页插件demo06实现mybatis分页插件demo06实现mybatis分页插件demo06实现mybatis分页插件demo...
mybatis自定义插件源码,实现了在插入数据记录时自动添加自定义字段值,sql拦截和改造。
IDEA2021版离线可装MyBatisX 插件
mybatis 3.x源码深度解析与最佳实践.html
《Spring+MyBatis企业应用实战》适合有较好的Java 编程基础,JSP、Servlet、JDBC 基础,Spring 框架基础的读者,尤其适合于对Spring MVC 和MyBatis 了解不够深入,或对Spring MVC+MyBatis 整合开发不太熟悉的开发...
mybatis核心源码mybatis核心源码mybatis核心源码mybatis核心源码mybatis核心源码mybatis核心源码mybatis核心源码mybatis核心源码mybatis核心源码mybatis核心源码mybatis核心源码mybatis核心源码mybatis核心源码...
IDEA离线安装MybatisX插件(MybatisX-1.1.2至1.4.17),IDEA版本:IntelliJ IDEA Ultimate 2021.1.3 MybatisX 插件特点: mapper和xml可以来回跳转 mybatis.xml,mapper.xml 提示 mapper 和 xml 支持类似 jpa 的自动...
mybatis 延迟加载样例,附uml图说明
MyBatis官方包和源码包.zipmybatis-3.3 jar完整包和源码,官方下载 mybatis-3.2.3 jar包和源码,所有jar包文件,完整版,最近整理方便大家下载
MybatisX 是一款基于 IDEA 的快速开发插件,为效率而生。 安装方法:打开 IDEA,进入 File -> Settings -> Plugins -> Browse Repositories,输入 mybatisx 搜索并安装。 无法打开的,下载插件安装
Mybatis插件开源架构源码2021.pdf
mybatis分页插件支持查询~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
springMVC+mybatis+mysql项目源码
项目说明:本音乐网站的客户端和管理端使用 Vue 框架来实现,服务端使用 Spring Boot + MyBatis 来实现,数据库...SpringBoot + Vue + MyBatis 音乐网站项目源码 SpringBoot + Vue + MyBatis 音乐网站项目源码 ......
mybatis-3.0.4源码,mybatis-3.0.4源码mybatis-3.0.4,源码,mybatis-3.0.4源码
eclipse的mybatis插件(源码) 详细操作及效果,请参看我的博文: http://blog.csdn.net/fu9958/article/details/7521681 如修改并在互联网上传播,请链接该博文。