1.插件执行原理
一、demo
1.测试类
@Test public void test1() { String resource = "mybatis-config.xml"; InputStream inputStream; try { inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(); sqlSession.clearCache(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); User selectUserById = mapper.selectUser(12); System.out.println(selectUserById); sqlSession.commit(); // 释放资源 sqlSession.close(); } catch (Exception e) { e.printStackTrace(); } }
2.plugin
对封装参数进行拦截
package plugin; 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 ParamPlugin implements Interceptor { @Override public Object intercept(Invocation invocation) throws Throwable { // TODO Auto-generated method stub System.out.println("interceptor开始:"); Object target = invocation.getTarget(); System.out.println("当前拦截目标对象:"+target); //拿到:StatementHandler==>parameterHandler==>parameterObject //拿到target的元数据 MetaObject metaObject = SystemMetaObject.forObject(target); System.out.println(metaObject); Object value = metaObject.getValue("parameterHandler.parameterObject"); System.out.println("sql语句的参数是:"+value); //修改sql语句的参数 metaObject.setValue("parameterHandler.parameterObject", 14); //执行目标方法并返回 return invocation.proceed(); } @Override public Object plugin(Object target) { // TODO Auto-generated method stub return Plugin.wrap(target, this); } @Override public void setProperties(Properties properties) { // TODO Auto-generated method stub } }
3.
<plugins> <plugin interceptor="plugin.ParamPlugin"></plugin> </plugins>
二、mybatis分页基于plugin实现原理
1.配置plugin
<plugin interceptor="com.github.pagehelper.PageInterceptor"> <!-- config params as the following --> <!-- <property name="param1" value="value1"/> --> </plugin>
2.使用插件分页
String resource = "mybatis-config.xml"; InputStream inputStream; try { inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); // Page<Object> page = PageHelper.startPage(2,3); // List<User> users = mapper.selectAllUser(); // for (User user: users) { // System.out.println(user.toString()); // } // System.out.println("当前页码:"+page.getPageNum()); // System.out.println("总记录数:"+page.getTotal()); // System.out.println("每页记录数:"+page.getPageSize()); // System.out.println("总页码数:"+page.getPages()); PageHelper.startPage(1, 10); List<User> list = mapper.selectAllUser(); //用PageInfo对结果进行包装 PageInfo page = new PageInfo(list,5); //测试PageInfo全部属性 //PageInfo包含了非常全面的分页属性 // assertEquals(1, page.getPageNum()); // assertEquals(10, page.getPageSize()); // assertEquals(1, page.getStartRow()); // assertEquals(10, page.getEndRow()); // assertEquals(183, page.getTotal()); // assertEquals(19, page.getPages()); // assertEquals(1, page.getFirstPage()); // assertEquals(8, page.getLastPage()); // assertEquals(true, page.isFirstPage()); // assertEquals(false, page.isLastPage()); // assertEquals(false, page.isHasPreviousPage()); // assertEquals(true, page.isHasNextPage()); sqlSession.commit(); // 释放资源 sqlSession.close(); } catch (Exception e) { e.printStackTrace(); } }