添加web依赖,在controller中接收和处理请求

master
hanrenchun 1 year ago
parent 1fca85e151
commit f661c416da

@ -22,7 +22,10 @@
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
@ -39,6 +42,7 @@
<version>2.3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>

@ -0,0 +1,54 @@
package com.example.mybatisdemo.controller;
import com.example.mybatisdemo.mapper.UserMapper;
import com.example.mybatisdemo.pojo.dto.UserDTO;
import com.example.mybatisdemo.pojo.entity.User;
import com.example.mybatisdemo.pojo.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@PostMapping("/addUser")
public String addUser(@RequestBody UserDTO userDTO){
User user = new User();
BeanUtils.copyProperties(userDTO,user);
int insert = userMapper.insert(user);
if (insert!=0){
return "添加成功";
}
return "添加失败";
}
@GetMapping("/selectUser/{id}")
public UserVO selectUser(@PathVariable int id){
UserVO userVO = userMapper.selectById(id);
return userVO;
}
@PostMapping("/update")
public String updateUser(@RequestBody UserDTO userDTO){
int i = userMapper.updateById(userDTO);
if (i!=0){
return "修改成功";
}
return "修改失败";
}
@GetMapping("delete/{id}")
public String deleteUser(@PathVariable int id){
int i = userMapper.deleteById(id);
if (i!=0){
return "删除成功";
}
return "删除失败";
}
}
Loading…
Cancel
Save