JPA+QueryDSL
工作需要,接觸到了QueryDSL,總結使用方法,參考了幾個大佬的文章,對我幫助很大 參考文章1,參考文章2,參考文章3,參考文章4感謝大佬們!!!
使用場景
JPA,JDO,SQL,Java,Collections,RDF,Lucene,Hibernate Search。
QueryDSL官網:http://querydsl.com/static/querydsl/4.1.3/reference/html_single/
一些概念
在 JPA 規范中, EntityManager 是完成持久化操作的核心對象。實體作為普通 Java 對象,只有在調用 EntityManager 將其持久化后才會變成持久化對象。
EntityManager 對象在一組實體類與底層數據源之間進行 O/R 映射的管理。它可以用來管理和更新 Entity Bean, 根椐主鍵查找 Entity Bean, 還可以通過JPQL語句查詢實體。
Persistence context是由一組受托管的實體對象實例所構成的集合。它受entity manager 的管理。Entity manager追蹤persistence context中所有對象的修改和更新情況,并根據指定的flush模式(本章稍后會做討論)將這些修改保存到數據庫中。一旦persistence context被關閉,所有實體對象實例都會脫離EntityManager而成為非托管對象。對象一旦從persistence context中脫離,就不再受entity manager管理了,任何對此對象的狀態變更也將不會被同步到數據庫。
使用方法
pom文件的導入
<!-- queryDSL --><dependency><groupId>com.querydsl</groupId><artifactId>querydsl-jpa</artifactId></dependency><!-- queryDSL --><dependency><groupId>com.querydsl</groupId><artifactId>querydsl-apt</artifactId><scope>provided</scope></dependency><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin><!--因為是類型安全的,所以還需要加上Maven APT plugin,使用 APT 自動生成一些類:--><plugin><groupId>com.mysema.maven</groupId><artifactId>apt-maven-plugin</artifactId><version>1.1.3</version><executions><execution><phase>generate-sources</phase><goals><goal>process</goal></goals><configuration><outputDirectory>target/generated-sources</outputDirectory><processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor></configuration></execution></executions></plugin></plugins> </build>下面是兩個大佬的文章里給出的例子,根據例子學習非常快,再次感謝大佬們!!!
例1
IDEA 的Maven面板,項目生命周期里重新編譯項目,會自動生成Q實體類
例2
一. 實體類
二. 單表動態分頁查詢
Spring Data JPA中提供了QueryDslPredicateExecutor接口,用于支持QueryDSL的查詢操作
public interface tCityRepository extends JpaRepository<TCity, Integer>, QueryDslPredicateExecutor<TCity> {}這樣的話單表動態查詢就可以參考如下代碼:
//查找出Id小于3,并且名稱帶有`shanghai`的記錄. //動態條件 QTCity qtCity = QTCity.tCity; //SDL實體類 //該Predicate為querydsl下的類,支持嵌套組裝復雜查詢條件 Predicate predicate = qtCity.id.longValue().lt(3).and(qtCity.name.like("shanghai")); //分頁排序 Sort sort = new Sort(new Sort.Order(Sort.Direction.ASC,"id")); PageRequest pageRequest = new PageRequest(0,10,sort); //查找結果 Page<TCity> tCityPage = tCityRepository.findAll(predicate,pageRequest);三. 多表動態查詢
QueryDSL對多表查詢提供了一個很好地封裝,看下面代碼:
/** * 關聯查詢示例,查詢出城市和對應的旅店 * @param predicate 查詢條件 * @return 查詢實體 */ @Override public List<Tuple> findCityAndHotel(Predicate predicate) { JPAQueryFactory queryFactory = new JPAQueryFactory(em); JPAQuery<Tuple> jpaQuery = queryFactory.select(QTCity.tCity,QTHotel.tHotel) .from(QTCity.tCity) .leftJoin(QTHotel.tHotel) .on(QTHotel.tHotel.city.longValue().eq(QTCity.tCity.id.longValue())); //添加查詢條件 jpaQuery.where(predicate); //拿到結果 return jpaQuery.fetch(); }城市表左連接旅店表,當該旅店屬于這個城市時查詢出兩者的詳細字段,存放到一個Tuple的多元組中.相比原生sql,簡單清晰了很多.
那么該怎么調用這個方法呢?
這樣做的話避免了返回Object[]數組,下面是自動生成的sql語句:
select tcity0_.id as id1_0_0_, thotel1_.id as id1_1_1_, tcity0_.country as country2_0_0_, tcity0_.map as map3_0_0_, tcity0_.name as name4_0_0_, tcity0_.state as state5_0_0_, thotel1_.address as address2_1_1_, thotel1_.city as city3_1_1_, thotel1_.name as name4_1_1_ from t_city tcity0_ left outer join t_hotel thotel1_ on ( cast(thotel1_.city as signed)=cast(tcity0_.id as signed) ) where tcity0_.name like ? escape '!'四 多表動態分頁查詢
分頁查詢對于queryDSL無論什么樣的sql只需要寫一遍,會自動轉換為相應的count查詢,下面代碼是對上面的查詢加上分頁功能:
@Override public QueryResults<Tuple> findCityAndHotelPage(Predicate predicate,Pageable pageable) { JPAQueryFactory queryFactory = new JPAQueryFactory(em); JPAQuery<Tuple> jpaQuery = queryFactory.select(QTCity.tCity.id,QTHotel.tHotel) .from(QTCity.tCity) .leftJoin(QTHotel.tHotel) .on(QTHotel.tHotel.city.longValue().eq(QTCity.tCity.id.longValue())) .where(predicate) .offset(pageable.getOffset()) .limit(pageable.getPageSize()); //拿到分頁結果 return jpaQuery.fetchResults(); }和上面不同之處在于這里使用了offset和limit限制查詢結果,并且返回一個QueryResults。該類會自動實現count查詢和結果查詢,并進行封裝。
調用形式如下:
生成的原生count查詢sql,當該count查詢結果為0的話,則直接返回,并不會再進行具體數據查詢:
select count(tcity0_.id) as col_0_0_ from t_city tcity0_ left outer join t_hotel thotel1_ on ( cast(thotel1_.city as signed)=cast(tcity0_.id as signed) ) where tcity0_.name like ? escape '!'生成的原生查詢sql:
select tcity0_.id as id1_0_0_, thotel1_.id as id1_1_1_, tcity0_.country as country2_0_0_, tcity0_.map as map3_0_0_, tcity0_.name as name4_0_0_, tcity0_.state as state5_0_0_, thotel1_.address as address2_1_1_, thotel1_.city as city3_1_1_, thotel1_.name as name4_1_1_ from t_city tcity0_ left outer join t_hotel thotel1_ on ( cast(thotel1_.city as signed)=cast(tcity0_.id as signed) ) where tcity0_.name like ? escape '!' limit ?查看打印,可以發現對應的city也都是同一個對象,hotel是不同的對象。
總結
以上是生活随笔為你收集整理的JPA+QueryDSL的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android下常见的内存泄露 经典
- 下一篇: Winform datagridview