Sprinboot using simple and JPA native tab assembly

A, POM.xml configuration file
 
<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>

  

 
Second, the relevant code
 
entity:
 
@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:  only need to inherit JpaRepository, you can inherit PagingAndSortingRepository more functions, where only the most simple demo
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 defined many ways, but the core of information are only two: First, paging information (page, size (limit)), the second is the sort of information. Spring Data Jpa a concrete implementation of PageRequest, Spring MVC provides very good support for the Spring Data JPA, we only provide pagination and ordering information to:
//分页
@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);
}
 
Third, the object and return Json
 
Pageable return a Json format for easy viewing objects:
{
    "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 shows the correlation properties of Pageable; can also be used directly,
I'm using here is thymeleaf template, so I use the first controller to render the template, core display data as follows, before and after the introduction omitted.
 
<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 is </ TH> 
        <TH> <a th:href="@{/user/findById(id=${user.getUserid()})}"> See </a> </ th> <-! code is implemented are not shown, may be deleted or their implementation ->
    </ TR>
        <TH> <a th:href="@{/user/deleteById(id=${user.getUserid()})}"> Delete </a> </ th> <-! code is implemented are not shown, may be deleted or realize his ->
    </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>
 
 
 Fourth, the reference to other
 
Another may refer to the article:  https://www.tianmaying.com/tutorial/spring-jpa-page-sort (Spring Data JPA integration with the Spring MVC: paging and sorting)
Details of the PagingAndSortingRepository, Pageable objects, etc., but only returned Json data, but I used Thymeleaf return objects and resolve, to achieve a faster front-end.
 
 

Guess you like

Origin www.cnblogs.com/kingstar718/p/10972700.html