From 5c6259fb8a99e6d65d84fad5047dbe43f88d537f Mon Sep 17 00:00:00 2001 From: wu <303054730@qq.com> Date: Mon, 11 Nov 2024 12:03:05 +0800 Subject: [PATCH] changed --- .../src/main/java/com/lk/demo/UserController.java | 3 +++ .../src/main/java/com/lk/demo/UserRepository.java | 5 ++++- .../src/main/java/com/lk/demo/UserService.java | 1 + .../spring-boot-jpa-demo/src/main/resources/application.yml | 4 +--- 4 files changed, 9 insertions(+), 4 deletions(-) diff --git a/examples/chapter06/spring-boot-jpa-demo/src/main/java/com/lk/demo/UserController.java b/examples/chapter06/spring-boot-jpa-demo/src/main/java/com/lk/demo/UserController.java index b935398..6f2b479 100644 --- a/examples/chapter06/spring-boot-jpa-demo/src/main/java/com/lk/demo/UserController.java +++ b/examples/chapter06/spring-boot-jpa-demo/src/main/java/com/lk/demo/UserController.java @@ -12,6 +12,9 @@ public class UserController { @Autowired UserService userService; + + @Autowired + private UserRepository userRepository; /** * 获取指定ID的用户信息 * Method: GET diff --git a/examples/chapter06/spring-boot-jpa-demo/src/main/java/com/lk/demo/UserRepository.java b/examples/chapter06/spring-boot-jpa-demo/src/main/java/com/lk/demo/UserRepository.java index fc09d11..b7a6ea0 100644 --- a/examples/chapter06/spring-boot-jpa-demo/src/main/java/com/lk/demo/UserRepository.java +++ b/examples/chapter06/spring-boot-jpa-demo/src/main/java/com/lk/demo/UserRepository.java @@ -2,16 +2,19 @@ package com.lk.demo; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import java.util.List; public interface UserRepository extends JpaRepository { List findByNameLikeOrderByAgeDesc(String name); - @Query("Select u from User u where u.name like %?1% order by u.age desc") List abcd(String name); + @Query("Select u from User u where u.name like %:name% order by u.age desc") + List dcba(@Param("name") String name); + @Query(value = "Select * from Users u where u.name like %?1% order by u.age desc", nativeQuery = true) List queryUsersByName(String name); diff --git a/examples/chapter06/spring-boot-jpa-demo/src/main/java/com/lk/demo/UserService.java b/examples/chapter06/spring-boot-jpa-demo/src/main/java/com/lk/demo/UserService.java index 6156710..c182326 100644 --- a/examples/chapter06/spring-boot-jpa-demo/src/main/java/com/lk/demo/UserService.java +++ b/examples/chapter06/spring-boot-jpa-demo/src/main/java/com/lk/demo/UserService.java @@ -28,6 +28,7 @@ public class UserService { userRepository.findByNameLikeOrderByAgeDesc(name); userRepository.abcd(name); + userRepository.dcba(name); return userRepository.queryUsersByName(name); } diff --git a/examples/chapter06/spring-boot-jpa-demo/src/main/resources/application.yml b/examples/chapter06/spring-boot-jpa-demo/src/main/resources/application.yml index ce0aa63..48f91fb 100644 --- a/examples/chapter06/spring-boot-jpa-demo/src/main/resources/application.yml +++ b/examples/chapter06/spring-boot-jpa-demo/src/main/resources/application.yml @@ -17,10 +17,8 @@ spring: - jackson: property-naming-strategy: SNAKE_CASE # 驼峰转下划线 - datasource: url: jdbc:h2:file:./db.h2 # 使用文件存储 driverClassName: org.h2.Driver @@ -39,4 +37,4 @@ spring: defer-datasource-initialization: true database-platform: org.hibernate.dialect.H2Dialect hibernate: - ddl-auto: update \ No newline at end of file + ddl-auto: update \ No newline at end of file