| package com.example.g8backend.controller; |
| |
| import com.example.g8backend.entity.User; |
| import com.example.g8backend.service.IUserService; |
| import org.springframework.beans.factory.annotation.Autowired; |
| import org.springframework.web.bind.annotation.*; |
| |
| import java.util.List; |
| |
| @RestController |
| @RequestMapping("/users") |
| public class UserController { |
| |
| @Autowired |
| private IUserService userService; |
| |
| // 获取所有用户 |
| @GetMapping |
| public List<User> getUsers() { |
| return userService.list(); |
| } |
| |
| // 通过ID获取用户 |
| @GetMapping("/{id}") |
| public User getUserById(@PathVariable Long id) { |
| return userService.getById(id); |
| } |
| |
| // 通过用户名获取用户 |
| @GetMapping("/name/{name}") |
| public User getUserByName(@PathVariable String name) { |
| return userService.getUserByName(name); |
| } |
| |
| // 添加用户 |
| @PostMapping |
| public boolean addUser(@RequestBody User user) { |
| System.out.println(user); |
| return userService.save(user); |
| } |
| |
| // 修改用户 |
| @PutMapping |
| public boolean updateUser(@RequestBody User user) { |
| return userService.updateById(user); |
| } |
| |
| // 删除用户 |
| @DeleteMapping("/{id}") |
| public boolean deleteUser(@PathVariable Long id) { |
| return userService.removeById(id); |
| } |
| } |
| |