mybatis插件开发小例子

接上一节,我们继续实现小例子,比如说将查询id=1的员工改为查询id=3的员工:

MyFirstPlugin.java

package com.gong.mybatis.dao;

import java.util.Properties;

 org.apache.ibatis.executor.statement.StatementHandler;
 org.apache.ibatis.plugin.Interceptor;
 org.apache.ibatis.plugin.Intercepts;
 org.apache.ibatis.plugin.Invocation;
 org.apache.ibatis.plugin.Plugin;
 org.apache.ibatis.plugin.Signature;
 org.apache.ibatis.reflection.MetaObject;
 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创建的动态代理
         wrap;
    }
    
    将插件注册时的property属性设置进来
void setProperties(Properties properties) {
         TODO Auto-generated method stub
        System.out.println("插件配置的信息:"+properties);
    }

}

注意橙色的代码,进行测试:

 com.gong.mybatis.test;

 java.io.IOException;
 java.io.InputStream;
 java.util.ArrayList;
 java.util.Arrays;
 java.util.HashMap;
 java.util.List;
 java.util.Map;

 org.apache.ibatis.io.Resources;
 org.apache.ibatis.session.SqlSession;
 org.apache.ibatis.session.SqlSessionFactory;
 org.apache.ibatis.session.SqlSessionFactoryBuilder;
 org.junit.Test;

 com.gong.mybatis.bean.Department;
 com.gong.mybatis.bean.Employee;
 com.gong.mybatis.dao.EmployeeMapper;
 com.gong.mybatis.mapper.EmployeeMapperDynamicSql;

 TestMybatis5 {
    
    public SqlSessionFactory getSqlSessionFactory()  IOException {
        String resource = "mybatis-config.xml";
        InputStream is = Resources.getResourceAsStream(resource);
        return new SqlSessionFactoryBuilder().build(is);
    }

    @Test
    void test()  IOException {
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        SqlSession openSession = sqlSessionFactory.openSession();
        
        try {
            EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.);
            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=小红,1)">gender=0,1)">email=xiaohong@qq.com,1)">dept=null]

相关文章

1.pom.xml引入依赖 &lt;dependency&gt; &lt;gro...
&lt;?xml version=&quot;1.0&quot; encoding=&a...
准备工作 ① 创建数据库&amp;数据表 ## 创建数据库 CREA...
MyBatis逆向工程是指根据数据库表结构自动生成对应的实体类、...
MyBatis获取参数值的两种方式:${}和#{} ${}的本质就是字符串...
resultMap作用是处理数据表中字段与java实体类中属性的映射关...