加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

mybatis插件开发小例子

发布时间:2020-12-14 18:02:16 所属栏目:大数据 来源:网络整理
导读:接上一节,我们继续实现小例子,比如说将查询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.Intercep

接上一节,我们继续实现小例子,比如说将查询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]

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读