美文网首页
Mybatis分页插件PageHelper

Mybatis分页插件PageHelper

作者: Apple_Boy | 来源:发表于2019-02-27 15:56 被阅读0次

对于使用Mybatis时,最头痛的就是写分页,需要先写一个查询count的select语句,然后再写一个真正分页查询的语句,当查询条件多了之后,会发现真不想花双倍的时间写count和select,

可以发现,重复的代码太多,虽然说复制粘贴简单的很,但是文件的长度在成倍的增加,以后翻阅代码的时候头都能大了。

于是希望只写一个select语句,count由插件根据select语句自动完成。找啊找啊,发现PageHelperhttps://github.com/pagehelper/Mybatis-PageHelper符合要求,于是就简单的写了一个测试项目

插件的环境引入:

1.pom.xml:

<dependency>

  <groupId>com.github.pagehelper</groupId>

    <artifactId>pagehelper</artifactId>

    <version>4.1.4</version>

</dependency>

2.在mybatis的配置文件中配置拦截(也可以在spring配置文件中配置)

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE configuration

        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

        "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>

  <plugins>

      <plugin interceptor="com.github.pagehelper.PageHelper">

          <!--设置数据可类型Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->

          <property name="dialect" value="mysql"/>

      </plugin>

  </plugins>

</configuration>

在spring配置文件中配置

<!--配置SqlSessionFactory对象 -->

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

        <property name="dataSource" ref="dataSource" />

        <property name="configLocation" value="classpath:mybatis-config.xml" />

        <property name="typeAliasesPackage" value="com.aoChine.model.entity" />

        <property name="mapperLocations" value="classpath:mapper/*.xml" />

        <!-- 配置mybatis分页插件PageHelper -->

        <property name="plugins">

            <array>

                <bean class="com.github.pagehelper.PageInterceptor">

                    <property name="properties">

                        <!-- 什么都不配,使用默认的配置 -->

                        <value></value>

                    </property>

                </bean>

            </array>

        </property>

    </bean>

使用:

@RequestMapping(value = "/list")

    public String jumpJsp(Map<String, Object> result){

        PageHelper.startPage(3 , 3);

        List<Person> personList = personService.findPerson();

        //得到分页的结果对象

        PageInfo<Person> personPageInfo = new PageInfo<>(personList);

        //得到分页中的person条目对象

        List<Person> pageList = personPageInfo.getList();

        //将结果存入map进行传送

        result.put("pageInfo" , pageList);

        return "person_list";

    }

解析:

(1)PageHelper.startPage(pageNum , pageSize),这个方法就是类似我们数据库操作的limit start , count

(2)得到的对象PageInfo里面包含很多的字段信息,这个可以自己看源码,非常详细

(3)如果我们只想得到分页处理之后我们的实体对象的结果,那么就调用PageInfo对象的getList()方法即可。

(4)这种配置使用的方式是最通用的方式,也就是对于环境搭建不同方式都可以利用这种使用方法。

优点:可以少写自己封装的工具类,并且还能得到很多分页信息,也少写了count总数的sql!!

问题:如果运行时出现,org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.github.pagehelper.autoconfigure.PageHelperAutoConfiguration': 这个错误。

解决办法:这是由于分页插件pagehelper的版本和mybatis不兼容的原因,修改分页插件的版本即可。


注意事项:

分页不安全的情况

PageHelper 方法使用了静态的 ThreadLocal 参数,分页参数和线程是绑定的。

只要你可以保证在 PageHelper 方法调用后紧跟 MyBatis 查询方法,这就是安全的。因为 PageHelper 在 finally 代码段中自动清除了 ThreadLocal 存储的对象。

相关文章

网友评论

      本文标题:Mybatis分页插件PageHelper

      本文链接:https://www.haomeiwen.com/subject/rdsfuqtx.html