springboot+springJPA+thmleaf按條件分頁查詢

 

作一個搜索商品名稱的分頁查詢,我是小菜鳥,html

寫的不對的地方,歡迎路過大神指教前端

maven中POM.xml引入spring

<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>


數據庫:數據庫

  CREATE TABLE `product` (
    `product_id` int(11) NOT NULL AUTO_INCREMENT,
    `product_name` varchar(255) DEFAULT NULL,
    `product_size` varchar(255) DEFAULT NULL,
    `product_price` double DEFAULT NULL,
    `product_picture` varchar(255) DEFAULT NULL,
    PRIMARY KEY (`product_id`)
  )app

 

實體類:框架

  @Entity //實體類對象註解
  @Table(name="product") 表
  public class Product {
    @Id 主鍵
    @GeneratedValue 自增
    private Integer product_id;
    @Column(name="product_name")
    private String product_name;
    @Column(name="product_size")
    private String product_size;
    @Column(name="product_price")
    private double product_price;
    @Column(name="product_picture")
    private String product_picture;
    maven

  此處省略set,get方法
  }ide

DAO:
注意包:
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;spring-boot

  @Repository
  public interface Productdao extends JpaRepository<Product, Integer>,JpaSpecificationExecutor<Product> {ui

注意:
JpaSpecificationExecutor 接口具備方法

Page<T> findAll(Specification<T> spec, Pageable pageable); //分頁按條件查詢

List<T> findAll(Specification<T> spec); //不分頁按條件查詢

 

Sevice:

只是分頁查詢的一個方法

@Autowired
private Productdao productdao;

public Page<Product> findAll(Product product, PageRequest pageRequest) {
  Page<Product> page = null;
  若是product的name屬性爲空
    if (product == null) {
       僅僅是分頁,調用此方法
      page = productdao.findAll(pageRequest);
    } else {
     
        Specification<Product> specification = new Specification<Product>() {
        
          Root:查詢哪一個表
          CriteriaQuery:查詢哪些字段,排序是什麼
          CriteriaBuilder:字段之間是什麼關係,如何生成一個查詢條件,每個查詢條件都是什麼方式
          Predicate(Expression):單獨每一條查詢條件的詳細描述
       @Override 

       public Predicate toPredicate(Root<Product> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
          List<Predicate> predicates = new ArrayList<>();
            if (product.getProduct_name()!= null) {
              predicates.add(cb.like(root.get("product_name").as(String.class), "%" + product.getProduct_name().trim() + "%"));
             }

            if (predicates.size() > 0) {
              Predicate[] predicateArr = new Predicate[predicates.size()];
              return query.where(predicates.toArray(predicateArr)).getRestriction();
            }
          這種方式使用JPA的API設置了查詢條件,因此不須要再返回查詢條件Predicate給Spring Data Jpa,故最後return null;便可
          return null;
            }
          };
          page =productdao.findAll(specification, pageRequest);
        }
      return page;
      }


Controller/*分頁展現商品*/
    @RequestMapping(value="/showproduct")
    public String toProductList(Product product,
                @RequestParam(value = "page", defaultValue = "0") Integer page,
                @RequestParam(value = "size", defaultValue = "5") Integer size,
                Model model) {

      Page<Product> productPage =productbiz.findAll(product,new PageRequest(page, size));
      model.addAttribute("page",page);
      model.addAttribute("productPage", productPage );
      model.addAttribute("product", product);
      return "goods/goods/showproduct" ;
    }

thymleaf前端頁面:

引入thymleaf
    <html xmlns="http://www.w3.org/1999/xhtml"
       xmlns:th="http://www.thymeleaf.org"
        xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">

  搜索框和分頁不展現了,用了別人的框架,拷過去也不能用

<table >
<thead>
<tr>
<th >商品ID</th>
<th >商品名稱</th>
<th >商品尺寸</th>
<th >商品價格</th>
<th >商品圖片</th>
</tr>
</thead>
<tbody>
<tr th:each="product,memberStat:${articlePage}">
<td th:text="${product.product_id}"></td>
<td th:text="${product.product_name}"></td>
<td th:text="${product.product_size}"></td>
<td th:text="${product.product_price}"></td>
<td th:text="${product.product_picture}"></td>
</tr>
</tbody>
</table>
相關文章
相關標籤/搜索