Home > Common Problem > What are the several ways of paging in mybatis?

What are the several ways of paging in mybatis?

藏色散人
Release: 2023-03-13 13:52:02
Original
2579 people have browsed it

The three ways of paging in mybatis are: 1. Use Limit paging, with core statements such as "select * from users limit startIndex,pageSize;"; 2. Use RowBounds to implement paging, with statements such as "List getUserLRowBounds();"; 3. Use the paging plug-in "PageHelper" to achieve it.

What are the several ways of paging in mybatis?

The operating environment of this tutorial: Windows 10 system, Dell G3 computer.

What are the several ways of paging in mybatis?

MyBatis commonly used paging methods

Why pagination?

If the front-end needs to display data, due to the large amount of data and one-time display, a large amount of data will appear on the page, and the loading may not be completed, which is very inefficient. At this time, paged query This problem can be solved very well by displaying huge data according to a certain number, and you can also display other data by clicking on the next page or previous page, which is more efficient!

Let’s introduce several paging queries commonly used by mybatis!
First, take a look at all the data in the database. There are comments in the detailed code

What are the several ways of paging in mybatis?

##1. Use Limit paging
sql使用的核心语句
Copy after login
-- 语法:表示从startIndex下标开始,一页显示pageSize个
select * from users limit startIndex,pageSize;
-- 语法:表示显示[0,n]范围的数据
select * from users limit n;
Copy after login

Use Mybatis to implement paging and implement it based on sql

Writing interface

 //分页查询
    List<User> getUserLImit(Map<String,Object> map);
Copy after login

Writing Mapper.xml

<!--    分页查询-->
    <select id="getUserLImit" parameterType="map" resultType="pojo.User">
        select * from firend_mq.users limit #{startIndex},#{pageSize}    </select>
Copy after login

Test class

    //测试分页查询
    @Test
    public  void getUserLImit(){
        SqlSession sqlSession = Mybatisutil.getSqlSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);

        Map<String, Object> map = new HashMap<>();
        map.put("startIndex",0);
        map.put("pageSize",3);
        List<User> userLImit = mapper.getUserLImit(map);
        for (User user : userLImit) {
            System.out.println(user);
        }
        sqlSession.close();
    }
Copy after login

Query Result:

What are the several ways of paging in mybatis?

2. RowBounds paging implementation

Based on RowBounds class object implementation, based on java code

Writing interface

//RowBounds实现分页查询
    List<User> getUserLRowBounds();
Copy after login

Writing Mapper.xml, the query is actually all users

   <!--   RowBounds 分页查询-->
    <select id="getUserLRowBounds"  resultType="pojo.User">
        select * from firend_mq.users    </select>
Copy after login

Test class

    //RowBounds分页查询
    @Test
    public  void getUserLRowBounds(){
        SqlSession sqlSession = Mybatisutil.getSqlSession();
        //RowBounds对象  参数(起点,个数)
        RowBounds rowBounds = new RowBounds(2, 3);

        //通过java代码层面实现分页,第一个参数是接口类的方法路径
        List<User> userlist = sqlSession.selectList("dao.UserDao.getUserLRowBounds", null, rowBounds);
        for (User user : userlist) {
            System.out.println(user);
        }
        sqlSession.close();
    }
Copy after login
Result:

What are the several ways of paging in mybatis?

3. Use the paging plug-in to achieve

What are the several ways of paging in mybatis?

#If you are interested, you can find out and post the official website link of the plug-in. There are official usage documents, learn about it by yourself

PageHelper paging plug-in

The above is the detailed content of What are the several ways of paging in mybatis?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template