【Spring Boot + Kotlin 实战教程】Spring Data JPA 多表关联查询 映射到 Dto 的方法

简介: 【Spring Boot + Kotlin 实战教程】Spring Data JPA 多表关联查询 映射到 Dto 的方法TechArticlepackage com.

【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>

}

完整的工程示例源代码:

https://github.com/AK-47-D/cms/tree/picture_2017.11.11

相关文章
|
1月前
|
XML Java 数据库连接
spring boot 参数的过滤注解与实战
在Spring Boot应用中,对于入参的过滤,通常会涉及到对Web层的数据验证和处理。Spring Boot借助Spring框架提供了强大的验证框架支持,主要基于JSR-303/JSR-380(Bean Validation API)规范,以及Spring自身的@Valid或@Validated注解来实现请求参数的验证。以下是一些常见的使用案例来展示如何对参数进行过滤和验证。
29 1
|
1月前
|
Java Spring 容器
【二十二】springboot整合拦截器实战并对比过滤器
【二十二】springboot整合拦截器实战并对比过滤器
31 0
|
1月前
|
存储 安全 Java
Spring Boot整合Spring Security--学习笔记
Spring Boot整合Spring Security--学习笔记
53 0
|
1月前
|
消息中间件 Cloud Native Java
【Spring云原生系列】SpringBoot+Spring Cloud Stream:消息驱动架构(MDA)解析,实现异步处理与解耦合
【Spring云原生系列】SpringBoot+Spring Cloud Stream:消息驱动架构(MDA)解析,实现异步处理与解耦合
|
16天前
|
安全 数据安全/隐私保护
Springboot+Spring security +jwt认证+动态授权
Springboot+Spring security +jwt认证+动态授权
|
1月前
|
消息中间件 JSON Java
Spring Boot、Spring Cloud与Spring Cloud Alibaba版本对应关系
Spring Boot、Spring Cloud与Spring Cloud Alibaba版本对应关系
480 0
|
16天前
|
Java 容器
SpringBoot使用配置注解开启自动配置功能&整合spring-boot-configuration-processor
SpringBoot使用配置注解开启自动配置功能&整合spring-boot-configuration-processor
14 0
|
1月前
|
存储 缓存 安全
Spring Boot从入门到实战
本课程从SpringBoot的最基础的安装、配置开始到SpringBoot的日志管理、Web业务开发、数据存储、数据缓存,安全控制及相关企业级应用,全程案例贯穿,案例每一步的都会讲解实现思路,全程手敲代码实现。让你不仅能够掌SpringBoot的应用,还能了解背后的原理,学习完本课程后,能够让你动手独立完成一个中小型的SpringBoot Web应用开发。
19 1
Spring Boot从入门到实战
|
1月前
|
缓存 NoSQL Java
spring cache整合redis实现springboot项目中的缓存功能
spring cache整合redis实现springboot项目中的缓存功能
46 1
|
1月前
|
前端开发 Java Spring
Java 新手如何使用Spring MVC 中的查询字符串和查询参数
Java 新手如何使用Spring MVC 中的查询字符串和查询参数