Spring Data JPA 写SQL语句也可以如此简单
发布日期:2021-06-30 17:38:24
浏览次数:4
分类:技术文章
本文共 2372 字,大约阅读时间需要 7 分钟。
在使用 Spring Data 的时候,通常我们只需要继承 JpaRepository 就能获得大部分常用的增删改查的方法。有时候我们需要自定义一些查询方法,可以写自定义 HQL 语句
像这样
/** * 根据关注者id查找所有记录(查找关注的人的id) * * @param fromUserId * @return */ @Query("select toUserId from Relationship where fromUserId =:fromUserId") ListfindByFromUserId(@Param("fromUserId") Long fromUserId);
但是,有时候一些查询比较复杂,当我们把 SQL 语句写好了,却不知道如何转成 HQL 语句,怎么办?
很简单,Spring Data 其实也支持自定义 SQL 语句查询。
比如,我们这里写了一条稍微复杂一些的 SQL 语句。
SELECT DISTINCT t1.from_user_id FROM(SELECT * FROM relationship WHERE to_user_id = 1) AS t1INNER JOIN relationship t2 ON t1.from_user_id = t2.to_user_id
这段 SQL 语句的作用是 查询id=1的用户的互相关注的用户的id。
如何让 帮我们查询呢?
我们查看文档:
发现,只需要在后面加一个 nativeQuery = true 就行,哇,是不是很简单!
赶紧试试。
@Query(value = "SELECT DISTINCT t1.to_user_id FROM (SELECT * FROM relationship WHERE from_user_id = ?1) AS t1 INNER JOIN relationship t2 ON t1.to_user_id = t2.from_user_id ", nativeQuery = true)ListfindFriendsByUserId(Long userId);
写个测试方法
package com.liuyanzhao.forum.repository;import com.liuyanzhao.forum.entity.Relationship;import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;import java.util.List;/** * @author 言曌 * @date 2018/4/24 下午9:56 */@SpringBootTest@RunWith(SpringRunner.class)public class RelationshipRepositoryTest { @Autowired private RelationshipRepository relationshipRepository; @Before public void save() { relationshipRepository.save(new Relationship(1L,2L)); relationshipRepository.save(new Relationship(1L,3L)); relationshipRepository.save(new Relationship(1L,4L)); relationshipRepository.save(new Relationship(2L,1L)); relationshipRepository.save(new Relationship(2L,4L)); relationshipRepository.save(new Relationship(3L,1L)); } @Test public void findFriendsByUserId() throws Exception { Listids = relationshipRepository.findFriendsByUserId(1L); System.out.println(ids); }}
最终查得结果 [2,3],答案正确
有坑
我们上面查到是[2,3],这两个数都是 bigint 类型的,如果我们将这两个数传参到其他方法里,比如
/** * 根据id集合查询用户 * * @param ids * @return */ ListfindByIdIn(List ids);
会出现参数类型不匹配。
所以,这里我们可以把 id 的 Long 类型改成 Integer 类型,数据表里的 bigint 改成int
文档直达:
原文地址:
转载地址:https://lemonstone.blog.csdn.net/article/details/83110805 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
做的很好,不错不错
[***.243.131.199]2024年04月11日 08时36分09秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
【matlab】显示图片且下方显示文字
2019-04-30
关于greater<int>以及类模板的一些理解
2019-04-30
对于时间复杂度的通俗理解
2019-04-30
如何输入多组数据并输出每组数据的和?
2019-04-30
基于CentOS 7的Linux常用命令行命令
2019-04-30
行阶梯型矩阵
2019-04-30
信号量机制
2019-04-30
临界资源与临界区
2019-04-30
matlab中uint8,double,im2double和im2uint8的区别
2019-04-30
数字图像处理总复习
2019-04-30
图像去噪(包含修正的阿尔法均值滤波及通用滤波方法代码)
2019-04-30
SVM进行人脸检测
2019-04-30
C++学习笔记
2019-04-30
图像处理学习笔记
2019-04-30
Machine Learning Onramp on MATLAB 学习笔记
2019-04-30
Machine Learning with MATLAB 1.1 to 2.2
2019-04-30
Learning DSP with MATLAB
2019-04-30
用MATLAB实现m序列的生成(MATLAB 2021a适用)
2019-04-30
MATLAB函数备忘(定期更新)
2019-04-30
13行MATLAB代码实现网络爬虫 爬取NASA画廊星图
2019-04-30