作一个搜索商品名称的分页查询,我是小菜鸟,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>