这期内容当中小编将会给大家带来有关MyBatis中如何使用 plugins插件,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
10多年的朝阳网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。成都全网营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整朝阳建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联从事“朝阳网站设计”,“朝阳网站推广”以来,每个客户项目都认真落实执行。
插件是MyBatis对外开放了四个接口,可以用于自定义扩展。
| 接口 | 可代理方法 | 说明 | |
|---|---|---|---|
| Executor | 执行器,对事务、缓存等提供统一接口 | update | 执行update、insert、delete操作 |
| query | 执行select操作 | ||
| flushStatements | 在commit的时候自动调用,SimpleExecutor、ReuseExecutor、BatchExecutor处理不同 | ||
| commit | 提交事务 | ||
| rollback | 事务回滚 | ||
| getTransaction | 获取事务 | ||
| close | 结束或关闭事务 | ||
| isClosed | 判断事务是否关闭 | ||
| ParameterHandler | 参数处理器,负责为 PreparedStatement 的 sql 语句参数动态赋值 | getParameterObject | 获取参数 |
| setParameters | 设置参数 | ||
| ResultSetHandler | 结果集处理 | handleResultSets | 处理结果集 |
| handleOutputParameters | 处理存储过程出参 | ||
| StatementHandler | 四大组件中最重要的一个对象,负责操作 Statement 对象与数据库进行交流,在工作时还会使用 ParameterHandler 和 ResultSetHandler 对参数进行映射,对结果进行实体类的绑定 | prepare | (BaseSatementHandler)SQL预编译 |
| parameterize | 设置参数 | ||
| batch | 批量处理 | ||
| update | 增删改操作 | ||
| query | 查询操作 |
以上4个接口在MyBatis中的工作流程如下图:

创建自定义插件主要步骤:
编写插件代码实现Interceptor接口,设置要代理的方法
在mybatis-config.xml中注册插件
下边来简单做一个分表的插件,根据主键ID分,实现单数入<表名>表双数入<表名_1>表
/**
* 简单分表,根据传入的主键ID,实现单数入<表名>表双数入<表名_1>表
* @Author: maomao
* @Date: 2021-04-09 17:28
*/
@Intercepts({
@Signature(type = Executor.class, //表示要代理的接口类型
method = "update", //表示要代理接口的对应方法
args = {MappedStatement.class, Object.class} //表示代理方法对应的参数列表,反射时使用(解决方法重载问题)
),
@Signature(type = Executor.class,
method = "query",
args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}
)
})
public class SimpleTableInterceptor implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
Set typeSet = CollectionUtil.newHashSet(SqlCommandType.DELETE,SqlCommandType.INSERT,SqlCommandType.UPDATE);
if (!typeSet.contains(mappedStatement.getSqlCommandType())) {
return invocation.proceed();
}
//获得执行的sql语句
BoundSql boundSql = mappedStatement.getBoundSql(invocation.getArgs()[1]);
//sql语法解析工具,方便获取表名
Statement statement = CCJSqlParserUtil.parse(boundSql.getSql());
//获取参数
Object parameter = boundSql.getParameterObject();
JSON parameterJson = JSONUtil.parse(parameter);
Long id = (Long) parameterJson.getByPath("id");
//使用ID取余数,确定执行表名
Long tableIndex = id % 2;
LoggerUtil.printThread("tableIndex : " + tableIndex);
Table table;
String newSql,newTableName = null;
if(statement instanceof Update){
Update update = (Update) statement;
table = update.getTable();
if(tableIndex == 0){
newTableName = table.getName() + "_1";
}else if(table.getName().lastIndexOf("_1") > 0){
newTableName = table.getName().replace("_1","");
}
if(StrUtil.isNotEmpty(newTableName)){
table.setName(newTableName);
}
newSql = update.toString();
}else{
Insert insert = (Insert) statement;
table = insert.getTable();
if(tableIndex == 0){
newTableName = table.getName() + "_1";
}else if(table.getName().lastIndexOf("_1") > 0){
newTableName = table.getName().replace("_1","");
}
if(StrUtil.isNotEmpty(newTableName)){
table.setName(newTableName);
}
newSql = insert.toString();
}
LoggerUtil.printThread("新sql : " + newSql);
// 自定义sqlSource
SqlSource sqlSource = new StaticSqlSource(mappedStatement.getConfiguration(), newSql, boundSql.getParameterMappings());
// 修改原来的sqlSource
Field field = MappedStatement.class.getDeclaredField("sqlSource");
field.setAccessible(true);
field.set(mappedStatement, sqlSource);
return invocation.proceed();
}
} 在mybatis-config.xml中注册插件
上边就可以简单的实现一个分表的逻辑,不需要修改任何业务代码。是不是非常方便。
那MyBatis是如何实现插件功能的呢?如果有多个插件它又是如何执行的呢?
插件的实现使用了动态代理、反射和责任链的方式实现。
下边我将抽出MyBatis的插件核心代码。
简单抽出MyBatis代理链核心代码地址
核心类说明: 
上述就是小编为大家分享的MyBatis中如何使用 plugins插件了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。