SprigDataJPA(三)SprigDataJPAカスタム@Query検索方法
GitHub:https://github.com/291685399/springboot-learning/tree/master/springboot-springdatajpa03
何がカスタム@Queryの検索方法ですか?
複数の条件を持つ非常に複雑なクエリを作成してデータをフィルタリングすると、照会方法の名前が非常に複雑になります。このような状況を避けるために、SpringDataJPAは@QueryコメントでSQLをカスタマイズして検索することをサポートします。
カスタムSQLクエリ
pom.xml:
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-jpaartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
dependencies>
appication.properties:# datasource
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springboot-springdatajpa03?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
# springdatajpa
# SQL,
spring.jpa.show-sql=true
#
spring.jpa.hibernate.ddl-auto=update
# sql ,
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.use_sql_comments=true
# InneoDB
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
User:@Entity
@Data
public class User {
@Id
@GeneratedValue
private Long id;
private String name;
private Integer age;
private String sex;
private String address;
}
UserRepository:
public interface UserRepository extends Repository<User, Long> {
/**
* id
*
* User ORM , ORM
* , ?1,?2,?3......
*
* @param id
* @return
*/
@Query("select u from User u where u.id=?1")
public User findUser(Long id);
/**
* id
*
* User ORM , ORM
* :id
*
* @param id
* @return
*/
@Query("select u from User u where u.id=:id")
public User findUserByParam(@Param("id") Long id);
/**
* id name
*
* User ORM , ORM
* , ?1,?2,?3......
*
* @param id
* @return
*/
@Query("select u from User u where u.id=?1 and u.name=?2")
public User findUserByIdAndName(Long id, String name);
/**
*
*
* User ORM , ORM
*
* @return
*/
@Query("select u from User u")
public List<User> findUserList();
/**
* name
*
* User ORM , ORM
* , ?1,?2,?3......
*
* @param name
* @return
*/
@Query("select u from User u where name like %?1%")
public List<User> findUserListByLikeName(String name);
/**
* name
*
* User ORM , ORM
* , ?1,?2,?3......
*
* @param name
* @return
*/
@Query("select u from User u where name like concat('%',?1,'%') ")
public List<User> findUserListByLikeConcatName(String name);
/**
* id
*
* nativeQuery = true ,JPA SQL
*
* @param id
* @return
*/
@Query(value = "select * from user where id=?1", nativeQuery = true)
public User findUserByNativeQuery(int id);
/**
* SpEL
*
* Spring Data JPA 1.4 , SpEL @Query。
* , 。Spring Data JPA entityName。 select x from #{#entityName} x。
* entityName 。 entityName : name @Entity , 。 ,
*
* @param name
* @return
*/
@Query("select u from #{#entityName} u where u.name = ?1")
public List<User> findBySpEL(String name);
}
UserRepositoryTests:@RunWith(SpringRunner.class)
@SpringBootTest
public class UserRepositoryTests {
@Autowired
private UserRepository userRepository;
@Test
public void findUserTest() {
Long id = 1L;
User user = userRepository.findUser(id);
System.out.println("user:" + user);
}
@Test
public void findUserByParamTest() {
Long id = 1L;
User user = userRepository.findUserByParam(id);
System.out.println("user:" + user);
}
@Test
public void findUserByIdAndNameTest() {
Long id = 1L;
String name = "ljk";
User user = userRepository.findUserByIdAndName(id, name);
System.out.println("user:" + user);
}
@Test
public void findUserListTest() {
List<User> userList = userRepository.findUserList();
System.out.println("userList:" + userList);
}
@Test
public void findUserListByLikeNameTest() {
String name = "j";
List<User> userListByLikeName = userRepository.findUserListByLikeName(name);
System.out.println("userListByLikeName:" + userListByLikeName);
}
@Test
public void findUserListByLikeConcatNameTest() {
String name = "j";
List<User> userListByLikeName = userRepository.findUserListByLikeConcatName(name);
System.out.println("userListByLikeName:" + userListByLikeName);
}
@Test
public void findUserByNativeQueryTest() {
int id = 1;
User user = userRepository.findUserByNativeQuery(id);
System.out.println("user:" + user);
}
@Test
public void findByLastnameTest() {
String str = "wyj";
List<User> userList = userRepository.findBySpEL(str);
System.out.println("userList:" + userList);
}
}