如何使用hibernate在spring boot中实现分页

Nad*_*med 13 java pagination hibernate hql spring-boot

我正在使用带有hibernate的spring boot,我想在我的项目中使用分页.我在谷歌上搜索并看到很多例子,但我无法在我的项目中实现它.

我想如果我在我的网址中传递1然后10个结果应该来,如果我传递2然后接下来10个结果应该来,依此类推.

这是我的

@Transactional
public interface PostDao extends CrudRepository<Post, Long>{

@Query(getAllPostsByRank)
List<Post> getAllPostsByRank();

final String getAllPostsByRank= "from Post order by value DESC";
}
Run Code Online (Sandbox Code Playgroud)

这是我的控制器

@RequestMapping("/top")
    @ResponseBody 
     public List<Post> getAllPosts(HttpServletRequest req, HttpServletResponse res) throws ServletException {

List<Post> postobj = postDao.getAllPostsByRank();
return postobj;
}
Run Code Online (Sandbox Code Playgroud)

这是我的网址:

http://localhost:8888/v1.0/post/top/1
Run Code Online (Sandbox Code Playgroud)

请建议.

Ith*_*har 22

我会考虑org.springframework.data.domain.Pageable直接使用你的控制器.然后可以将此对象传递到JPA层,在该层中它将处理返回结果的数量和大小.

使用的好处Pageable是它返回一个Page可以在前端使用的对象,以形成上一页/下一页逻辑.

默认情况下,此类使用url参数' page '和' size '; 因此page = 0&size = 10将返回前10个项目.

因此,在您的情况下,代码可能看起来像:

@ResponseBody
@RequestMapping("/top/pages/")
public List<Post> getAllPosts(@PageableDefault(value=10, page=0) Pageable pageable) throws ServletException {
    Page page = postDao.findAll(pageable);
    return page.getContent();
}
Run Code Online (Sandbox Code Playgroud)

请注意,注释@PageableDefault只是设置默认值而不是必需的.

在前端下一页呼叫可以<a href="/top/pages?page=1">Next</a>; 这将返回11到20的帖子列表.


jee*_*mar 9

在Spring Boot中实现分页非常简单,只需要遵循基本步骤 -

1 - 在存储库界面中扩展PagingAndSortingRepository

public interface UserRepository extends PagingAndSortingRepository <User, Long> 
Run Code Online (Sandbox Code Playgroud)

2 - 方法声明应如下例所示

Page<User> userList(Pageable pageable);
Run Code Online (Sandbox Code Playgroud)

3 - Service类中的方法实现应该如下例所示

@Override
public Page<User> userList(Pageable pageable) {
        return userRepository.findAll(pageable);
}
Run Code Online (Sandbox Code Playgroud)

4 - 控制器类代码应如下所示

@GetMapping("/list")
public String userList(Model model, Pageable pageable) {
        Page<User> pages = userService.userList(pageable);
        model.addAttribute("number", pages.getNumber());
        model.addAttribute("totalPages", pages.getTotalPages());
        model.addAttribute("totalElements",       
                                      pages.getTotalElements());
        model.addAttribute("size", pages.getSize());
        model.addAttribute("users", pages.getContent());
        return "/user/list";
}
Run Code Online (Sandbox Code Playgroud)

从前端呼叫应该如下

http://localhost:8080/application/user/list?page=0&size=5
http://localhost:8080/application/user/list?page=1&size=5
http://localhost:8080/application/user/list?page=2&size=5
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请观看以下视频

Spring Boot:Pagination Basic

Spring Boot:Pagination Advanced

谢谢阅读


art*_*tle 8

核实.你的控制器

@RequestMapping("/top/pages/{pageno}")
    @ResponseBody 
     public List<Post> getAllPosts(@PathVariable("pageno") int pageno, HttpServletRequest req, HttpServletResponse res) throws ServletException {

List<Post> postobj = postDao.getAllPostsByRank(new PageRequest(pageno,10));
return postobj;
}
Run Code Online (Sandbox Code Playgroud)

你的道

@Transactional
public interface PostDao extends CrudRepository<Post, Long>{

@Query(getAllPostsByRank)
List<Post> getAllPostsByRank(Pageable pageable);

final String getAllPostsByRank= "from Post order by value DESC";
}
Run Code Online (Sandbox Code Playgroud)


小智 5

我已经在 Spring Boot 中实现了分页。下面是我的存储库。

    @Repository("userRepository")
    public interface UserRepository extends PagingAndSortingRepository<User, Long> {
  }
Run Code Online (Sandbox Code Playgroud)

下面是我的控制器。

@Controller
public class SampleController {

    @Autowired
    private UserRepository repository;

    @GetMapping("/userview")
    public String getEmployees(@PageableDefault(size = 1) Pageable pageable,
                               Model model) {
        Page<User> page = repository.findAll(pageable);
        model.addAttribute("page", page);
        return "userdetail";
    }
}
Run Code Online (Sandbox Code Playgroud)

下面是视图,为此我使用了百里香叶。

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<h2>USER DETAILS</h2>

<table class="table table-striped table-responsive-md">
    <thead>
    <tr>
        <th> ID </th>
        <th>Name</th>
        <th>Last Name</th>
        <th>Email</th>
        <th>Role</th>
    </tr>
    </thead>
    <tbody>
    <tr th:each="user : ${page.content}">
        <td th:text="${user.id}"></td>
        <td th:text="${user.email}"></td>
        <td th:text="${user.name}"></td>
        <td th:text="${user.lastName}"></td>
        <td th:text="${user.roles[0].role}"></td>

    </tr>
    </tbody>
</table>

<div class="pagination-div">
    <span th:if="${page.hasPrevious()}">
        <a th:href="@{/userview(page=${page.number-1},size=${page.size})}">Previous</a>
    </span>
    <th:block th:each="i: ${#numbers.sequence(0, page.totalPages - 1)}">
        <span th:if="${page.number == i}" class="selected">[[${i}+1]]</span>
        <span th:unless="${page.number == i}">
             <a th:href="@{/userview(page=${i},size=${page.size})}">[[${i}+1]]</a>
        </span>
    </th:block>
    <span th:if="${page.hasNext()}">
        <a th:href="@{/userview(page=${page.number+1},size=${page.size})}">Next</a>
    </span>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)