springboot mybatis批量删除
时间: 2023-08-24 18:07:06 浏览: 88
1. 在Mapper接口中定义一个批量删除方法:
```java
int deleteBatch(List<Long> ids);
```
2. 在Mapper.xml文件中编写SQL语句:
```xml
<delete id="deleteBatch">
delete from table_name where id in
<foreach collection="list" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</delete>
```
3. 在Service层调用Mapper接口的批量删除方法:
```java
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public int deleteBatch(List<Long> ids) {
return userMapper.deleteBatch(ids);
}
}
```
4. 在Controller层调用Service层的批量删除方法:
```java
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserService userService;
@DeleteMapping("/deleteBatch")
public Result deleteBatch(@RequestBody List<Long> ids) {
int count = userService.deleteBatch(ids);
return Result.success(count);
}
}
```
5. 在前端页面中调用Controller层的批量删除接口。
阅读全文