This commit is contained in:
wu 2024-11-11 12:03:05 +08:00
parent 2a078a47cc
commit 5c6259fb8a
4 changed files with 9 additions and 4 deletions

View File

@ -12,6 +12,9 @@ public class UserController {
@Autowired @Autowired
UserService userService; UserService userService;
@Autowired
private UserRepository userRepository;
/** /**
* 获取指定ID的用户信息 * 获取指定ID的用户信息
* Method: GET * Method: GET

View File

@ -2,16 +2,19 @@ package com.lk.demo;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.util.List; import java.util.List;
public interface UserRepository extends JpaRepository<User, Long> { public interface UserRepository extends JpaRepository<User, Long> {
List<User> findByNameLikeOrderByAgeDesc(String name); List<User> findByNameLikeOrderByAgeDesc(String name);
@Query("Select u from User u where u.name like %?1% order by u.age desc") @Query("Select u from User u where u.name like %?1% order by u.age desc")
List<User> abcd(String name); List<User> abcd(String name);
@Query("Select u from User u where u.name like %:name% order by u.age desc")
List<User> dcba(@Param("name") String name);
@Query(value = "Select * from Users u where u.name like %?1% order by u.age desc", nativeQuery = true) @Query(value = "Select * from Users u where u.name like %?1% order by u.age desc", nativeQuery = true)
List<User> queryUsersByName(String name); List<User> queryUsersByName(String name);

View File

@ -28,6 +28,7 @@ public class UserService {
userRepository.findByNameLikeOrderByAgeDesc(name); userRepository.findByNameLikeOrderByAgeDesc(name);
userRepository.abcd(name); userRepository.abcd(name);
userRepository.dcba(name);
return userRepository.queryUsersByName(name); return userRepository.queryUsersByName(name);
} }

View File

@ -17,10 +17,8 @@
spring: spring:
jackson: jackson:
property-naming-strategy: SNAKE_CASE # 驼峰转下划线 property-naming-strategy: SNAKE_CASE # 驼峰转下划线
datasource: datasource:
url: jdbc:h2:file:./db.h2 # 使用文件存储 url: jdbc:h2:file:./db.h2 # 使用文件存储
driverClassName: org.h2.Driver driverClassName: org.h2.Driver
@ -39,4 +37,4 @@ spring:
defer-datasource-initialization: true defer-datasource-initialization: true
database-platform: org.hibernate.dialect.H2Dialect database-platform: org.hibernate.dialect.H2Dialect
hibernate: hibernate:
ddl-auto: update ddl-auto: update