Mybatis分页查询

Mybatis分页查询

其实我们都知道Mybatis是对JDBC进行了轻量级的封装框架,在我们使用Mybatis对数据库进行CRUD等操作的时候,最重要的还是通过SQL语句来进行相应的操作,那么对Mybatis分页查询又该怎么来实现呢?
其实Mybatis的分页查询同样也是使用:(select * from 表名 limit number,number;)来进行分查询。下面我简单的介绍两种方式分页查询:

Mybatis分页查询方式一

通过编写SQL语句进行分页查询
映射文件:

<select id="limitUser" parameterType="int" resultType="com.lc.mybatis.po.User">
        Select * from User limit #{arg0},#{arg1}
</select>

Mapper接口:
这里关于@Param注解是传递多参数的映射方式;关于Mybatis传递多参数可以参考:http://www.cnblogs.com/mingyue1818/p/3714162.html

public List<User> limitUser(@Param("arg0") int start,@Param("arg1") int limit);

测试类:

    @Test
    public void testLimitUser() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //获取UserMapper的代理类
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = userMapper.limitUser(0, 5);
        System.out.println(users.size());
        sqlSession.close();
    }

Mybatis分页查询方式二

通过Mybatis插件进行分页查询:
我这里使用的是PageHelper插件,该插件是GitHub上的一个开源项目;该插件的方式下载地址:https://www.versioneye.com/java/com.github.pagehelper:pagehelper/3.3.2;
PageHelper插件的原理
通过前面几篇关于Mybatis的介绍,可以知道Mybatis的执行流程和原理:SqlSessionFactory的任务是创建SqlSession(SqlSession是用户用来操作数据库的接口),其实真正操作数据库并不是SqlSession去操作数据库,而是在SqlSession中封装了一个executor执行器来真真的操作数据库,而在MappedStatement中封装了操作数据库中的sql语句以及输入输出。因此,PageHelper插件就是在executor执行sql语句的时候通过Mybatsi的Intercept拦截器拦截对MappedStatement中的Sql语句进行修改(增加可以分页查询的功能),然后再把sql语句返回给MappedStatement。这样当executor执行MappedStatement中的sql语句就具有分页查询的功能。
执行过程大致如下图:
这里写图片描述
使用PageHelper插件实现Mybatis分页查询
第一步:添加jar包到工程中去(我这里使用的是maven工程)

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>3.3.2</version>
</dependency>

第二步:修改SqlMapConfig.xml配置文件(添加该插件的使用)

<!-- 配置分页插件 -->
<plugins>
    <plugin interceptor="com.github.pagehelper.PageHelper">
        <!-- 指定使用的数据库是什么 -->
        <property name="dialect" value="mysql"/>
    </plugin>
</plugins>

第三步:使用分页查询插件测试分页查询
这里通过Mybatis的逆向工程生成了表对应的pojo和一些简单的Mapper接口和映射文件。

public class TestPageHelper {

    @Test
    public void testPageHelper() throws Exception {
        //1、获得mapper代理对象
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml");
        TbItemMapper itemMapper = applicationContext.getBean(TbItemMapper.class);
        //2、设置分页
        PageHelper.startPage(1, 30);
        //3、执行查询
        TbItemExample example = new TbItemExample();
        List<TbItem> list = itemMapper.selectByExample(example);
        //4、取分页后结果
        PageInfo<TbItem> pageInfo = new PageInfo<>(list);
        //总记录数(相当于执行了select count(*) from 表名)
        long total = pageInfo.getTotal();
        System.out.println("total:" + total);
        //总页数
        int pages = pageInfo.getPages();
        System.out.println("pages:" + pages);
        //每页显示的数量
        int pageSize = pageInfo.getPageSize();
        System.out.println("pageSize:" + pageSize);
    }
}

方式一和方式二的不同:

  • 方式一需要手动去单独的写分页查询sql语句,而方式二只需要使用逆向工程生成的代码,在执行sql语句前设置分页查询的支持就可以了。
  • 方式一需要单独的编写查询数据总量的sql语句,而方式二中PageHelper已经为我们做了,可以通过PageInfo去获取许多分页信息。

猜你喜欢

转载自blog.csdn.net/ITITII/article/details/80217446