接上一节,我们继续实现小例子,比如说将查询id=1的员工改为查询id=3的员工:
MyFirstPlugin.java
package com.gong.mybatis.dao; import java.util.Properties; import org.apache.ibatis.executor.statement.StatementHandler; 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.SystemMetaObject; //完成插件签名,用于拦截哪个对象的哪个方法 @Intercepts({ @Signature(type=StatementHandler.class,method="parameterize",args=java.sql.Statement.class) }) public class MyFirstPlugin implements Interceptor { /** * intercept:拦截 * */ @Override public Object intercept(Invocation invocation) throws Throwable { // TODO Auto-generated method stub System.out.println("myfirstplugin...intercept:"+invocation.getMethod()); Object target = invocation.getTarget(); System.out.println("当前拦截到的对象:"+target); MetaObject metaObject = SystemMetaObject.forObject(target); Object value = metaObject.getValue("parameterHandler.parameterObject"); System.out.println("sql语句中的参数是:"+value); metaObject.setValue("parameterHandler.parameterObject", 3); //执行目标方法 Object proceed = invocation.proceed(); //返回执行后的返回值 return proceed; } //包装目标对象,为目标对象创建一个代理对象 @Override public Object plugin(Object target) { System.out.println("-->myfirstplugin...plugin,将要包装的对象:"+target); // TODO Auto-generated method stub Object wrap = Plugin.wrap(target, this); //返回为当前target创建的动态代理 return wrap; } //将插件注册时的property属性设置进来 @Override public void setProperties(Properties properties) { // TODO Auto-generated method stub System.out.println("插件配置的信息:"+properties); } }
注意橙色的代码,进行测试:
package com.gong.mybatis.test; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Test; import com.gong.mybatis.bean.Department; import com.gong.mybatis.bean.Employee; import com.gong.mybatis.dao.EmployeeMapper; import com.gong.mybatis.mapper.EmployeeMapperDynamicSql; public class TestMybatis5 { public SqlSessionFactory getSqlSessionFactory() throws IOException { String resource = "mybatis-config.xml"; InputStream is = Resources.getResourceAsStream(resource); return new SqlSessionFactoryBuilder().build(is); } @Test public void test() throws IOException { SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); SqlSession openSession = sqlSessionFactory.openSession(); try { EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class); Employee em = mapper.getEmpById(1); System.out.println(em); } finally { // TODO: handle finally clause openSession.close(); } } }
测试结果:
插件配置的信息:{password=123456, username=root} -->myfirstplugin...plugin,将要包装的对象:org.apache.ibatis.executor.CachingExecutor@23faf8f2 -->myfirstplugin...plugin,将要包装的对象:org.apache.ibatis.scripting.defaults.DefaultParameterHandler@1563da5 -->myfirstplugin...plugin,将要包装的对象:org.apache.ibatis.executor.resultset.DefaultResultSetHandler@34c4973 -->myfirstplugin...plugin,将要包装的对象:org.apache.ibatis.executor.statement.RoutingStatementHandler@7a765367 DEBUG 01-23 13:31:26,758 ==> Preparing: select id,last_name lastName,email,gender from tbl_employee where id = ? (BaseJdbcLogger.java:145) myfirstplugin...intercept:public abstract void org.apache.ibatis.executor.statement.StatementHandler.parameterize(java.sql.Statement) throws java.sql.SQLException 当前拦截到的对象:org.apache.ibatis.executor.statement.RoutingStatementHandler@7a765367 sql语句中的参数是:1 DEBUG 01-23 13:31:26,837 ==> Parameters: 3(Integer) (BaseJdbcLogger.java:145) DEBUG 01-23 13:31:26,866 <== Total: 1 (BaseJdbcLogger.java:145) Employee [id=3, lastName=小红, gender=0, email=xiaohong@qq.com, dept=null]