JPA 多表多條件分頁查詢
標簽: jpa springboot 多表聯查
業務場景:
主表:訂單表,與客戶表、產品表、物流表存在一對一關系,映射字段為id,現需要根據訂單編號、訂單日期、客戶名稱、客戶編號、產品名稱、產品編號、快遞單號查詢該筆訂單,需要支持模糊查詢和分頁。
Order實體類中的需要進行一對一關系映射:
@OneToOne
@JoinColumn(name = "express_id")
private Express express;
@OneToOne
@JoinColumn(name = "product_id")
private Product product;
@OneToOne
@JoinColumn(name = "customer_id")
private Customer customer;
Dao中的需要繼承JpaRepository,JpaSpecificationExecuto兩個接口:
@Repository
public interface OrderDao<T, ID extends Serializable> extends JpaRepository<T, ID>, JpaSpecificationExecutor<T> {
}
Service中的寫法:
Pageable通過前端傳入的pageSize 和 pageNumber進行創建
Pageable pageable = PageRequest.of(pageNum - 1, pageSize);
@Autowired
OrderDao licenseDao;
@Autowired
KeyPairService keyPairService;
public Page<Order> getLicenseList(Order order, Pageable pageable) {
Specification<License> specification = new Specification<Order>() {
@Override
public Predicate toPredicate(Root<License> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
List<Predicate> list = new ArrayList<Predicate>();
Join<Order, Express> expressJoin = root.join("express", JoinType.LEFT);
Join<Order, Product> proJoin = root.join("product", JoinType.LEFT);
Join<Order, Customer> customerJoin = root.join("customer", JoinType.LEFT);
if (null != order.getCustomer() && !StringUtils.isEmpty(order.getCustomer().getCode())) {
list.add(criteriaBuilder.like(customerJoin.get("code").as(String.class), "%" + order.getCustomer().getCode() + "%"));
}
if (null != order.getCustomer() && !StringUtils.isEmpty(order.getCustomer().getName())) {
list.add(criteriaBuilder.like(customerJoin.get("name").as(String.class), "%" + order.getCustomer().getName() + "%"));
}
if (null != order.getProduct() && !StringUtils.isEmpty(order.getProduct().getCode())) {
list.add(criteriaBuilder.like(proJoin.get("code").as(String.class), "%" + order.getProduct().getCode() + "%"));
}
if (null != order.getProduct() && !StringUtils.isEmpty(order.getProduct().getName())) {
list.add(criteriaBuilder.like(proJoin.get("name").as(String.class), "%" + order.getProduct().getName() + "%"));
}
if (null != order.getExpress() && !StringUtils.isEmpty(order.getExpress().getCode())) {
list.add(criteriaBuilder.like(expressJoin.get("code").as(String.class), "%" + order.getExpress().getCode() + "%"));
}
if (!StringUtils.isEmpty(order.getCode())) {
list.add(criteriaBuilder.like(root.get("code").as(String.class), "%" + order.getCode() + "%"));
}
if (null != order.getCreateDate()) {
list.add(criteriaBuilder.lessThan(root.get("createDate").as(Date.class), order.getCreateDate()));
}
Predicate[] p = new Predicate[list.size()];
return criteriaBuilder.and(list.toArray(p));
}
};
return orderDao.findAll(specification, pageable);
}
智能推薦
Springboot Hibernate+JPA 實現多條件動態查詢
首先需要在數據Repository接口中繼承JpaRepository,之后還需要繼承JpaSpecificationExecutor即可使用動態條件查詢 持久層: 業務層需要Specification類,重寫toPredicate方法,這里使用了lambda表達式: Controller控制層使用get請求傳遞參數,使用對象接收。 如果需要分頁的話,在findAll條件參數后,可以再加分頁參數P...
SpringData JPA分頁查詢
首先我們需要知道SpringData JPA 的幾個接口 其實看名字就大概懂了,也可以很方便的使用 首先我們的持久化層繼承JpaRepository,相當于繼承了增刪改查的持久化層以及分頁查詢的持久化層 所以如果我們要使用分頁查詢 ,我們只需要直接調用 由一開始的圖也可以看到Pageable的其中一個實現,直接新建一個對象傳給你的持久層即可 下面是一個小Demo的例子 下面是返回結果:...
Spring Data Jpa條件分頁查詢(簡單查詢)
Spring Data Jpa條件分頁查詢(簡單查詢) 實體類 dao Controller接口 測試結果 實體類 對應數據庫 dao Controller接口 測試結果 [1]: http://meta.math.stackexchange.com/questions/5020/mathjax-basic-tutorial-and-quick-reference [2]: https://mer...
JPA分頁
常用方法 基本分頁單元 場景一 1. Controller部分 只傳遞數據內容tableData.getContent(),否則前臺分頁模版不能解析。tableData還包括分頁信息 2. DAO部分 sql為原生sql,pageable為分頁對象,map為查詢參數 查詢參數 紅線為模糊查詢 3. 工具類 其中orderBy和total方法為工具類 4. 工具類源碼...
Jpa多表關聯查詢
準備 一對多關系 需求:通過ID,查詢學生信息,然后通過學生查詢對應的成績信息 --實體類 –測試代碼 多對一關系 需求:通過ID,查詢成績信息,然后通過成績查詢對應的學生信息 –實體類 –測試代碼 一對一關系 需求:通過ID查詢學生身份信息,在通過學生身份查詢學生信息 --學生身份實體類 –測試代碼 多對多關系 需求:通過ID,查詢學生信息,然后通...
猜你喜歡
Spring Data JPA多表查詢
多表查詢在Spring Data JPA中有兩種實現方式 第一種創建一個結果集接口來接收多表連查的結果 第二種利用JPA的關聯映射來實現 先來熟悉一下幾個注解 注解 意思 屬性 @ManyToOne 定義了連接表之間的多對一的關系。 targetEntity屬性表示關聯的實體類型,可省略,fetch屬性表示加載策略,FetchType的取值范圍LAZY(延遲加載)默認上EAGER,cascade屬...
Spring JPA的多表查詢
SpringBoot JPA的多表查詢 1.關系映射 單項多對一關聯 我們以User和Role表為例 修改User實體類,添加Role對象 @ManyToOne注解映射多對一關聯,targetEntity屬性表示關聯實體類型,可以省略 @JoinColumn注解映射的外鍵字段,如果不指定,則生成一張新表維護兩個對象之間的關系 2.創建UserRepository接口 3.編寫測試類即可 4.雙向一...
spring-data-jpa 動態條件 分頁查詢
pom.xml配置 數據庫連接配置 實體類user ,省去部分字段及set、get方法 dao層用UserRepository userService層用 UserService usercontroller 測試 Postman 測試結果 ...
3D游戲編程與設計——游戲對象與圖形基礎章節作業與練習
3D游戲編程與設計——游戲對象與圖形基礎章節作業與練習 3D游戲編程與設計——游戲對象與圖形基礎章節作業與練習 自學資源 作業內容 1、基本操作演練【建議做】 天空盒的制作: 地圖的制作: 整體效果: 2、編程實踐 項目要求: 項目結構: 代碼詳解: Actions: ISSActionCallback.cs SSAction.cs SSAction...