This commit is contained in:
wu 2024-11-11 10:41:29 +08:00
parent 7b8e45ded1
commit 2a078a47cc
4 changed files with 23 additions and 3 deletions

View File

@ -15,7 +15,7 @@ public class User {
@Column(nullable = false)
private String password;
@Column
private String email;

View File

@ -39,9 +39,9 @@ public class UserController {
* - List<User> 用户列表每个User对象包含id, name, age
*/
@GetMapping("")
public List<User> findUsers(@RequestParam("name") String name, @RequestParam int page, @RequestParam int pageSize){
public List<User> findUsers(@RequestParam("name") String name, @RequestParam(required=false) Integer page, @RequestParam(required = false) Integer pageSize){
System.out.println("Got name: " + name+ " page: " + page+ " pageSize: " + pageSize);
return userService.queryUsers();
return userService.queryUsersContaining(name);
}
/**

View File

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

View File

@ -15,6 +15,7 @@ public class UserService {
UserRepository userRepository;
public User getUser(Long id){
return userRepository.findById(id).orElse(null);
}
@ -23,6 +24,13 @@ public class UserService {
return userRepository.findAll();
}
public List<User> queryUsersContaining(String name){
userRepository.findByNameLikeOrderByAgeDesc(name);
userRepository.abcd(name);
return userRepository.queryUsersByName(name);
}
public User createUser(User user){
return userRepository.save(user);