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