当前位置:网站首页>@Query 疑难杂症
@Query 疑难杂症
2022-06-26 00:33:00 【InfoQ】
快速体验 @Query 的方法
package com.example.jpa.example1;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
public interface UserDtoRepository extends JpaRepository<User,Long> {
//通过query注解根据name查询user信息
@Query("From User where name=:name")
User findByQuery(@Param("name") String nameParam);
}
package com.example.jpa.example1;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
@DataJpaTest
public class UserRepositoryQueryTest {
@Autowired
private UserDtoRepository userDtoRepository;
@Test
public void testQueryAnnotation() {
//新增一条数据方便测试 userDtoRepository.save(User.builder().name("jackxx").email("[email protected]").sex("man").address("shanghai").build());
//调用上面的方法查看结果
User user2 = userDtoRepository.findByQuery("jack");
System.out.println(user2);
}
}
Hibernate: insert into user (address, email, name, sex, version, id) values (?, ?, ?, ?, ?, ?)
Hibernate: select user0_.id as id1_0_, user0_.address as address2_0_, user0_.email as email3_0_, user0_.name as name4_0_, user0_.sex as sex5_0_, user0_.version as version6_0_ from user user0_ where user0_.name=?
User(id=1, name=jack, [email protected], version=0, sex=man, address=shanghai)
JpaQueryLookupStrategy 关键源码剖析




@Query 的基本用法
package org.springframework.data.jpa.repository;
public @interface Query {
/**
* 指定JPQL的查询语句。(nativeQuery=true的时候,是原生的Sql语句)
*/
String value() default "";
/**
* 指定count的JPQL语句,如果不指定将根据query自动生成。
* (如果当nativeQuery=true的时候,指的是原生的Sql语句)
*/
String countQuery() default "";
/**
* 根据哪个字段来count,一般默认即可。
*/
String countProjection() default "";
/**
* 默认是false,表示value里面是不是原生的sql语句
*/
boolean nativeQuery() default false;
/**
* 可以指定一个query的名字,必须唯一的。
* 如果不指定,默认的生成规则是:
* {$domainClass}.${queryMethodName}
*/
String name() default "";
/*
* 可以指定一个count的query的名字,必须唯一的。
* 如果不指定,默认的生成规则是:
* {$domainClass}.${queryMethodName}.count
*/
String countName() default "";
}
JPQL 的语法
SELECT ... FROM ...
[WHERE ...]
[GROUP BY ... [HAVING ...]]
[ORDER BY ...]
DELETE FROM ... [WHERE ...]
UPDATE ... SET ... [WHERE ...]
边栏推荐
- Redis的使用
- A lost note for konjaku beginner
- Show spirit chenzitong was invited to be the chief experience officer of the global finals of the sixth season perfect children's model
- 创建OpenGl窗口
- Easy to understand C language keyword static
- 螺旋矩阵
- 缓存技术之第一次亲密接触
- Data analysis - similarities and differences between C-end and b-end data analysis
- Differences and functions of export set env in makefile
- NDK20b FFmpeg4.2.2 编译和集成
猜你喜欢
recvmsg & sendmsg
shell学习记录(四)
cyclegan:unpaired image-to-image translation using cycle-consistent adversarial network
一分钟了解同步、异步、阻塞和非阻塞的区别
Abnova CSV monoclonal antibody solution
Easy to understand C language keyword static
Disruptor(一)Sequence
论文阅读 Exploring Temporal Information for Dynamic Network Embedding
Sweet girl lisixia was invited to be the little host of the global finals of the sixth season perfect child model
Differences and functions of TOS cos DSCP
随机推荐
xargs 的作用详解
其他代码,,vt,,,k
创建OpenGl窗口
Energetic girl wangyujie was invited to be the spokesperson for the global finals of the sixth season perfect children's model
NDK20b FFmpeg4.2.2 编译和集成
求n乘阶之和
memory leak check tools 详解
biggan:large scale gan training for high fidelity natural image synthesis
影响个人成长的三个因素
PTA class a simulated 8th bomb: 1164-1167
Input 3 integers and output them from large to small
Steps of program compilation precompile compilation assembly connection
Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined) D. Felicity‘s Big Secret Revealed
Mot - clé C facile à comprendre statique
Reverse output an integer
Two indicators for determining the value of points to the business
Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined) C. Felicity is Coming!
Application and chemical properties of elastase
One stop solution EMQ for hundreds of millions of communication of Internet of things
vtk初始化代码学习1