Spring Boot 使用 @Delete 注解实现数据库删除操作 - 示例代码
以下是使用 @Delete 注解完成对数据库表 t_comment 的删除操作的示例代码:
@Mapper
public interface CommentMapper {
// 其他方法...
@Delete('DELETE FROM t_comment WHERE id = #{id}')
void deleteCommentById(Long id);
}
在上述代码中,我们在 CommentMapper 接口中使用 @Delete 注解,并指定了对应的 SQL 语句。通过占位符 '#{id}' 引用方法参数,实现了根据 id 删除数据库表 t_comment 中的记录。
在具体使用时,可以注入 CommentMapper 接口并调用 deleteCommentById 方法完成删除操作。例如:
@Service
public class CommentService {
private final CommentMapper commentMapper;
@Autowired
public CommentService(CommentMapper commentMapper) {
this.commentMapper = commentMapper;
}
public void deleteComment(Long id) {
commentMapper.deleteCommentById(id);
}
}
在上述示例中,我们在 CommentService 中注入了 CommentMapper,并调用 deleteCommentById 方法实现根据 id 删除评论的操作。
使用 @Delete 注解可以方便地完成对数据库表 t_comment 的删除操作。根据实际需求,可以根据表结构和业务逻辑编写相应的 SQL 语句和方法,进行删除操作。
原文地址: https://www.cveoy.top/t/topic/5Ar 著作权归作者所有。请勿转载和采集!