UsersController层增加

This commit is contained in:
myh 2023-10-25 18:16:21 +08:00
parent b09082f30a
commit a2bc716716

View File

@ -1,60 +1,47 @@
package com.example.springdemo.controller; package com.example.springdemo.controller;
import com.example.springdemo.dao.impl.UsersDaoImpl;
import com.example.springdemo.entities.Users; import com.example.springdemo.entities.Users;
import com.example.springdemo.service.UsersService;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List; import java.util.Optional;
@RestController @RestController
@RequestMapping("/users") @RequestMapping("/users")
public class UsersController { public class UsersController {
@Resource @Resource
private UsersDaoImpl usersDao; private UsersService usersService;
@GetMapping("/findAll") @PostMapping("/add")
public List<Users> getUsers() { public Users saveUsers(@RequestBody Users users) {
Users user = new Users(); return usersService.insetUsers(users);
return usersDao.search(user);
} }
@GetMapping("/findByID") @DeleteMapping("/delete/{id}")
public Users getUserByID(@RequestParam("id") Long id) { public void deleteUsers(@PathVariable("id") Long id) {
return usersDao.searchID(id); usersService.deleteUsersById(id);
} }
@GetMapping("/save") @PutMapping("/update")
public String saveUser(@RequestParam("name") String name, public Users updateUsers(@RequestBody Users users) {
@RequestParam("phoneNumber") String phoneNumber, return usersService.updateUsers(users);
@RequestParam("address") String address,
@RequestParam("password") String password) {
Users user = new Users();
user.setName(name);
user.setPhoneNumber(phoneNumber);
user.setAddress(address);
user.setPassword(password);
if (usersDao.insert(user) <= 0) {
return "success";
} else {
return "fail";
}
} }
@GetMapping("/delete") @GetMapping("/find")
public String deleteUser(@RequestParam("id") Long id) { public Iterable<Users> getUsers() {
Users user = new Users(); return usersService.findAllUsers();
user.setId(id);
if (usersDao.delete(user) <= 0) {
return "fail";
} else {
return "success";
} }
@GetMapping("/find/{name}")
public Users getUsersByName(@PathVariable("name") String name) {
return usersService.findByName(name);
}
@GetMapping("/find/{id}")
public Optional<Users> getUsersById(@PathVariable("id") Long id) {
return usersService.findById(id);
} }
} }