根据一个完整的单表增删改查的controller、service、mapping。 使用mybatis plus。
时间: 2024-05-10 08:13:54 浏览: 48
好的,以下是一个基于Mybatis-Plus的单表增删改查的Controller、Service、Mapper的示例代码:
Controller:
```java
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
// 查询用户列表
@GetMapping("/list")
public List<User> userList() {
return userService.list();
}
// 根据id查询用户
@GetMapping("/{id}")
public User getUser(@PathVariable Long id) {
return userService.getById(id);
}
// 新增用户
@PostMapping("/add")
public boolean addUser(@RequestBody User user) {
return userService.save(user);
}
// 修改用户信息
@PutMapping("/{id}")
public boolean updateUser(@PathVariable Long id, @RequestBody User user) {
user.setId(id);
return userService.updateById(user);
}
// 根据id删除用户
@DeleteMapping("/{id}")
public boolean deleteUser(@PathVariable Long id) {
return userService.removeById(id);
}
}
```
Service:
```java
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}
```
Mapper:
```java
public interface UserMapper extends BaseMapper<User> {
}
```
以上代码中,我们使用了Mybatis-Plus的Service以及BaseMapper,省去了大量的增删改查的重复代码,方便开发。同时也提供了常用的增删改查方法,包括:
- list:查询列表
- getById:根据id查询实体
- save:新增实体
- updateById:根据id更新实体
- removeById:根据id删除实体
另外,我们在Controller中使用了@RestController和@RequestMapping注解,将Controller注册为Restful服务,同时接收GET、POST、PUT以及DELETE请求,并调用对应的Service方法处理数据。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)