spring data jpa如何只查詢實(shí)體部分字段
現(xiàn)在有一張article表,用來(lái)儲(chǔ)存文章,對(duì)應(yīng)的實(shí)體類如下:
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)上的一些方法分別是重寫(xiě)構(gòu)造器、或者自定義接口作為返回類型,但是我試了后都不能很好的解決問(wèn)題。下面提供一種方法,親測(cè)可以實(shí)現(xiàn)上面的需求。
一個(gè)字段的情況Controler:
package com.qianyucc.blog.controller;/** * @author lijing * @date 2019-08-05 15:13 * @description */@RestControllerpublic 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ù)庫(kù)訪問(wèn)層 */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的時(shí)候可以使用SQL語(yǔ)句。
測(cè)試結(jié)果:

控制臺(tái)打印:

只需修改Repository,注意現(xiàn)在的返回值為L(zhǎng)ist<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();}
測(cè)試結(jié)果

控制臺(tái)打印

JPA使用HQL查詢部分字段出錯(cuò):
org.hibernate.hql.internal.ast.QuerySyntaxException: XXX is not mapped
解決:應(yīng)該@Entity指定name名,name值為對(duì)應(yīng)表名,同@Table的name值相同
使用HQL的注意:
1.想要使用JPA查詢部分信息,需要使用HQL
2.select需跟實(shí)體,可以是map(必須是小寫(xiě),大寫(xiě)試了下報(bào)錯(cuò)),或者是將待查詢的字段單獨(dú)封裝成一個(gè)實(shí)體,new 實(shí)體
3.查詢的字段中需要指定as別名,否則得到的map結(jié)果集中,key值默認(rèn)是'0',“1”,“2”…數(shù)字
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 編程語(yǔ)言PHP在Web開(kāi)發(fā)領(lǐng)域的優(yōu)勢(shì)在哪?2. 詳解Android studio 動(dòng)態(tài)fragment的用法3. 什么是python的自省4. Android如何加載Base64編碼格式圖片5. Spring Boot和Thymeleaf整合結(jié)合JPA實(shí)現(xiàn)分頁(yè)效果(實(shí)例代碼)6. 基于android studio的layout的xml文件的創(chuàng)建方式7. 圖文詳解vue中proto文件的函數(shù)調(diào)用8. Vuex localStorage的具體使用9. 解決Android studio xml界面無(wú)法預(yù)覽問(wèn)題10. 在IDEA中實(shí)現(xiàn)同時(shí)運(yùn)行2個(gè)相同的java程序

網(wǎng)公網(wǎng)安備