MyBatis中三种分页查询方式

文章目录

通过limit分页查询
mapper接口

1 List<User> getUserByLimit(Map<String,Integer> map);

mapper.xml

1 <select id="getUserByLimit" parameterType="map" resultType="com.hzy.pojo.User">
2 select * from user limit #{offset},#{pageSize}
3 </select>

test

1 Map<String,Integer> map = new HashMap<String, Integer>();
2 map.put("offset",1);
3 map.put("pageSize",2);
4 List<User> userByLimit = userMapper.getUserByLimit(map);

通过Rowbounds
mapper接口

List<User> getUserByRowBounds(RowBounds rowBounds);

mapper.xml

1 <select id="getUserByRowBounds" resultType="com.hzy.pojo.User">
2 select * from user
3 </select>

test

1 RowBounds rowBounds = new RowBounds(1,2);
2 List<User> userByRowBounds = userMapper.getUserByRowBounds(rowBounds);

通过分页插件pagehelper
pom.xml

1 <dependency>
2 <groupId>com.github.pagehelper</groupId>
3 <artifactId>pagehelper</artifactId>
4 <version>4.1.6</version>
5 </dependency>

配置mybatis-config.xml

1 <plugins>
2 <plugin interceptor="com.github.pagehelper.PageHelper">
3 <property name="dialect" value="mysql"/>
4 </plugin>
5 </plugins>

mapper接口

1 List<User> getUserByPageHelper();

mapper.xml

1 <select id="getUserByPageHelper()" resultType="com.hzy.pojo.User">
2 select * from user
3 </select>

test

1 PageHelper.offsetPage(1,2);
2 List<User> userList = userMapper.getUserByPageHelper();

 





posted @ 2022-10-12 08:39  我来偷家了  阅读(5790)  评论(0编辑  收藏  举报