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.
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
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<User> getUserLImit(Map<String,Object> map);
<!-- 分页查询--> <select id="getUserLImit" parameterType="map" resultType="pojo.User"> select * from firend_mq.users limit #{startIndex},#{pageSize} </select>
//测试分页查询 @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(); }
Based on RowBounds class object implementation, based on java code
Writing interface//RowBounds实现分页查询 List<User> getUserLRowBounds();
<!-- RowBounds 分页查询--> <select id="getUserLRowBounds" resultType="pojo.User"> select * from firend_mq.users </select>
//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(); }
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!