当前位置:网站首页>@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 ...]
边栏推荐
- Visual studio 2013 redistributable is installed, but MySQL installation fails
- Dataframe extracts data from a column and converts it into a list
- Differences and functions of export set env in makefile
- 关于VS scanf出现‘scanf‘: This function or variable may be unsafe. Consider usi问题的解决方法
- Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined) D. Felicity‘s Big Secret Revealed
- Dazhou suggested that we media bloggers do these four things in the early stage
- Redis-链表
- Pre ++, post ++ and pre -- and post -- (+a, a++ and --a, a--)
- Sweet cool girl jinshuyi was invited to be the spokesperson for the global finals of the sixth season perfect children's model
- Chinese and English instructions of collagen enzyme Worthington
猜你喜欢

recv & send

Shell learning record (II)

readv & writev

图形渲染管线

One minute to understand the difference between synchronous, asynchronous, blocking and non blocking

关于strlen与sizeof的区别

vscode调试时提示更新到最新调试版本

Prompt to update to the latest debug version during vscode debugging

Redis7.0 installation steps

cyclegan:unpaired image-to-image translation using cycle-consistent adversarial network
随机推荐
Dataframe to list
UN make (6) conditional execution of makefile
NDK20b FFmpeg4.2.2 编译和集成
输入3个整数,从大到小输出出来
Connectez Le projecteur
qtvtkvs2015测试代码
Pre ++, post ++ and pre -- and post -- (+a, a++ and --a, a--)
Disruptor(一)Sequence
缓存技术之第一次亲密接触
Other codes,, VT,,, K
通俗易懂C语言关键字static
Create OpenGL window
Pointnet/Pointnet++学习
启牛推荐的证券账户安全吗?
Visual studio 2013 redistributable is installed, but MySQL installation fails
General introduction to gun make (2)
Detailed explanation of WiFi related knowledge
Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined) C. Felicity is Coming!
Redis的使用
Abnova actn4 DNA probe solution