Mybatis series (21) – related concepts – PageHelper

Time:2020-10-24

Step 1: introduction to PageHelper
Step 2: run first, see the effect, and then learn
Step 3: imitation and troubleshooting
Step 4: jar package
Step 5: set the data to 100
Step 6: configure the plug-in
Step 7: Category.xml
Step 8: paging query
Step 9: get the total number

Step 1: introduction to PageHelper

PageHelper is a sharp mybatis paging plug-in. After using this plug-in, paging development is easier and easier.
This knowledge point explains how to use the plug-in. For more details, please click Page helper official

Step 2: run first, see the effect, and then learn

As the old rule, first download the executable items in the download area (click to enter), configure and run them, and then learn what steps to do to achieve this effect.

Step 3: imitation and troubleshooting

After making sure that the runnable project runs correctly, follow the steps of the tutorial and imitate the code again.
It is inevitable that there is code discrepancy in the simulation process, which leads to the failure to get the expected running resultsright key(can run the project) and your own code to locate the problem.
In this way,Learning is effective and troubleshooting is efficientIt can obviously improve the learning speed and cross the threshold of learning.

It is recommended to use diffmerge software for folder comparison. Compare your own project folder with my runnable project folder.
This software is very powerful, you can know which two files in the folder are wrong, and mark them out clearly
Here is a green installation and use tutorial: diffmerge download and use tutorial

Step 4: jar package

Because it is a third-party plug-in, additional jar packages are required. They are all available in the download area (click to enter): pagehelper-5.1.0-beta2.jar, jsqlparser-1.0.jar

Step 5: set the data to 100

In order to observe the effect of paging query, delete the data in the classification table, and then add 100 data

package com.how2java;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import com.how2java.pojo.Category;
public class TestMybatis {
    public static void main(String[] args) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sqlSessionFactory.openSession();
        List<Category> cs = session.selectList("listCategory");
        for (Category c : cs) {
            session.delete("deleteCategory", c);
        }
        for (int i = 0; i < 100; i++) {
            Category c = new Category();
            c.setName("category name " + i);
            session.insert("addCategory", c);
        }
        List<Category> cs2 = session.selectList("listCategory");
        for (Category c : cs2) {
            System.out.println(c.getName());
        }
        session.commit();
        session.close();
    }
}

Step 6: configure the plug-in

In mybatis- config.xml Add 15-19 lines of code to open the PageHelper plug-in

<?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>
    <settings>
        <! -- turn on the delay load switch -- >
        <setting name="lazyLoadingEnabled" value="true" />
        <! -- change active load to message load that is, load on demand -- >
        <setting name="aggressiveLazyLoading" value="false"/>
    </settings>
    <typeAliases>
        <package name="com.how2java.pojo"/>
    </typeAliases>
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor">

        </plugin>
    </plugins>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/how2java?characterEncoding=UTF-8"/>
                <property name="username" value="root"/>
                <property name="password" value="admin"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/how2java/pojo/Category.xml"/>
        <mapper class="com.how2java.mapper.CategoryMapper"/>
        <mapper class="com.how2java.mapper.ProductMapper"/>
    </mappers>

</configuration>

Step 7: Category.xml

The limit in lines 24-26 is commented out, because the paging related work will be done by PageHelper, so there is no need to write it yourself

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.how2java.pojo">
    <insert id="addCategory" parameterType="Category" >
            insert into category_ ( name ) values (#{name})   
        </insert>

    <delete id="deleteCategory" parameterType="Category" >
            delete from category_ where id= #{id}  
        </delete>

    <select id="getCategory" parameterType="_int" resultType="Category">
            select * from   category_  where id= #{id}   
        </select>

    <update id="updateCategory" parameterType="Category" >
            update category_ set name=#{name} where id=#{id}   
        </update>
    <select id="listCategory" resultType="Category">
        select * from   category_
        <!--                 <if test="start!=null and count!=null"> -->
        <!--                     limit #{start},#{count} -->
        <!--                 </if> -->
    </select>
</mapper>

Step 8: paging query

The query is very interesting. You just need to execute a statement before executing all the calls of the query
PageHelper.offsetPage(`0,` `5);`

This means to find out the first page of data, 5 items per page

Mybatis series (21) - related concepts - PageHelper

package com.how2java;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import com.github.pagehelper.PageHelper;
import com.how2java.pojo.Category;
public class TestMybatis {
    public static void main(String[] args) throws IOException, InterruptedException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sqlSessionFactory.openSession();
        PageHelper.offsetPage(0, 5);
        List<Category> cs = session.selectList("listCategory");
        for (Category c : cs) {
            System.out.println(c.getName());
        }
        session.commit();
        session.close();
    }
}

Step 9: get the total number

Through the code on line 31

PageInfo page= new PageInfo<>(cs);

You can get the paging information, including the total number and others

Mybatis series (21) - related concepts - PageHelper

package com.how2java;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.how2java.pojo.Category;
public class TestMybatis {
    public static void main(String[] args) throws IOException, InterruptedException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sqlSessionFactory.openSession();
        PageHelper.offsetPage(0, 5);
        List<Category> cs = session.selectList("listCategory");
        for (Category c : cs) {
            System.out.println(c.getName());
        }
        PageInfo pageInfo = new PageInfo<>(cs);
        System.out.println (total number:+ pageInfo.getTotal ());
        System.out.println(pageInfo);
        session.commit();
        session.close();
    }
}

For more information, click to learn: https://how2j.cn/k/mybatis/mybatis-pagehelper/1372.html

Recommended Today

Layout of angular material (2): layout container

Layout container Layout and container Using thelayoutDirective to specify the layout direction for its child elements: arrange horizontally(layout=”row”)Or vertically(layout=”column”)。 Note that if thelayoutInstruction has no value, thenrowIs the default layout direction. row: items arranged horizontally.max-height = 100%andmax-widthIs the width of the item in the container. column: items arranged vertically.max-width = 100%andmax-heightIs the height of the […]