Spring Data JPA 写SQL语句也可以如此简单
2023-06-13 09:14:30 时间
在使用 Spring Data JPA 的时候,通常我们只需要继承 JpaRepository 就能获得大部分常用的增删改查的方法。有时候我们需要自定义一些查询方法,可以写自定义 HQL 语句
像这样
/**
* 根据关注者id查找所有记录(查找关注的人的id)
*
* @param fromUserId
* @return
*/
@Query("select toUserId from Relationship where fromUserId =:fromUserId")
List<Long> findByFromUserId(@Param("fromUserId") Long fromUserId);
但是,有时候一些查询比较复杂,当我们把 SQL 语句写好了,却不知道如何转成 HQL 语句,怎么办?
很简单,Spring Data JPA 其实也支持自定义 SQL 语句查询。
比如,我们这里写了一条稍微复杂一些的 SQL 语句。
SELECT DISTINCT t1.from_user_id FROM
(SELECT * FROM relationship WHERE to_user_id = 1) AS t1
INNER JOIN relationship t2 ON t1.from_user_id = t2.to_user_id
这段 SQL 语句的作用是 查询id=1的用户的互相关注的用户的id。
如何让 JPA 帮我们查询呢?
只需要在后面加一个 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)
List<Long> findFriendsByUserId(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 {
List<Long> ids = relationshipRepository.findFriendsByUserId(1L);
System.out.println(ids);
}
}
最终查得结果 [2,3],答案正确
有坑
我们上面查到是[2,3],这两个数都是 bigint 类型的,如果我们将这两个数传参到其他方法里,比如
/**
* 根据id集合查询用户
*
* @param ids
* @return
*/
List<User> findByIdIn(List<Long> ids);
会出现参数类型不匹配。
所以,这里我们可以把 id 的 Long 类型改成 Integer 类型,数据表里的 bigint 改成int
原文地址:https://liuyanzhao.com/8069.html
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/180064.html原文链接:https://javaforall.cn
相关文章
- springioc和aop原理_描述spring框架的工作原理
- 原生AspectJ用法分析以及Spring-AOP原理分析
- Spring Boot拓展注解@SpringBootApplication和@Configuration
- Spring Cloud Task 任务执行-命令行启动任务
- SQL开发知识:Sql中存储过程的定义、修改和删除操作
- Sql存储过程游标循环的用法及sql如何使用cursor写一个简单的循环
- Spring Boot2.0之整合事物管理详解编程语言
- spring Boot(十九):使用Spring Boot Actuator监控应用详解编程语言
- Spring整合Redis简单实现高效缓存(spring集成redis)
- pl/sql探索Oracle数据库:SQL PL/SQL(oraclesql)
- MongoDB实现“SQL NoSQL”合一(mongodb支持sql)
- 用Oracle动态SQL轻松解决复杂问题(oracle动态sql)
- 使用MySQL执行SQL指令:简单易学的基础教程(mysql运行sql命令)
- SQL如何转换成MySQL,易学易用(sql转换成mysql)
- MySQL 嵌入式 SQL 的特点和应用场景分析(mysql嵌入式sql)
- SQL与Oracle在数据库中的区别(sql和oracle区别)
- 如何在Linux上安装SQL:简单易懂的教程(linux安装sql)
- MySQL: SQL日志分析与运维(mysql sql 日志)
- 运行Oracle终止SQL运行:收尾全搞定(oracle终止sql)
- SQLserver如何快速导入SQL文件(sqlserver导入sql文件)
- MySQL如何还原SQL文件(mysql还原sql文件)
- Oracle如何利用SQL快速写出时间表达式(oracle写时间sql)
- Oracle SQL替换指南简单操作轻松有效实现(oracle中sql替换)
- 使用Oracle SQL处理文本数据的方法(oracle sql文本)
- Oracle SQL报告精准信息助力数据分析(oracle sql报告)
- Oracle SQL查询速度缓慢,何去何从(oracle sql太慢)
- Sql学习第四天——SQL关于withcube,withrollup和grouping解释及演示