Spring?Data?JPA?映射VO/DTO對象方式
Spring Data JPA 映射VO/DTO對象
在項目開發(fā)中,時常需要根據(jù)業(yè)務(wù)需求來映射VO/DTO對象(這兩個概念理解感覺很模糊- 。- ),本文將簡單介紹以Spring Data JPA的方式處理實體類映射
HQL方式
public interface MusicTypeRepository extends JpaRepository<MusicType,Integer> { @Query("select new cn.srblog.springbootcurd.vo.StudentTypeInfoVo(count(s.id),m.name) " + "FROM MusicType m left JOIN Student s on s.musicTypeId = m.id group by m.id ") List<StudentTypeInfoVo> getTypeInfo(); }
- 填寫實體類路徑,構(gòu)造參數(shù)順序要一致,字段名一律為實體類中的屬性
- 如果配置了實體類屬性的映射關(guān)系,則on s.musicTypeId = m.id語句可以省略
VO實體類
@Value public class StudentTypeInfoVo { private Long count; private String name; }
使用Lombok的 @Value 注解
- 默認(rèn)生成帶參構(gòu)造方法
- 默認(rèn)為成員變量添加final修飾,且只提供getter()方法
原生SQL的形式
接口形式
public interface CoursePlanRepository extends JpaRepository<CoursePlan,Integer> { @Query(nativeQuery = true,value = "SELECT " + " c.id as id," + "DAYOFWEEK(c.start_time) as week," + "m.name as musicType," + "t.name as teacherName," + "c.start_time as startTime," + "c.end_time as endTime " + " FROM t_courseplan c,t_musictype m , t_teacher t " + " WHERE DATE(c.start_time) < DATE_ADD(CURDATE(), INTERVAL 7 DAY ) AND CURDATE() <= DATE(c.start_time) " + " and t.id=c.tea_id and c.music_type_id = m.id order by c.start_time ") List<CoursePlanVos> getWeekList(); }
- nativeQuery = true 表示開啟原生SQL查詢
- 查詢字段別名需要與實體類中字段一一對應(yīng)
- 該方法功能為查詢一周后的數(shù)據(jù)
函數(shù) | 說明 |
---|---|
DAYOFWEEK() | DAYOFWEEK函數(shù)返回日期的工作日索引值,即星期日為1,星期一為2,星期六為7。例:DAYOFWEEK('2019-05-09') 返回 5 |
DATE() | 提取日期或日期/時間表達(dá)式的日期部分,格式'YYYY-MM-DD'或者'YYYYMMDD' |
DATE_ADD(date,INTERVAL expr unit) | 給日期添加指定的時間間隔。date 參數(shù)是合法的日期表達(dá)式,expr 參數(shù)是您希望添加的時間間隔,type 參數(shù)可以是MySQL支持的時間日期相關(guān)類型值 |
CURDATE() | 返回當(dāng)前日期 例:'2019-05-09' |
VO實體類(接口形式)
public interface CoursePlanVos{ Integer getId(); Integer getWeek(); String getMusicType(); String getTeacherName(); Date getStartTime() ; Date getEndTime(); }
結(jié)果集形式
@Query(value = "select count(s.id) as count,m.name as name " + " FROM t_musictype m left JOIN t_student s on s.music_type_id = m.id group by m.id ",nativeQuery = true) List<Object[]> listType1();
對比第一種方法,使用原生SQL默認(rèn)會返回Object數(shù)組
Spring Data Jpa 自定義repository轉(zhuǎn)DTO
近期項目中需要 關(guān)聯(lián) 幾張表再把字段轉(zhuǎn)出來,在這里記錄以下,我感覺網(wǎng)上寫的都不太規(guī)范和清晰。
@Entity @SqlResultSetMapping( name="TestMapping", entities = { @EntityResult( entityClass = com.xxx.xx.data.model.TestEntity.class, fields = { @FieldResult(name="id",column="id"), @FieldResult(name="localTime",column="time"), @FieldResult(name="maximumAppointment",column="maxAppointment"), } ) } ) @NamedNativeQuery(name="getTestQuery", query="select tableC.id as id,tableB.time,tableC.maximumAppointment as maxAppointment from tableB " + " inner join tableA on tableA.id = tableB.tableAId " + " inner join tableC on tableB.id = tableC.tableBId " + " inner join custom on custom.id = tableA.customId " + "where " + " tableA.locationId = :locationId" + " and custom.id = :customId" + " and tableB.deleted = false ", resultSetMapping="TestMapping") @Data public class TestEntity { @Id private String id; private LocalTime localTime; private Integer maximumAppointment; }
需要聲明接口:
@Repository public interface TestEntityRepository extends JpaRepository<TestEntity,String> { @Query(name="getTestQuery") List<TestEntity> getTestQuery(String locationId, String customId); }
若不想聲明接口,那可以用EntityManager 來實現(xiàn)。
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Cloud Ribbon實現(xiàn)客戶端負(fù)載均衡的示例
本篇文章主要介紹了Spring Cloud Ribbon實現(xiàn)客戶端負(fù)載均衡的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02詳解Java中布隆過濾器(Bloom Filter)原理及其使用場景
布隆過濾器是1970年由布隆提出的,它實際上是一個很長的二進(jìn)制向量和一系列隨機(jī)映射函數(shù),它的作用是檢索一個元素是否存在我們的集合之中,本文給大家詳細(xì)的講解一下布隆過濾器,感興趣的同學(xué)可以參考閱讀2023-05-05Java并發(fā)編程示例(二):獲取和設(shè)置線程信息
這篇文章主要介紹了Java并發(fā)編程示例(二):獲取和設(shè)置線程信息,本文是系列文章的第二篇,本文著重講解Thread類的幾個重要屬性,需要的朋友可以參考下2014-12-12springboot日期轉(zhuǎn)換器實現(xiàn)實例解析
這篇文章主要介紹了springboot日期轉(zhuǎn)換器實現(xiàn)實例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-12-12