getUserLRowBounds();"; 3. Use the paging plug-in "PageHelper" to achieve it."/> getUserLRowBounds();"; 3. Use the paging plug-in "PageHelper" to achieve it.">

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

What are the several ways of paging in mybatis?

藏色散人
藏色散人 Original
2023-03-13 13:52:02 2448browse

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使用的核心语句
-- 语法:表示从startIndex下标开始,一页显示pageSize个 select * from users limit startIndex,pageSize; -- 语法:表示显示[0,n]范围的数据 select * from users limit n;

Use Mybatis to implement paging and implement it based on sql

Writing interface

//分页查询 List getUserLImit(Map map);
Writing Mapper.xml

 
Test class

//测试分页查询 @Test public void getUserLImit(){ SqlSession sqlSession = Mybatisutil.getSqlSession(); UserDao mapper = sqlSession.getMapper(UserDao.class); Map map = new HashMap(); map.put("startIndex",0); map.put("pageSize",3); List userLImit = mapper.getUserLImit(map); for (User user : userLImit) { System.out.println(user); } sqlSession.close(); }
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 getUserLRowBounds();
Writing Mapper.xml, the query is actually all users

 
Test class

//RowBounds分页查询 @Test public void getUserLRowBounds(){ SqlSession sqlSession = Mybatisutil.getSqlSession(); //RowBounds对象 参数(起点,个数) RowBounds rowBounds = new RowBounds(2, 3); //通过java代码层面实现分页,第一个参数是接口类的方法路径 List userlist = sqlSession.selectList("dao.UserDao.getUserLRowBounds", null, rowBounds); for (User user : userlist) { System.out.println(user); } sqlSession.close(); }
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!

Statement:
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
Previous article:What is ms card Next article:What is ms card