非Jpa,UsersController实现

This commit is contained in:
myh 2023-10-07 19:53:17 +08:00
parent a13ea384e3
commit 38b4554c98

View File

@ -0,0 +1,60 @@
package com.example.springdemo.controller;
import com.example.springdemo.dao.impl.UsersDaoImpl;
import com.example.springdemo.entities.Users;
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 java.util.List;
@RestController
@RequestMapping("/users")
public class UsersController {
@Resource
private UsersDaoImpl usersDao;
@GetMapping("/findAll")
public List<Users> getUsers() {
Users user = new Users();
return usersDao.search(user);
}
@GetMapping("/findByID")
public Users getUserByID(@RequestParam("id") Long id) {
return usersDao.searchID(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";
}
}
@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";
}
}
}