1,引入依赖,spingboot版本,少配置及兼容性
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.6</version>
</dependency>
2,配置:
pagehelper:
helper-dialect: mysql
reasonable: true
support-methods-arguments: true
params: count=countSql
page-size-zero: true
3,使用方式
controller
@GetMapping("/getUserList")
@ResponseBody
public PageInfo<User> getUserList(@RequestParam int pageNum, @RequestParam int pageSize) {
PageHelper.startPage(pageNum,pageSize);
List<User> userList = userService.getUserList();
PageInfo<User> pageInfo = new PageInfo<>(userList);
return pageInfo;
}
service
public interface UserService {
public User getUser();
List<User> getUserList();
}
service实现类
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
public User getUser() {
User user = userMapper.getUser();
return user;
}
@Override
public List<User> getUserList() {
return userMapper.getUserList();
}
}
mapper
@Mapper
public interface UserMapper {
public User getUser();
List<User> getUserList();
}
<mapper namespace="top.lihenhao.myspring.mapper.UserMapper">
<select id="getUser" resultType="top.lihenhao.myspring.model.User">
select * from user limit 1;
</select>
<select id="getUserList" resultType="top.lihenhao.myspring.model.User">
select * from user // 注意这里不能加分号,PageHelper会加入分页数据
</select>
</mapper>
测试
http://localhost:9999/getUserList?pageNum=1&pageSize=1
3,其他
mysql 分页方法:
limit 0,5 // 0为偏移量,取第一页,5条数据
limit 5,10 // 取第2页面,6-10的5条数据
pagehelper作为myabtis分页插件使用,其他如mybatis-plus有自带分页。
标签:插件,分页,List,mybtis,pagehelper,getUser,getUserList,public From: https://www.cnblogs.com/lihenhao/p/17175395.html