帮我写一段基于ssm框架的博客代码
时间: 2023-04-06 20:05:00 浏览: 110
好的,我可以回答这个问题。以下是一个基于SSM框架的博客代码示例:
@Controller
@RequestMapping("/blog")
public class BlogController {
@Autowired
private BlogService blogService;
@RequestMapping("/list")
public ModelAndView list() {
List<Blog> blogs = blogService.getAllBlogs();
ModelAndView modelAndView = new ModelAndView("blog/list");
modelAndView.addObject("blogs", blogs);
return modelAndView;
}
@RequestMapping("/add")
public String add() {
return "blog/add";
}
@RequestMapping("/save")
public String save(Blog blog) {
blogService.saveBlog(blog);
return "redirect:/blog/list";
}
@RequestMapping("/edit/{id}")
public ModelAndView edit(@PathVariable("id") Long id) {
Blog blog = blogService.getBlogById(id);
ModelAndView modelAndView = new ModelAndView("blog/edit");
modelAndView.addObject("blog", blog);
return modelAndView;
}
@RequestMapping("/update")
public String update(Blog blog) {
blogService.updateBlog(blog);
return "redirect:/blog/list";
}
@RequestMapping("/delete/{id}")
public String delete(@PathVariable("id") Long id) {
blogService.deleteBlog(id);
return "redirect:/blog/list";
}
}
@Service
public class BlogServiceImpl implements BlogService {
@Autowired
private BlogMapper blogMapper;
@Override
public List<Blog> getAllBlogs() {
return blogMapper.getAllBlogs();
}
@Override
public Blog getBlogById(Long id) {
return blogMapper.getBlogById(id);
}
@Override
public void saveBlog(Blog blog) {
blogMapper.saveBlog(blog);
}
@Override
public void updateBlog(Blog blog) {
blogMapper.updateBlog(blog);
}
@Override
public void deleteBlog(Long id) {
blogMapper.deleteBlog(id);
}
}
@Repository
public interface BlogMapper {
List<Blog> getAllBlogs();
Blog getBlogById(Long id);
void saveBlog(Blog blog);
void updateBlog(Blog blog);
void deleteBlog(Long id);
}
以上是一个简单的基于SSM框架的博客代码示例,包括了Controller、Service和Mapper三个层次的代码。
阅读全文