blob: 0bbcdc6b52d829723ef41c11961c087fbd7e24a7 [file] [log] [blame]
wuchimedesa1bf2782025-03-27 15:08:54 +08001package com.example.g8backend.controller;
2
3import com.example.g8backend.entity.User;
4import com.example.g8backend.service.IUserService;
5import org.springframework.beans.factory.annotation.Autowired;
6import org.springframework.web.bind.annotation.*;
7
8import java.util.List;
9
10@RestController
11@RequestMapping("/users")
12public class UserController {
13
14 @Autowired
15 private IUserService userService;
16
17 // 获取所有用户
18 @GetMapping
19 public List<User> getUsers() {
20 return userService.list();
21 }
22
23 // 通过ID获取用户
24 @GetMapping("/{id}")
25 public User getUserById(@PathVariable Long id) {
26 return userService.getById(id);
27 }
28
29 // 通过用户名获取用户
30 @GetMapping("/name/{name}")
31 public User getUserByName(@PathVariable String name) {
32 return userService.getUserByName(name);
33 }
34
35 // 添加用户
36 @PostMapping
37 public boolean addUser(@RequestBody User user) {
38 System.out.println(user);
39 return userService.save(user);
40 }
41
42 // 修改用户
43 @PutMapping
44 public boolean updateUser(@RequestBody User user) {
45 return userService.updateById(user);
46 }
47
48 // 删除用户
49 @DeleteMapping("/{id}")
50 public boolean deleteUser(@PathVariable Long id) {
51 return userService.removeById(id);
52 }
53}
54