SpringDataJpa默認查詢都是查詢全部的字段,如果只想查詢指定的幾個字段可以采用如下方式查詢。
<br/>
**1. hsql + 指定構造器查詢**
```java
public class Student {
//必須提供對應的構造器
public Student(String name, Integer age) {
this.name = name;
this.age = age;
}
}
```
```java
/**
* hsql查詢,實體類接收
*/
@Query("select new com.learn.springdatajpa01.pojo.Student(student.name, student.age) from Student student")
List<Student> findAll01();
```
**2. hsql查詢,Map接收**
```java
//必須使用as重命名字段
@Query("select student.name as name, student.age as age from Student student")
List<Map<String, Object>> findAll02();
```
**3. 原生SQL查詢,實體類接收**
```java
/**
* 1. 必須寫全實體類Student的所有屬性,不需要查詢的字段給一個默認值,比如 ''。
* 2. 如果Student實體類中還存在其他的實體類,這個查詢方式就不適用了。
*/
@Query(value = "select '' as id, '' as sex, '' as total, name, age from student", nativeQuery = true)
List<Student> findAll03();
```
**4. 原生SQL查詢,Map接收**
```java
@Query(value = "select name, age from student", nativeQuery = true)
List<Map<String, Object>> findAll04();
```
- MapStruct屬性映射
- MapStruct是什么
- maven依賴
- 基本映射
- 字段名不一致的映射
- 字段類型不一致的映射
- 基本數據類型轉換
- 日期格式轉換
- 使用表達式轉換
- 枚舉映射
- 多個源類的映射
- 集合的映射
- 添加自定義映射方法
- 映射前后
- 添加默認值
- 映射異常處理
- SpringDataJPA
- SpringDataJPA是什么
- 與JPA、Hibernate的關系
- 環境搭建
- 簡單CURD操作
- 內部原理
- 主鍵生成策略
- 聯合主鍵
- 查詢方式
- 方法命名規則查詢
- 限制查詢結果查詢
- 注解@Query查詢
- 命名參數查詢
- SpEL表達式查詢
- 原生查詢
- 更新與刪除
- Specification動態查詢
- 核心接口
- 查詢例子
- 分頁查詢與排序
- 多表查詢
- 一對一查詢
- 一對多查詢
- 多對多查詢
- 注意事項
- Specification多表查詢
- @Query多表查詢
- 只查詢指定字段
- 級聯操作
- 加載規則