Spring Data JPA - Web 支持、使用Pageable 参数Thymeleaf 视图进行搜索/过滤、排序和分页

实体

package com.example;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import java.util.Date;
import lombok.Data;
import org.hibernate.annotations.DynamicUpdate;

@Data
@Entity
@DynamicUpdate

public class LoginLog {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String username;

    private String description;

    private String ip;

    private String sessionid;

    private Date eventtime;

}

存储库

package com.example;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

public interface LoginLogRepository extends JpaRepository {

    Page findAllByOrderByIdDesc(Pageable pageable);

    @Query("SELECT l FROM LoginLog l WHERE CONCAT(l.username, ' ', l.description, ' ', l.ip, ' ', l.sessionid) LIKE %?1%")
    public Page search(String keyword, Pageable pageable);

}

MVC 控制器

package com.example;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.query.Param;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class LoginLogController {

    @Autowired
    private LoginLogRepository loginLogRepository;

    @GetMapping("/loginlog")
    public String getLoginLog(@Param("keyword") String keyword, @PageableDefault(size = 10, sort = "id", direction = Sort.Direction.DESC) Pageable pageable,
            Model model) {

        if (keyword == null || keyword.trim().isEmpty()) {
            Page page = loginLogRepository.findAll(pageable);
            List sortOrders = page.getSort().stream().collect(Collectors.toList());
            if (sortOrders.size() > 0) {
                Sort.Order order = sortOrders.get(0);
                model.addAttribute("sortProperty", order.getProperty());
                model.addAttribute("sortDesc", order.getDirection() == Sort.Direction.DESC);
                model.addAttribute("sortDirection", order.getDirection().isAscending() ? "asc" : "desc");
                model.addAttribute("reverseSortDirection", order.getDirection().isAscending() ? "desc" : "asc");
            }
            model.addAttribute("keyword", "");
            model.addAttribute("page", page);
        } else {
            Page page = loginLogRepository.search(keyword, pageable);
            List sortOrders = page.getSort().stream().collect(Collectors.toList());
            if (sortOrders.size() > 0) {
                Sort.Order order = sortOrders.get(0);
                model.addAttribute("sortProperty", order.getProperty());
                model.addAttribute("sortDesc", order.getDirection() == Sort.Direction.DESC);
                model.addAttribute("sortDirection", order.getDirection().isAscending() ? "asc" : "desc");
                model.addAttribute("reverseSortDirection", order.getDirection().isAscending() ? "desc" : "asc");
            }
            model.addAttribute("keyword", keyword);
            model.addAttribute("page", page);
        }

        return "loginlog/list_loginlog";

    }

}

Thymeleaf 视图



    
        
        
        Dashboard - Admin Bootstrap Template
        
        
        
        
        
        
        
        
        
    
    
        
        
        


登录日志

ID 登录账号 事件描述 IP 会话ID 时间
ID 登录账号 事件描述 IP 会话ID 时间
总计99行,当前显示1 - 5行   总计99
with love FreeeTemplates

输出

Spring Data JPA - Web 支持、使用Pageable 参数Thymeleaf 视图进行搜索/过滤、排序和分页_第1张图片

你可能感兴趣的:(前端,java,spring)