SpringBoot与Mybatis整合(3)
程序员文章站
2024-01-26 12:49:34
...
Controller层实现
创建controller包,在其中编写controller类
此处贴出UserController如下:
package com.example.mybatistest.web;
import com.example.mybatistest.entity.User;
import com.example.mybatistest.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value = "/listuser", method = RequestMethod.GET)
private Map<String,Object> listUser(){
Map<String,Object> map = new HashMap<String, Object>();
map.put("userList",userService.getUserList());
return map;
}
@RequestMapping(value = "/getuserbyname", method = RequestMethod.GET)
private Map<String,Object> getUserByName(String userName){
Map<String,Object> map = new HashMap<String, Object>();
User user = userService.getUserByName(userName);
map.put("user",user);
return map;
}
@RequestMapping(value = "/adduser",method = RequestMethod.POST)
private Map<String,Object> addUser(@RequestBody User user){
Map<String,Object> map = new HashMap<String, Object>();
map.put("success",userService.addUser(user));
return map;
}
@RequestMapping(value = "/modifyuser",method = RequestMethod.POST)
private Map<String,Object> modifyUser(@RequestBody User user){
Map<String,Object> map = new HashMap<String, Object>();
map.put("success",userService.modifyUser(user));
return map;
}
@RequestMapping(value = "/removeuser",method = RequestMethod.GET)
private Map<String,Object> removeUser(String userName){
Map<String,Object> map = new HashMap<String, Object>();
map.put("success",userService.deleteUser(userName));
return map;
}
}