【Spring Boot + Kotlin 实战教程】Spring Data JPA 多表关联查询 映射到 Dto 的方法
【Spring Boot + Kotlin 实战教程】Spring Data JPA 多表关联查询 映射到 Dto 的方法
TechArticle
package com.ak47.cms.cms.entity import java.util.* import javax.persistence.* @Entity @Table(indexes = arrayOf( Index(name = "idx_url", unique = true, columnList = "url"))) open class TechArticle { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) var id: Long = -1 var url = "URL" var title = "Kotlin 使用 Spring WebFlux 实现响应式编程" @Lob var simpleContent = "文章摘要" @Lob var showContent = "文章内容" // TechArticleTag 表中的 tagId var tagId = -1 var category = "编程语言" var gmtCreate = Date() var gmtModified = Date() }
TechArticleTag
package com.ak47.cms.cms.entity import java.util.* import javax.persistence.Entity import javax.persistence.GeneratedValue import javax.persistence.GenerationType import javax.persistence.Id @Entity class TechArticleTag { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) var id: Long = -1 var tagId = -1 // 文章标签:例如 Kotlin,Java,Spring Boot 等 var tagDetail = "Kotlin" var gmtCreate = Date() var gmtModified = Date() }
DTO 定义类: TechArticleDto
package com.ak47.cms.cms.dto import com.ak47.cms.cms.entity.TechArticle import java.util.* class TechArticleDto : TechArticle { var tagDetail = "" constructor( id: Long, url: String, title: String, simpleContent: String, showContent: String, tagId: Int, tagDetail: String, gmtCreate: Date, gmtModified: Date ) : super() { this.id = id this.url = url this.title = title this.simpleContent = simpleContent this.showContent = showContent this.tagId = tagId this.tagDetail = tagDetail this.gmtCreate = gmtCreate this.gmtModified = gmtModified } }
JPQL 语句的写法
package com.ak47.cms.cms.dao import com.ak47.cms.cms.dto.TechArticleDto import com.ak47.cms.cms.entity.TechArticle import org.springframework.data.domain.Page import org.springframework.data.domain.Pageable import org.springframework.data.jpa.repository.JpaRepository import org.springframework.data.jpa.repository.Query import org.springframework.data.repository.query.Param interface TechArticleRepository : JpaRepository<TechArticle, Long> { @Query("select count(*) from #{#entityName} a where a.url = :url") fun countByUrl(@Param("url") url: String): Int @Query("select new com.ak47.cms.cms.dto.TechArticleDto( a.id, a.url, a.title, a.simpleContent, a.showContent , a.tagId, b.tagDetail , a.gmtCreate, a.gmtModified ) " + "from TechArticle a, TechArticleTag b where a.tagId = b.tagId") fun listTechArticleDto(page: Pageable): Page<TechArticleDto> @Query("select new com.ak47.cms.cms.dto.TechArticleDto( a.id, a.url, a.title, a.simpleContent, a.showContent , a.tagId, b.tagDetail , a.gmtCreate, a.gmtModified ) " + "from TechArticle a left join TechArticleTag b on a.tagId = b.tagId where a.title like %:searchText% or a.showContent like %:searchText% ") fun searchTechArticleDto(page: Pageable, @Param("searchText") searchText: String): Page<TechArticleDto> }
完整的工程示例源代码:
相关推荐
Nostalgiachild 2020-11-13
拓网科技 2020-11-13
susmote 2020-09-30
FrankAbagnale 2020-09-15
DanielMan 2020-09-02
hanqiusy 2020-08-26
Khellendros 2020-08-20
带你装逼带你飞 2020-07-20
星辰 2020-06-27
hohohogetitup 2020-06-19
NightWish 2020-06-11
Khellendros 2020-06-01
hohohogetitup 2020-05-31
Stranger 2020-05-30
ilovewqf 2020-05-30
ncuboy0wsq 2020-05-17
Stranger 2020-05-16
wuShiJingZuo 2020-04-26