尝试使用了一下mybati的插件功能
写了一个基础类备以后使用
import java.sql.Connection;
import java.util.Properties;
import org.apache.ibatis.executor.statement.StatementHandler;
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.reflection.MetaObject;
import org.apache.ibatis.reflection.factory.DefaultObjectFactory;
import org.apache.ibatis.reflection.factory.ObjectFactory;
import org.apache.ibatis.reflection.wrapper.DefaultObjectWrapperFactory;
import org.apache.ibatis.reflection.wrapper.ObjectWrapperFactory;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.RowBounds;
@Intercepts({
@Signature(type = StatementHandler.class, method = "prepare", args = { Connection.class })
})
public abstract class AbstractMyBatisPlugin implements Interceptor {
private static final ObjectFactory OBJECT_FACTORY = new DefaultObjectFactory();
private static final ObjectWrapperFactory OBJECT_WRAPPER_FACTORY = new DefaultObjectWrapperFactory();
private Properties properties;
public abstract void preIntercept(MetaObject metaStatementHandler, Configuration configuration, MappedStatement mappedStatement, BoundSql boundSql, RowBounds rowBounds);
@Override
public final Object intercept(Invocation invocation) throws Throwable {
StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
MetaObject metaStatementHandler = MetaObject.forObject(statementHandler, OBJECT_FACTORY, OBJECT_WRAPPER_FACTORY);
// 分离代理对象链(由于目标类可能被多个拦截器拦截,从而形成多次代理,通过下面的两次循环可以分离出最原始的的目标类)
while (metaStatementHandler.hasGetter("h")) {
Object object = metaStatementHandler.getValue("h");
metaStatementHandler = MetaObject.forObject(object, OBJECT_FACTORY, OBJECT_WRAPPER_FACTORY);
}
// 分离最后一个代理对象的目标类
while (metaStatementHandler.hasGetter("target")) {
Object object = metaStatementHandler.getValue("target");
metaStatementHandler = MetaObject.forObject(object, OBJECT_FACTORY, OBJECT_WRAPPER_FACTORY);
}
Configuration configuration = (Configuration) metaStatementHandler.getValue("delegate.configuration");
MappedStatement mappedStatement = (MappedStatement) metaStatementHandler.getValue("delegate.mappedStatement");
BoundSql boundSql = (BoundSql) metaStatementHandler.getValue("delegate.boundSql");
RowBounds rowBounds = (RowBounds) metaStatementHandler.getValue("delegate.rowBounds");
preIntercept(metaStatementHandler, configuration, mappedStatement, boundSql, rowBounds);
return invocation.proceed();
}
@Override
public final Object plugin(Object target) {
if (target instanceof StatementHandler) {
return Plugin.wrap(target, this);
} else {
return target;
}
}
public Properties getProperties() {
return properties;
}
@Override
public void setProperties(Properties properties) {
this.properties = properties;
}
}
分享到:
相关推荐
mybatis plugin插件是一款兼容 IntelliJ IDEA 和Android Studio编程开发工具的插件,这个工具目前更新到了v3.42最新版本,...mybatis plugin插件是从头开始重新编写的,使其更稳定,带来许多新功能。一起来体验下吧!
MyBatis plugin 3.155 (最新版) 修正了大量Bug 简化了配置文件编写
Mybatis 中使用插件,需要实现接口org.apache.ibatis.plugin.Interceptor,如下所示: public interface Interceptor { Object intercept(Invocation invocation) throws Throwable; default Object plugin...
MyBatis 目录(?)[-] mybatis实战教程mybatis in action之一开发环境搭建 mybatis实战教程mybatis in action之二以接口的方式编程 mybatis实战教程mybatis in action之三实现数据的增删改查 mybatis实战教程mybatis ...
本工具是基于mybatis的插件机制编写的一套敏感数据加解密以及数据脱敏工具。 在使用时通过注解指定一个字段是需要加密的字段,该插件会在存储时自动加密存储。 而查询时会自动解密出明文在程序内部使用。 在使用时也...
MyBatisCodeHelper-Pro是IDEA下的一个插件,类似于mybatis plugin,但可以进行和(ji)谐(huo)使用 : MyBatisCodeHelper-Pro是IDEA下的一个插件,功能类似mybatis plugin。 MybatisCodeHelperMarketPlaceNew-2.9 IDEA...
Mybatis SQL开发神器MybatisBoost,为Mybatis带很多官方没有的高级特性,包含 , ,, ,,, ,流式查询(开发中...)等功能,使用MybatisBoost来提高开发效率,轻松编写SQL代码! 使用MybatisBoost的最低要求: ...
MyBatis 2Way SQL脚本插件我试图在MyBatis上提供 。 以下是语法高亮显示(IntelliJ + Kotlin)。 这不是现实的方法,因为它完全合并了的jar。 如果要使用2-Way SQL ,则最好使用Doma而不是MyBatis。
mybatis-generator-gui-extension是一个为MybatisGenerator编写的图形化界面,为实体/Example/Mapper提供了丰富的扩展。 功能清单 文件合并:在重新生成代码时,将保留没有添加@mbg.generated注释的字段/方法/SQL...
Free Mybatis Plugin:可以直接跳转到mapper.xml文件中 Key Promoter X:快捷键提醒 GsonFormat:把json格式的内容转成Object。 CodeGlance:代码编辑区迷你缩放插件,全局预览代码 CamelCase:变量等命名风格转换...
后台基于SpringBoot框架,数据库访问同时集成了Jpa与MyBatis-Plus,即可自动生成数据库表结构,又可灵活编写sql。前端基于vue-next-admin/vben框架,引入了bpmn.js工作流、VForm可视化表单。默认前端(Element-Plus...
本项目依赖于Lombok插件,请在IDE中安装Lombok Plugin ,了解Lombok请查看: 首要条件 特色 基于Spring Boot 1.5.9,内嵌Jetty 提供双重异常捕获,查看到Json,跨域访问等功能 提供Maven配置文件和Spring配置文件...
集成 mybatis + druid 做数据操作 继承redis, 使用Jedis操作redis数据, 封装了统一的缓存key. 第2章-实现用户登录以及分布式session功能 学习了自己通过 cookie 实现分布式session, 不使用spring boot默认提供的 ...
Maven定义了软件开发的整套流程体系,并进行了封装,开发人员只需要指定项目的构建流程,无需针对每个流程编写自己的构建脚本。 2、依赖管理。除了项目构建,Maven最核心的功能是软件包的依赖管理,能够自动分析...
云生活超市管理系统(完整的后台管理系统)React... 服务器端我是用springboot+mybatis编写的,仓库地址是:作者想说:其实是想用React-native做一个超市的app,取名云生活超市(名字很难听各位轻吐槽),既然有app,