今天看了別人的mybatis的教學視頻,本身手寫了一個簡單的自定義的插件,有些細節記錄一下。java
先看下mybatis的插件的一些說明:spring
MyBatis 容許你在已映射語句執行過程當中的某一點進行攔截調用。默認狀況下,MyBatis 容許使用插件來攔截的方法調用包括:sql
Executor (update, query, flushStatements, commit, rollback, getTransaction, close, isClosed) --執行sqlapache
ParameterHandler (getParameterObject, setParameters) --獲取、設置參數mybatis
ResultSetHandler (handleResultSets, handleOutputParameters) --處理結果集app
StatementHandler (prepare, parameterize, batch, update, query) --記錄sqlide
這裏須要注意的是,這4個類型是固定的,裏面的方法也是固定的,不能再被改變的,具體的信息,能夠相關的類(Executor.class、ParameterHandler .class、ResultSetHandler .class、StatementHandler .class)查看。測試
-----------------------------------------------------------------------------------------------------this
先定義一個插件攔截器,代碼以下:url
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); } }
經過