您的位置:首页 > 编程语言 > Java开发

springboot整合mybatis之分页插件pagehelper

2019-01-11 08:51 633 查看

在springboot中使用分页插件非常简单
我使用的是PageHelper的starter
博主之前用的PageHelper.startPage(pageNum, 5);会有黄色的警告,忍受不了,换了PageMethod.startPage(pageNum, 5);
这篇文章是在spring boot整合mybatis添加数据(带表单验证功能)(二)上面做的一点修改,实现分页查询功能
1.在pom.xml中添加pagehelper的依赖

<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>

2.application.properties添加pagehelper的配置信息(我用的是mysql数据库,更改数据库pagehelper.helperDialect=mysql,将后面的信息改成对应的数据库)

#pagehelper
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql

3.UsersMapper(这里用的是注解方式,不用去写配置文件,在实际开发中,查询语句不要直接写select *)

public interface UsersMapper {
@Select("select * from user")
List<Users> selectUser();
}

4.service

public interface UsersService {
List<Users> selectUser();
}

5.service的实现类

@Service
@Transactional
public class UsersServiceImpl implements UsersService {
@Autowired
private UsersMapper userMapper;
@Override
public List<Users> selectUser() {
return this.userMapper.selectUser();
}

}

6.controller

@Controller
public class AddController {
@Autowired
private UsersService usersService;
@GetMapping("/getAllUser")
public String getAllUser(Model model, @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum) {
PageMethod.startPage(pageNum, 5);
List<Users> list = usersService.selectUser();
PageInfo<Users> pageInfo = new PageInfo<Users>(list);
model.addAttribute("pageInfo", pageInfo);
return "list";
}

}

7.前端页面
list.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2 style="color: green; text-align: center">All Records in Table user</h2>
<div align="center">
<table style="width: 150px">
<tr style="color: red">
<th>Id</th>
<th>Username</th>
</tr>
<tr th:each="user:${pageInfo.list}">
<td th:text="${user.id}"></td>
<td th:text="${user.username}" style="padding-left:20px"></td>
</tr>
</table>
<p>
当前 <span th:text="${pageInfo.pageNum}"></span> 页,总 <span
th:text="${pageInfo.pages}"></span> 页,共 <span
th:text="${pageInfo.total}"></span> 条记录
</p>
<a th:href="@{/getAllUser}">首页</a> <a
th:href="@{/getAllUser(pageNum=${pageInfo.hasPreviousPage}?${pageInfo.prePage}:1)}">上一页</a>
<a
th:href="@{/getAllUser(pageNum=${pageInfo.hasNextPage}?${pageInfo.nextPage}:${pageInfo.pages})}">下一页</a>
<a th:href="@{/getAllUser(pageNum=${pageInfo.pages})}">尾页</a>
</div>
</body>
</html>

8.一些常用的pageInfo的属性

9.最后的效果

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: