一、博客分页查询
1,新建BlogService接口
package net.zl.myblog.service;
import net.zl.myblog.po.Blog;
import net.zl.myblog.vo.BlogQuery;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Map;
/**
*
*/
public interface BlogService {
Blog getBlog(Long id);//根据id查询
Page<Blog> listBlog(Pageable pageable, Blog blog);//分页查询
Blog saveBlog(Blog blog);//新增
Blog updateBlog(Long id,Blog blog);//更新
void deleteBlog(Long id);//删除
}
2,新建BlogRepository接口
package net.zl.myblog.dao;
import net.zl.myblog.po.Blog;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import java.util.List;
/**
*
*/
public interface BlogRepository extends JpaRepository<Blog, Long>, JpaSpecificationExecutor<Blog> {
}
3,新建BlogServiceImpl.java类
package net.zl.myblog.service;
import net.zl.myblog.NotFoundException;
import net.zl.myblog.dao.BlogRepository;
import net.zl.myblog.po.Blog;
import net.zl.myblog.po.Type;
import net.zl.myblog.util.MarkdownUtils;
import net.zl.myblog.util.MyBeanUtils;
import net.zl.myblog.vo.BlogQuery;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.criteria.*;
import java.util.*;
/**
*
*/
@Service
public class BlogServiceImpl implements BlogService {
@Autowired
private BlogRepository blogRepository;
//根据id查询
@Override
public Blog getBlog(Long id) {
return blogRepository.findById(id).orElse(null);
}
@Transactional
@Override
public Blog getAndConvert(Long id) {
Blog blog=blogRepository.findById(id).orElse(null);
if(blog==null){
throw new NotFoundException("该博客不存在");
}
Blog b=new Blog();
BeanUtils.copyProperties(blog,b);
String content=b.getContent();
b.setContent(MarkdownUtils.markdownToHtmlExtensions(content));
blogRepository.updateViews(id);
return b;
}
//分页动态查询
@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);
}
@Override
public Page<Blog> listBlog(Pageable pageable) {
return blogRepository.findAll(pageable);
}
@Override
public Page<Blog> listBlog(Long tagId, Pageable pageable) {
return blogRepository.findAll(new Specification<Blog>() {
@Override
public Predicate toPredicate(Root<Blog> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
Join join= root.join("tags");
return cb.equal(join.get("id"),tagId);
}
},pageable);
}
@Override
public Page<Blog> listBlog(String query, Pageable pageable) {
return blogRepository.findByQuery(query,pageable);
}
@Override
public List<Blog> listRecommendBlogTop(Integer size) {
Sort sort=Sort.by(Sort.Direction.DESC,"updateTime");
Pageable pageable = PageRequest.of(0, size, sort);
return blogRepository.findTop(pageable);
}
@Override
public Map<String, List<Blog>> archiveBlog() {
List<String> years=blogRepository.findGroupYear();
TreeMap<String,List<Blog>> map=new TreeMap<>((o1,o2)->Integer.parseInt(o2)-Integer.parseInt(o1));
// Map<String, List<Blog>> map =new LinkedHashMap<>();
for (String year : years){
map.put(year,blogRepository.findByYear(year));
}
return map;
}
@Override
public long countBlog() {
return blogRepository.count();
}
//新增
@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);
}
//更新
@Transactional
@Override
public Blog updateBlog(Long id, Blog blog) {
Blog b=blogRepository.findById(id).orElse(null);
if (b == null) {
throw new NotFoundException("该博客不存在");
}
BeanUtils.copyProperties(blog,b, MyBeanUtils.getNullPropertyNames(blog));
b.setUpdateTime(new Date());
return blogRepository.save(b);
}
//删除
@Transactional
@Override
public void deleteBlog(Long id) {
blogRepository.deleteById(id);
}
}
4、修改BlogController
package net.zl.myblog.web.admin;
import net.zl.myblog.po.Blog;
import net.zl.myblog.po.User;
import net.zl.myblog.service.BlogService;
import net.zl.myblog.service.TagService;
import net.zl.myblog.service.TypeService;
import net.zl.myblog.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import javax.servlet.http.HttpSession;
/**
* blogs控制器
*/
@Controller
@RequestMapping("/admin")
public class BlogController {
@Autowired
private BlogService blogService;
@GetMapping("/blogs")
public String blogs(@PageableDefault(size = 3,sort = {"updateTime"},direction = Sort.Direction.DESC)
Pageable pageable, Blog blog, Model model){
model.addAttribute("page",blogService.listBlog(pageable,blog));
return “admin/blogs";
}
}
5、修改blogs.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head th:replace="admin/_fragments :: head(~{::title})">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.css">
<link rel="stylesheet" href="../../static/css/me.css">
<title>博客管理</title>
</head>
<body>
<!--导航-->
<nav th:replace="admin/_fragments :: menu(1)" class="ui inverted attached segment m-padded-tb-mini m-shadow-small">
<div class="ui container">
<div class="ui inverted secondary stackable menu">
<h2 class="ui teal header item">管理后台</h2>
<a href="#" class="active m-item item m-mobile-hide"><i class="mini home icon"></i>博客</a>
<a href="#" class="m-item item m-mobile-hide"><i class="mini idea icon"></i>分类</a>
<a href="#" class="m-item item m-mobile-hide"><i class="mini tags icon"></i>标签</a>
<div class="right m-item m-mobile-hide menu">
<div class="ui dropdown item">
<div class="text">
<img src="https://unsplash.it/100/100?image=1005" alt="" class="ui avatar image">
周璐
</div>
<i class="dropdown icon"></i>
<div class="menu">
<a href="#" class="item">注销</a>
</div>
</div>
</div>
</div>
</div>
<a href="#" class="ui menu toggle black icon button m-right-top m-mobile-show" >
<i class="sidebar icon"></i>
</a>
</nav>
<!--二级导航-->
<div class="ui attached pointing menu">
<div class="ui container">
<div class="right menu">
<a href="#" th:href="@{/admin/blogs/input}" class="item">发布</a>
<a href="#" th:href="@{/admin/blogs}" class="teal active item">列表</a>
</div>
</div>
</div>
<!--中间内容-->
<div class="m-container-small m-padded-tb-big">
<div class="ui container">
<!--中间头部表单-->
<div class="ui secondary segment form">
<input type="hidden" name="page" >
<div class="inline fields">
<!--标题-->
<div class="field">
<input type="text" name="title" placeholder="标题">
</div>
<!--分类-->
<div class="field">
<div class="ui labeled action input">
<div class="ui type selection dropdown">
<input type="hidden" name="type">
<i class="dropdown icon"></i>
<div class="default text">分类</div>
<div class="menu">
<div th:each="type : ${types}" class="item" data-value="1" th:data-value="${type.id}" th:text="${type.name}">错误日志</div>
<!--/*-->
<div class="item" data-value="2">开发者手册</div>
<!--*/-->
</div>
</div>
<button id="clear-btn" class="ui compact button">clear</button>
</div>
</div>
<!--推荐-->
<div class="field">
<div class="ui checkbox">
<input type="checkbox" id="recommend" name="recommend">
<label for="recommend">推荐</label>
</div>
</div>
<!--搜索-->
<div class="field">
<button type="button" id="search-btn" class="ui mini teal basic button"><i class="search icon"></i>搜索</button>
</div>
</div>
</div>
<div id="table-container">
<!--中间表格-->
<table th:fragment="blogList" class="ui compact teal table">
<!--表头-->
<thead>
<tr>
<th></th>
<th>标题</th>
<th>类型</th>
<th>推荐</th>
<th>状态</th>
<th>更新时间</th>
<th>操作</th>
</tr>
</thead>
<!--表格内容-->
<tbody>
<tr th:each="blog,iterStat : ${page.content}">
<td th:text="${iterStat.count}">1</td>
<td th:text="${blog.title}">刻意练习清单</td>
<td th:text="${blog.type.name}">认知升级</td>
<td th:text="${blog.recommend} ? '是':'否'">是</td>
<td th:text="${blog.published} ? '发布':'草稿'">草稿</td>
<td th:text="${blog.updateTime}">2017-10-02 09:45</td>
<td>
<a href="#" th:href="@{/admin/blogs/{id}/input(id=${blog.id})}" class="ui mini teal basic button">编辑</a>
<a href="#" th:href="@{/admin/blogs/{id}/delete(id=${blog.id})}" class="ui mini red basic button">删除</a>
</td>
</tr>
</tbody>