[Semi-automatic ORM framework | MyBatis] Use of pagination plugin

2022.10.12: Beginners learning Java to write simple background management will inevitably be tortured by writing paging code (I was tortured), but after getting in touch with the MyBatis framework, only a few configurations and a few lines of code can complete the previous several times Workload, this article briefly describes the overall usage process of the MyBatis pagination plug-in, if it can help you, I am very honored!


Table of contents

1. MyBatis framework

2. MyBatis framework paging plug-in usage process

1. Create a new MAVEN project and import dependencies

2. Configure the plug-in in the MyBatis global configuration file

3. Test the plugin in the test class


1. MyBatis framework

First, let me briefly introduce the MyBatis framework, which is an excellent persistence layer framework. When we first learn Java, we need to write the code of the persistence layer by ourselves in order to connect to the database to submit SQL statements, selectively set the parameters of the SQL statement, and finally get the result set returned by the database. Using the MyBatis framework eliminates almost all JDBC code and setting parameters. and the job of getting the result set. MyBatis can be configured through simple XML or annotations to map the database with the object entity class we set ourselves.

2. MyBatis framework paging plug-in usage process

1. Create a new MAVEN project and import dependencies

I won’t introduce much about the MAVEN project. Idea can directly create a new MAVEN project. After the MAVEN project is built, it comes with an XML file called pom.xml. In this file, you can complete the import of dependencies and some related configurations. What we need to do It is the dependency of importing the paging plug-in. It is recommended to find the dependency on https://mvnrepository.com/, and directly search for the plug-in dependency pagehelper we need to use .

Choose the first one, click in and choose a version, I choose 5.3.2 here

 Copy the dependency information to the pom.xml file, and note that the outer layer also needs a dependencies pair tag.

    <dependencies>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.3.2</version>
        </dependency>
    </dependencies>

2. Configure the plug-in in the MyBatis global configuration file

The use of MyBatis requires a global configuration file mybatis.cfg.xml, and configure the following code in the configuration tag of the global configuration file to allow the framework to recognize the plug-in.

    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
    </plugins>

Note: If your configuration tag will be underlined in red, it means that your plugins tag is placed in the wrong position. There is its own set of identification order in the DTD file, and the plugins tag should be placed above the environment tag.

3. Test the plugin in the test class

Well, the configuration is over at this step, and it is time to test the plug-in. The premise of the test is that you need to connect to your database in the global configuration file, and there is data to be tested in your database. In terms of Java, you need to create an object entity class, and the mapper.xml mapping file has completed the database column and The mapping of the object entity class, and the corresponding method in the mapper.xml mapping file is the operation of adding, deleting, modifying and checking the database.

I won’t repeat the above, we concentrate on testing the plug-in, and the above process has been completed by default.

The following process is the standard process of using the MyBatis framework, but it will be simplified under the premise of using other frameworks.

    public static void main(String[] args) throws IOException {
        InputStream is = Resources.getResourceAsStream("mybatis.cfg.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = factory.openSession();
        PageHelper.startPage(1, 3);
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> students = studentMapper.selectAll();
        PageInfo<Student> pageInfo = new PageInfo<>(students);
        System.out.println(pageInfo.getList());
        System.out.println(pageInfo.getTotal());
    }

The first three lines of the code are fixed routines, get the input stream of the MyBatis global configuration file, create a factory class after loading the global configuration file, use the factory class to create a SqlSession, and SqlSession encapsulates all the methods of adding, deleting, modifying and checking. Then open the plug-in, there are two parameters: the current number of pages and how many pieces of data per page, and then get the interface object of mapper. method.

The important step: create a PageInfo object, pass in the array returned after calling the method, here is to query all SQL, all passed in is a collection, after passing in, it will return a pageInfo object, this object contains all we need Information, such as the total number of entries, the current number of pages, how many pieces of data per page, and the data collection, can be viewed through get+ the desired information. The above is to obtain the collection and total number of current pages.

Let's see the result:

 You can see that there is a lot of information in the returned pageInfo object, the set of three pieces of data that we need to print on the current page, and a total of five pieces of data are also printed out, and the test is completed.

Guess you like

Origin blog.csdn.net/weixin_45973679/article/details/127291684