学习mybaits不得不研究系列:Mybatis三剑客之分页插件Mybatis-PageHelper,以及自定义mybatis拦截器

Mybatis拦截器

mybatis的分页插件底层其实使用的就是mybatis的拦截器,mybatis的拦截器可以拦截到增删改查的方法,然后在执行这些方法之前做一些操作,类似与aop面向切面编程的思想,下面我们自定义一个拦截器

1.1 自定义拦截器实现步骤
  • 定义一个类实现Interceptor接口
  • 在mybatis的核心配置文件中配置自定义拦截器
    对于拦截器Mybatis为我们提供了一个Interceptor接口,通过实现该接口就可以定义我们自己的拦截器。
2.1 自定义拦截器具体实现

1、自定义类实现Interceptor接口,并复写抽象方法

@Intercepts({
        //注意update是拦截增删改操的,type的类型有很多,这里选的是Executor,我们可以
        //查看Executor接口,method的就是方法名。args参数的class类型
        @Signature(type = Executor.class, //选定签名
                method = "update",
                args = {MappedStatement.class,Object.class}
        ),
        //query拦截的是查询操作
        @Signature(type = Executor.class,
                method = "query",
                args = {MappedStatement.class,Object.class, RowBounds.class, ResultHandler.class}
        )
})
public class MyInterceptor implements Interceptor {
    /**
     * 这个是拦截器的主要方法:对拦截到的方法可以先进行其他的逻辑操作然后再执行当前方法
     */
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        System.out.println("执行之前进行操作。。。。");
        //执行当前拦截到的方法
        Object result = invocation.proceed();
        System.out.println("执行之后进行操作。。。。");
        return result;
    }
    /**
     * plugin判断当前操作是否足以拦截:里面的代码是固定写法:Plugin.wrap(o,this)
     * this是当前对象:我们通过类上的注解来判断对那些操作进行拦截
     */
    @Override
    public Object plugin(Object o) {
        return Plugin.wrap(o,this);
    }
    /**
     * setProperties用来接收拦截器配置的参数
     * @param properties
     */
    @Override
    public void setProperties(Properties properties) {
        String prop = properties.getProperty("prop");
        System.out.println(prop);
    }
}

2、在mybatis的核心配置文件:mybatis_config.xml配置

<plugins>
    <!--配置自定义的拦截器-->
    <plugin interceptor="cn.lqq.Interceptor.MyInterceptor">
        <property name="prop" value="mysql"/>
        <!--<property name="prop2" value="prop2"/>-->
    </plugin>
</plugins>

3、测试:上面对crud的操作进行了拦截,现在在执行这些方法会通过自定义拦截器

@Test
public void testFindOne()throws Exception{
    SqlSession session = MybatisUtil.openSession();
    EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
    Employee employee = mapper.selectByPrimaryKey(75L);
    System.out.println(employee);
}

@Test
public void testSave()throws Exception{
    SqlSession session = MybatisUtil.openSession();
    EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
    Employee employee = new Employee();
    employee.setName("赵敏");
    employee.setAge(18);
    employee.setSex(true);
    mapper.insert(employee);
    //增删改一定不能忘了提交事务
    session.commit();
}

MyBatis-PageHelper分页插件

2.1MyBatis的分页插件

是一个通用的分页插件,使用这个插件可以完成分页的功能,其实底层的实现原理是Mybatis的拦截器,所以使用的时候是需要配置一个分页的拦截器

2.2mybatis分页插件的使用步骤
  • 在pom.xml中导入mybatis-pagehelper的分页插件
  • 在mybatis的核心配置文件中配置分页拦截器
  • 在查询的时候使用分页对象开启分页
2.3mybatis分页的代码实现

1、pom.xml

 <!--mybaits的分页插件的包-->
<dependency>
	 <groupId>com.github.pagehelper</groupId>
	 <artifactId>pagehelper</artifactId>
	 <version>5.0.0</version>
</dependency>

2、mybatis的核心配置文件:mybatis_config.xml
注意:如果使用了spring,这个配置文件就可以不用写,直接在spring的配置文件中配置mybatis的分页插件即可
mybatis_config.xml

<plugins>
    <!--配置mybatis分页插件的拦截器-->
    <plugin interceptor="com.github.pagehelper.PageInterceptor">
    </plugin>
</plugins>

3、测试,在查询的时候开启分页

@Test
public void testselectAll()throws Exception{
    //开启分页:pageNum表示页数,pageSize表示没有数据
    PageHelper.startPage(1,10);
    SqlSession session = MybatisUtil.openSession();
    EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
    List<Employee> employees = mapper.selectAll();
    //注意使用分页插件以后,employees实际上是一个page对象,page对象实现了ArrayList类
    Page page = (Page)employees;
    System.out.println(page.getTotal());
    System.out.println(page.getPageSize());
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值