spring data jpa如何只查詢實體部分字段
需求
現(xiàn)在有一張article表,用來儲存文章,對應(yīng)的實體類如下:
package com.qianyucc.blog.model; import lombok.*; import javax.persistence.*; /** * @author lijing * @date 2019-08-05 14:28 * @description 文章 */ @Data @Entity @Table(name = "article") public class Article { @Id // 主鍵自增 @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name = "author",unique = false,nullable = false,length = 20) private String author; @Column(name = "title",length = 100) private String title; @Column(name = "content",columnDefinition = "clob not null") private String content; @Column(name = "tags",length = 50) private String tags; @Column(name = "type") private Integer type; @Column(name = "categories",length = 50) private String categories; @Column(name = "gmt_create") private Long gmtCreate; @Column(name = "gmt_update") private Long gmtUpdate; @Column(name = "tabloid") private String tabloid; @Column(name = "likes") private Integer likes; @Column(name = "views") private Integer views; }
現(xiàn)在需要查詢文章的所有分類,也就是categories屬性
解決方法
網(wǎng)上的一些方法分別是重寫構(gòu)造器、或者自定義接口作為返回類型,但是我試了后都不能很好的解決問題。下面提供一種方法,親測可以實現(xiàn)上面的需求。
一個字段的情況
Controler:
package com.qianyucc.blog.controller; /** * @author lijing * @date 2019-08-05 15:13 * @description */ @RestController public class ArticleController { @Autowired private ArticleRepositoryarticleRepository; @GetMapping("/getAllCategories") public Object getAllCategories(){ return articleRepository.getAllCategories(); } }
Repository:(這里省略Service層)
package com.qianyucc.blog.repository; import com.qianyucc.blog.model.*; import org.springframework.data.jpa.repository.*; import java.util.*; /** * @author lijing * @date 2019-08-05 14:28 * @description 文章數(shù)據(jù)庫訪問層 */ public interface ArticleRepository extends JpaRepository<Article,Long>,JpaSpecificationExecutor<Article> { @Query(value = "select distinct categories from article",nativeQuery = true) // 這里注意返回值用String類型接收 List<String> findAllCategories(); }
上面的nativeQuery屬性設(shè)置為true的時候可以使用SQL語句。
測試結(jié)果:
控制臺打?。?/p>
多個字段的情況
只需修改Repository,注意現(xiàn)在的返回值為List<Map<String,Object>>
public interface ArticleRepository extends JpaRepository<Article,Long>,JpaSpecificationExecutor<Article> { @Query(value = "select author,categories from article",nativeQuery = true) List<Map<String,Object>> findAllCategories(); }
測試結(jié)果
控制臺打印
JPA查詢部分字段的相關(guān)事項
JPA使用HQL查詢部分字段出錯:
org.hibernate.hql.internal.ast.QuerySyntaxException: XXX is not mapped
解決:
應(yīng)該@Entity指定name名,name值為對應(yīng)表名,同@Table的name值相同
使用HQL的注意:
1.想要使用JPA查詢部分信息,需要使用HQL
2.select需跟實體,可以是map(必須是小寫,大寫試了下報錯),或者是將待查詢的字段單獨封裝成一個實體,new 實體
3.查詢的字段中需要指定as別名,否則得到的map結(jié)果集中,key值默認是"0",“1”,“2”…數(shù)字
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
基于parameters參數(shù)實現(xiàn)參數(shù)化過程解析
這篇文章主要介紹了基于parameters參數(shù)實現(xiàn)參數(shù)化過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08SpringBoot @PostConstruct原理用法解析
這篇文章主要介紹了SpringBoot @PostConstruct原理用法解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08SwiftUI中級List如何添加新內(nèi)容(2020年教程)
這篇文章主要介紹了SwiftUI中級List如何添加新內(nèi)容,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01