Ten, PageHelper paging plug-in of Mybatis

One, the principle of PageHelper:

Perform analysis on the basis of executing the original sql, automatically generate paging and select count (*) statements, because it involves the analysis and parsing of the original sql, so it is handed over to the sql interpreter component of jsqlparser, all must be correct It is referenced, and the specific sql execution process can be viewed after execution.

Second, the process of using PageHelper

  1. maven introduces PageHelop and jsqlparser

    Note: After pagehelper5.1.10, because of the new api, jsqlparser version must be 2.0 and above to be able to adapt

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

<dependency>
    <groupId>com.github.jsqlparser</groupId>
    <artifactId>jsqlparser</artifactId>
    <version>2.0</version>
</dependency>
  1. Mybatis-config.xml adds Plungin configuration
<plugins>
    <plugin interceptor="com.github.pagehelper.PageInterceptor">
        <!--指定对应的数据库,这个可以不写,插件会自动识别,但是严谨角度建议还是写一下-->
        <property name="helperDialect" value="mysql"/>
        <!--开启分页合理化(如输入第0页时,展示第一页数据,输入超过最大页数的数据时候,展示最大页数的数据)-->
        <property name="reasonable" value="true"/>
    </plugin>
</plugins>

goods.xml file configuration

<select id="selectPage" resultType="com.imooc.mybatis.entity.Goods">
 select * from t_goods where current_price &lt;1000
</select>

Note: I stepped on the pit here, and habitually added a semicolon to the tail of the sql statement, and an error was reported when it was run. After checking for a long time, everyone took a warning

your MySQL server version for the right syntax to use near ‘LIMIT 10, 10’ at line 1
The error may exist in mappers/goods.xml
The error may involve defaultParameterMap
The error occurred while setting parameters
SQL: select * from t_goods where current_price <1000; LIMIT ?, ?
Cause: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘LIMIT 10, 10’ at line 1

  1. Use PageHelper.startPage() in the code to automatically paginate
@Test
public void testSelectPage() {
    
    
    SqlSession sqlSession = null;
    try {
    
    
        sqlSession = MyBatisUtils.openSession();
        // 起始行号,每页显示行数
        PageHelper.startPage(2, 10);
        Page<Goods> page = (Page) sqlSession.selectList("goods.selectPage");
        System.out.println("总条数:" + page.getTotal());
        System.out.println("总页数:" + page.getPages());
        System.out.println("开始页数:" + page.getStartRow());
        System.out.println("结束页数:" + page.getEndRow());
        // 获得当前页数据
        List<Goods> goods = page.getResult();
        for (Goods goods1 : goods) {
    
    
            System.out.println(goods1.getTitle());
        }
    } catch (Exception e) {
    
    
        throw e;
    } finally {
    
    
        MyBatisUtils.closeSqlSession(sqlSession);
    }
}

Guess you like

Origin blog.csdn.net/qq_36792120/article/details/112474714