Springboot个人博客系统 7 博客管理
发布时间
阅读量:
阅读量
博客管理
1.Service层
根据需求需要如下方法:
根据id查询博客:getBlog();
分页查询列出所有博客:listBlogs();
在前端页面中查询条件为三个,分别为标题,分类,是否被推荐,所以新建了BlogQuerylei类
这里使用了SpringData中的Specification<>接口进行查询
插入博客数据的方法:saveBlog();
更新博客数据的方法:updateBlog();
删除博客数据的方法:deleteBlog();
BlogQuery类:
@Data
@NoArgsConstructor
public class BlogQuery {
private String title;
private Long typeId;
private boolean recommend;
}
BlogServiceImpl类
@Service
public class BlogServiceImpl implements BlogService {
@Autowired
private BlogRepository blogRepository;
@Override
public Blog getBlog(Long id) {
return blogRepository.findOne(id);
}
@Override
public Page<Blog> listBlog(Pageable pageable, BlogQuery blog) {
return blogRepository.findAll(new Specification<Blog>() {
@Override
public Predicate toPredicate(Root<Blog> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
List<Predicate> predicates = new ArrayList<>();
if(!"".equals(blog.getTitle()) && blog.getTitle() != null){
predicates.add(cb.like(root.<String>get("title"),"%"+blog.getTitle()+"%"));
}
if(blog.getTypeId() != null){
predicates.add(cb.equal(root.<Type>get("type").get("id"),blog.getTypeId()));
}
if(blog.isRecommend()){
predicates.add(cb.equal(root.<Boolean>get("recommend"), blog.isRecommend())) ;
}
cq.where(predicates.toArray(new Predicate[predicates.size()]));
return null;
}
},pageable);
}
@Transactional
@Override
public Blog saveBlog(Blog blog) {
if(blog.getId() == null){
blog.setCreateTime(new Date());
blog.setUpdateTime(new Date());
blog.setViews(0);
}else {
blog.setUpdateTime(new Date());
}
return blogRepository.save(blog);
}
@Override
public Blog updateBlog(Long id, Blog blog) {
Blog b = blogRepository.findOne(id);
if (b == null){
throw new NotFoundException("该博客不存在");
}
BeanUtils.copyProperties(b,blog);
return blogRepository.save(b);
}
@Override
public void deleteBlog(Long id) {
blogRepository.delete(id);
}
}
BlogService接口:
public interface BlogService {
Blog getBlog(Long id);
Page<Blog> listBlog(Pageable pageable, BlogQuery blog);
Blog saveBlog(Blog blog);
Blog updateBlog(Long id, Blog blog);
void deleteBlog(Long id);
}
2.Dao层
public interface BlogRepository extends JpaRepository<Blog, Long>, JpaSpecificationExecutor<Blog> {
}
3.Controller层
在Controller层需要编写编辑博客方法,新增博客方法,查询博客方法,删除博客方法,处理表单信息方法
在这里编辑与新增共用一个页面blog-input
public class BlogController {
private static final String INPUT = "admin/blogs-input";
private static final String LIST = "admin/blogs";
private static final String REDIRECT_LIST = "redirect:/admin/blogs";
@Autowired
private BlogService blogService;
@Autowired
private TypeService typeService;
@Autowired
private TagService tagService;
/** * 博客列表
* 分页查询,一页为十条数据
* 采取DESC排序(倒序)
* @param pageable
* @param model
* @param blog
* @return
*/
@GetMapping("/blogs")
public String list(@PageableDefault(size = 10, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable, Model model, BlogQuery blog){
model.addAttribute("types", typeService.listType());
model.addAttribute("page", blogService.listBlog(pageable,blog));
return LIST;
}
/** * 博客查询
* 使用了AJAX技术,只更新一部分页面
* @param pageable
* @param model
* @param blog
* @return
*/
@PostMapping("/blogs/search")
public String search(@PageableDefault(size = 10, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable, Model model, BlogQuery blog){
model.addAttribute("page", blogService.listBlog(pageable,blog));
return "admin/blogs :: blogList";
}
/** * 博客编辑
* 从前端页面得到一个id
* 通过id得到这个博客并将对象传到前端
* @param id
* @param model
* @return
*/
@GetMapping("/blogs/{id}/input")
public String editinput(@PathVariable Long id, Model model){
setTypeAndTag(model);
Blog b = blogService.getBlog(id);
b.init();
model.addAttribute("blog", b);
return INPUT;
}
/** * 查询所有博客标签和分类
* @param model
*/
private void setTypeAndTag(Model model){
model.addAttribute("types", typeService.listType());
model.addAttribute("tags", tagService.listTag());
}
/** * 博客新增
* new一个空对象传到前端,并将所有标签,分类信息传到前端进行显示
* 空对象是因为新增与编辑共用页面,为了防止报错传过去一个空对象
* @param model
* @return
*/
@GetMapping("/blogs/input")
public String input(Model model){
setTypeAndTag(model);
model.addAttribute("blog", new Blog());
return INPUT;
}
/** * 处理post表单
* 首先得到session中的用户信息,设置为作者
* 校验传过来的标签分类是否和数据库一直,设置标签分类
* 若前端传过来的id为空,则说明是新增,使用saveBlog
* 若id不为空,则说明是更新,使用updateBlog
* @param blog
* @param session
* @param attributes
* @return
*/
@PostMapping("/blogs")
public String post(Blog blog, HttpSession session, RedirectAttributes attributes){
String flag = blog.getFlag();
System.out.println(flag);
blog.setUser((User)session.getAttribute("user"));
blog.setType(typeService.getType(blog.getType().getId()));
blog.setTags(tagService.listTag(blog.getTagIds()));
Blog b;
if(blog.getId() == null){
b = blogService.saveBlog(blog);
}else {
b = blogService.updateBlog(blog.getId(), blog);
}
if(b == null){
attributes.addFlashAttribute("message","操作失败");
}else {
attributes.addFlashAttribute("message","操作成功");
}
return REDIRECT_LIST;
}
/** * 根据前端传过来的id进行删除操作
* @param id
* @param attributes
* @return
*/
@GetMapping("/blogs/{id}/delete")
public String delete(@PathVariable Long id, RedirectAttributes attributes){
blogService.deleteBlog(id);
attributes.addFlashAttribute("message","删除成功");
return REDIRECT_LIST;
}
}
全部评论 (0)
还没有任何评论哟~
