@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/add")
@ResponseBody
public String add(@RequestBody User user){
try{
userService.add(user);
return "success";
} catch (Exception e) {
return "fail";
}
}
@GetMapping("/delete/{id}")
@ResponseBody
public String delete(@PathVariable Integer id){
try {
userService.deleteById(id);
return "success";
} catch (Exception e) {
return "fail";
}
}
@GetMapping("/list")
public String list(Model model){
List<User> users=userService.selectAll();
model.addAttribute("mos", users)
return "user/list";
}
}
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public void add(User user){
userMapper.insert(user);
}
public void deleteById(Integer id){
userMapper.deleteById(id);
}
public List<User> selectAll(){
userMapper.selectAll();
}
}
@Repository
public interface UserMapper {
void insert(User user);
void deleteById(Integer id);
List<User> selectAll();
}
|