标签:param extends 第一个 highlight interface ges 首页 文件配置 配置
<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
}
public interface UserInfoDao extends JpaRepository<UserInfo, Long>, Serializable {
}
public interface UserInfoService {
//分页查询
Page<UserInfo> findByPage(Pageable pageable);
}
@Service
@Transactional
public class UserInfoServiceImpl implements UserInfoService {
@Override
public Page<UserInfo> findByPage(Pageable pageable) {
return userInfoDao.findAll(pageable);
}
}
//分页
@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);
}
{
"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
}
<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>
标签:param extends 第一个 highlight interface ges 首页 文件配置 配置
原文地址:https://www.cnblogs.com/kingstar718/p/10972700.html