今天看了别人的mybatis的教学视频,自己手写了一个简单的自定义的插件,有些细节记录一下。
先看下mybatis的插件的一些说明:
MyBatis 允许你在已映射语句执行过程中的某一点进行拦截调用。默认情况下,MyBatis 允许使用插件来拦截的方法调用包括:
Executor (update, query, flushStatements, commit, rollback, getTransaction, close, isClosed) --执行sql
ParameterHandler (getParameterObject, setParameters) --获取、设置参数
ResultSetHandler (handleResultSets, handleOutputParameters) --处理结果集
StatementHandler (prepare, parameterize, batch, update, query) --记录sql
这里需要注意的是,这4个类型是固定的,里面的方法也是固定的,不能再被改变的,具体的信息,可以相关的类(Executor.class、ParameterHandler .class、ResultSetHandler .class、StatementHandler .class)查看。
-----------------------------------------------------------------------------------------------------
先定义一个插件拦截器,代码如下:
package com.drafire.testall.interceptor; import org.apache.ibatis.executor.Executor; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.plugin.*; import java.util.Properties; /** * mybatis 自定义插件 */ @Intercepts(value = {@Signature( type= Executor.class, //这里对应4个类 method = "update", //这里对应4个类里面的参数 args = {MappedStatement.class,Object.class})}) //这里的参数类型,是对应4个类中的各种方法的参数。如果方法没有参数,这里直接写{}就可以了 public class DrafirePlugin implements Interceptor { @Override public Object intercept(Invocation invocation) throws Throwable { System.out.println("mybatis插件打印了乐乐"); return invocation.proceed(); } @Override public Object plugin(Object target) { return Plugin.wrap(target, this); } @Override public void setProperties(Properties properties) { } }
配置mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typeHandlers> <typeHandler handler="com.drafire.testall.handler.DrafireStringHandler"></typeHandler> </typeHandlers> <plugins> <plugin interceptor="com.drafire.testall.interceptor.DrafirePlugin"> </plugin> </plugins> <environments default="development"> <environment id="sell"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${ds.sell.driverClassName}"/> <property name="url" value="${ds.sell.url}"/> <property name="username" value="${ds.sell.username}"/> <property name="password" value="${ds.sell.password}"/> </dataSource> </environment> <environment id="bank"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="${ds.bank.driverClassName}"></property> <property name="url" value="${ds.bank.url}"></property> <property name="username" value="${ds.bank.username}"></property> <property name="password" value="${ds.bank.password}"></property> </dataSource> </environment> </environments> <mappers> <!--<mapper resource="org/mybatis/example/BlogMapper.xml"/>--> </mappers> </configuration>
测试代码如下,测试
package com.drafire.testall.Sevice; import com.drafire.testall.model.User; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @SpringBootTest @RunWith(SpringRunner.class) public class UserServiceTest { @Autowired private UserService userService; //@Test public void addUser() { User user=new User(); user.setId(1); user.setAmount(110L); user.setName("李四"); userService.add(user); } @Test public void updateUser(){ User user=new User(); user.setId(1); user.setAmount(50L); user.setName("王五123"); userService.update(user); } }
通过