Sprinboot中使用JPA及简单的原生分页组件
一、POM.xml文件配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
二、相关代码
实体:
@Entity
@Table(name = "userinfo")
public class UserInfo implements Serializable {
@Id
@GeneratedValue
private long userid;
private String username;
private String password;
private int age;
省略getter&setter
}
DAO: 仅需要继承JpaRepository, 也可以继承PagingAndSortingRepository 实现更多功能, 这里只做最简单的演示
public interface UserInfoDao extends JpaRepository<UserInfo, Long>, Serializable {
}
Service:
public interface UserInfoService {
//分页查询
Page<UserInfo> findByPage(Pageable pageable);
}
ServiceImpl:
@Service
@Transactional
public class UserInfoServiceImpl implements UserInfoService {
@Override
public Page<UserInfo> findByPage(Pageable pageable) {
return userInfoDao.findAll(pageable);
}
}
Controller:
Pageable定义了很多方法,但其核心的信息只有两个:一是分页的信息(page、size(limit)),二是排序的信息。Spring Data Jpa提供了PageRequest的具体实现,Spring MVC提供了对Spring Data JPA非常好的支持,我们只提供分页以及排序信息即可:
//分页
@RequestMapping("/list")
public String pageUser(@RequestParam(value = "start", defaultValue = "0") Integer start,
@RequestParam(value = "limit", defaultValue = "10") Integer limit,
Model model){
start = start <0 ? 0:start;
Sort sort = new Sort(Sort.DEFAULT_DIRECTION, "userid");
Pageable pageable = new PageRequest(start, limit, sort);
Page<UserInfo> page = userInfoService.findByPage(pageable);
model.addAttribute("page", page);
return "user/userlist";
}
//分页json
@RequestMapping("/pagejson")
@ResponseBody
public Page<UserInfo> pageUser(@PageableDefault(value = 15, sort = "userid", direction = Sort.Direction.ASC)Pageable pageable){
return userInfoService.findByPage(pageable);
}
三、返回Json及对象
返回一个Json格式的Pageable方便查看对象:
{
"content": [
{
"userid": 1,
"username": "sasa",
"password": "e",
"age": 123
},
{
"userid": 11,
"username": "问问",
"password": "w",
"age": 121
}
],
"pageable": {
"sort": {
"sorted": true,
"unsorted": false,
"empty": false
},
"offset": 0,
"pageSize": 15,
"pageNumber": 0,
"paged": true,
"unpaged": false
},
"totalElements": 16,
"totalPages": 2,
"last": false,
"number": 0,
"size": 15,
"sort": {
"sorted": true,
"unsorted": false,
"empty": false
},
"numberOfElements": 15,
"first": true,
"empty": false
}
Json显示了Pageable的相关属性;, 也可直接使用,
这里我使用的是thymeleaf模板, 于是我使用第一个controller来渲染模板, 核心的显示数据如下, 省略了前后的引入.
<table class="table table-hover">
<thead>
<tr>
<th>id</th>
<th>name</th>
<th>password</th>
<th>age</th>
<th>details</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr th:each="user: ${page.content}">
<th scope="row" th:text="${user.getUserid()}">2</th>
<th th:text="${user.getUsername()}">leo</th>
<th th:text="${user.getPassword()}">pwd</th>
<th th:text="${user.getAge()}">13</th>
<th><a th:href="@{/user/findById(id=${user.getUserid()})}" >查看</a></th> <!--代码中未给出实现,可删除或自己实现-->
<th><a th:href="@{/user/deleteById(id=${user.getUserid()})}" >删除</a></th> <!--代码中未给出实现,可删除或自己实现-->
</tr>
</tbody>
</table>
<ul class="list-group">
<li class="list-group-item">
<a class="btn-default" th:href="@{/user/list(start=0)}">[首页]</a>
<a class="btn-default" th:if="${not page.isFirst()}" th:href="@{/user/list(start=${page.number-1})}">[上页]</a>
<a class="btn-default" th:if="${not page.isLast()}" th:href="@{/user/list(start=${page.number+1})}">[下页]</a>
<a class="btn-default" th:href="@{/user/list(start=${page.totalPages-1})}">[末页]</a>
</li>
</ul>
四、参考其他
另可参考这篇文章: https://www.tianmaying.com/tutorial/spring-jpa-page-sort(整合Spring Data JPA与Spring MVC: 分页和排序)
详细介绍了PagingAndSortingRepository, Pageable对象等, 但是只返回了Json数据, 而我使用了Thymeleaf返回对象并解析, 更快实现前端.

浙公网安备 33010602011771号