前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Boot 结合 spring data jpa中的 DTO 映射查询《Spring Boot 开发实战》

Spring Boot 结合 spring data jpa中的 DTO 映射查询《Spring Boot 开发实战》

作者头像
一个会写诗的程序员
发布2018-08-17 10:24:34
2.2K0
发布2018-08-17 10:24:34
举报
文章被收录于专栏:一个会写诗的程序员的博客

我们知道,在 MyBatis 中直接 DTO 映射查询的时候,写起来非常简单。只要字段对应上即可:

下面是对应的 Java 代码实例:

代码语言:javascript
复制
package com.alibaba.microtek.mapper.microtek;

import com.alibaba.microtek.dto.SlowSqlTemplateDto;
import com.alibaba.microtek.model.microtek.XxptSlowQueyInfo;
import com.taobao.pandora.pandolet.annotation.Service;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface XxptSlowQueyInfoMapper {
   
    @Select("SELECT app_name appName,sum(slow_template_count) c  FROM `xxpt_slow_quey_info` GROUP BY app_name ORDER BY c desc limit 10")
    @ResultType(SlowSqlTemplateDto.class)
    List<SlowSqlTemplateDto> listSlowSqlTemplateDto();

}



package com.alibaba.microtek.dto;

public class SlowSqlTemplateDto {
    String appName;
    Integer c;

    public String getAppName() {
        return appName;
    }

    public void setAppName(String appName) {
        this.appName = appName;
    }

    public Integer getC() {
        return c;
    }

    public void setC(Integer c) {
        this.c = c;
    }
}

在 JPA 中,稍微有点费事,但也还好。下面是一个完整的 Kotlin 代码实例

代码语言:javascript
复制
package com.slow.sql.speedo

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController

@RestController
class XxptSlowQueyInfoController {

    @Autowired
    lateinit var syncSlowSqlDataJob: SyncSlowSqlDataJob
    @Autowired
    lateinit var xxptSlowQueyInfoDao: XxptSlowQueyInfoDao

    @GetMapping("/syncSlowSqlDataJob")
    fun syncSlowSqlDataJob() {
        syncSlowSqlDataJob.sync()
    }

    @GetMapping("/listSlowSqlTemplateDto")
    fun listSlowSqlTemplateDto(): List<SlowSqlTemplateDto> {
        return xxptSlowQueyInfoDao.listSlowSqlTemplateDto()
    }

}



package com.slow.sql.speedo

import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query

interface XxptSlowQueyInfoDao : JpaRepository<XxptSlowQueyInfo, Long> {
    fun save(record: XxptSlowQueyInfo): XxptSlowQueyInfo

    @Query("""
        SELECT 
        new com.slow.sql.speedo.SlowSqlTemplateDto( x.appName , sum(x.slowTemplateCount) ) 
        FROM XxptSlowQueyInfo x 
        GROUP BY x.appName 
        ORDER BY sum(x.slowTemplateCount) desc
    """)
    fun listSlowSqlTemplateDto(): List<SlowSqlTemplateDto>
}




package com.slow.sql.speedo

import com.alibaba.fastjson.annotation.JSONField
import java.util.*
import javax.persistence.*

@Entity
@Table(name = "xxpt_slow_quey_info")
class XxptSlowQueyInfo {
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Id
    var id: Long? = null

    @JSONField(name = "app_name")
    var appName: String? = null

    var owner: String? = null

    var ops: String? = null

    @JSONField(name = "need_optimize_slow_template_count")
    var needOptimizeSlowTemplateCount: Int? = null

    @JSONField(name = "need_optimize_slow_sql_count")
    var needOptimizeSlowSqlCount: Int? = null

    @JSONField(name = "slow_template_count")
    var slowTemplateCount: Int? = null

    @JSONField(name = "slow_sql_count")
    var slowSqlCount: Int? = null

    var date: Date? = null


}

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018.08.03 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档