初始化项目
This commit is contained in:
@@ -1,63 +0,0 @@
|
||||
package com.example.bocai.controller;
|
||||
|
||||
import com.example.bocai.entity.User;
|
||||
import com.example.bocai.repository.UserRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/users")
|
||||
public class UserController {
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
// 获取所有用户
|
||||
@GetMapping
|
||||
public List<User> getAllUsers() {
|
||||
return userRepository.findAll();
|
||||
}
|
||||
|
||||
// 根据ID获取用户
|
||||
@GetMapping("/{id}")
|
||||
public ResponseEntity<User> getUserById(@PathVariable Long id) {
|
||||
Optional<User> user = userRepository.findById(id);
|
||||
return user.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
|
||||
}
|
||||
|
||||
// 创建新用户
|
||||
@PostMapping
|
||||
public User createUser(@RequestBody User user) {
|
||||
return userRepository.save(user);
|
||||
}
|
||||
|
||||
// 更新用户
|
||||
@PutMapping("/{id}")
|
||||
public ResponseEntity<User> updateUser(@PathVariable Long id, @RequestBody User userDetails) {
|
||||
Optional<User> user = userRepository.findById(id);
|
||||
if (user.isPresent()) {
|
||||
User updatedUser = user.get();
|
||||
updatedUser.setName(userDetails.getName());
|
||||
updatedUser.setEmail(userDetails.getEmail());
|
||||
updatedUser.setPassword(userDetails.getPassword());
|
||||
return ResponseEntity.ok(userRepository.save(updatedUser));
|
||||
} else {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
}
|
||||
|
||||
// 删除用户
|
||||
@DeleteMapping("/{id}")
|
||||
public ResponseEntity<Void> deleteUser(@PathVariable Long id) {
|
||||
Optional<User> user = userRepository.findById(id);
|
||||
if (user.isPresent()) {
|
||||
userRepository.delete(user.get());
|
||||
return ResponseEntity.noContent().build();
|
||||
} else {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
package com.example.bocai.entity;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import lombok.Data;
|
||||
|
||||
@Entity
|
||||
@Data
|
||||
public class User {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
private String name;
|
||||
private String email;
|
||||
private String password;
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
package com.example.bocai.repository;
|
||||
|
||||
import com.example.bocai.entity.User;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface UserRepository extends JpaRepository<User, Long> {
|
||||
User findByEmail(String email);
|
||||
}
|
||||
Reference in New Issue
Block a user