news/dao/NewRepository.java
package com.zr.news.dao;
import com.zr.news.po.News;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
public interface NewRepository extends JpaRepository<News,Long> , JpaSpecificationExecutor {
// @Query(“select n from News n where n.title like ?1 or n.content like?1”)
// Page findByQuery(String query, Pageable pageable);
}
po/News.java
package com.zr.news.po;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Entity
@Table(name = “t_news”)
public class News {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
@Basic(fetch = FetchType.LAZY)
@Lob
private String content;
private String firstPicture;
private String flag;
private String views;
private boolean appreciation;
private boolean shareStatement;
private boolean commentabled;
private boolean published;
private boolean recommend;
@Temporal(TemporalType.TIMESTAMP)
private Date createTime;
@Temporal(TemporalType.TIMESTAMP)
private Date updateTime;
@ManyToOne
private Type type;
@ManyToOne
private User user;
@ManyToMany(cascade = CascadeType.PERSIST) //级联
private List<Tag> tags = new ArrayList<>();
@Transient
private String tagIds;
private String description;
public News() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getFirstPicture() {
return firstPicture;
}
public void setFirstPicture(String firstPicture) {
this.firstPicture = firstPicture;
}
public String getFlag() {
return flag;
}
public void setFlag(String flag) {
this.flag = flag;
}
public String getViews() {
return views;
}
public void setViews(String views) {
this.views = views;
}
public boolean isAppreciation() {
return appreciation;
}
public void setAppreciation(boolean appreciation) {
this.appreciation = appreciation;
}
public boolean isShareStatement() {
return shareStatement;
}
public void setShareStatement(boolean shareStatement) {
this.shareStatement = shareStatement;
}
public boolean isCommentabled() {
return commentabled;
}
public void setCommentabled(boolean commentabled) {
this.commentabled = commentabled;
}
public boolean isPublished() {
return published;
}
public void setPublished(boolean published) {
this.published = published;
}
public boolean isRecommend() {
return recommend;
}
public void setRecommend(boolean recommend) {
this.recommend = recommend;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public Type getType() {
return type;
}
public void setType(Type type) {
this.type = type;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public List<Tag> getTags() {
return tags;
}
public void setTags(List<Tag> tags) {
this.tags = tags;
}
public String getTagIds() {
return tagIds;
}
public void setTagIds(String tagIds) {
this.tagIds = tagIds;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
// @Override
// public String toString() {
// return “News{” +
// “id=” + id +
// “, title=’” + title + ‘’’ +
// “, content=’” + content + ‘’’ +
// “, firstPicture=’” + firstPicture + ‘’’ +
// “, flag=’” + flag + ‘’’ +
// “, views=’” + views + ‘’’ +
// “, appreciation=” + appreciation +
// “, shareStatement=” + shareStatement +
// “, commentabled=” + commentabled +
// “, published=” + published +
// “, recommend=” + recommend +
// “, createTime=” + createTime +
// “, updateTime=” + updateTime +
// “, type=” + type +
// “, user=” + user +
// ‘}’;
public void init(){
this.tagIds = tagsToIds(this.getTags());
}
//1,2,3
private String tagsToIds(List<Tag> tags){
if(!tags.isEmpty()){
StringBuffer ids = new StringBuffer();
boolean flag = false;
for (Tag tag:tags){
if(flag){
ids.append(",");
}else{
flag=true;
}
ids.append(tag.getId());
}
return ids.toString();
}else{
return tagIds;//看不懂这一句
}
}
@Override
public String toString() {
return "News{" +
"id=" + id +
", title='" + title + '\'' +
", content='" + content + '\'' +
", firstPicture='" + firstPicture + '\'' +
", flag='" + flag + '\'' +
", views='" + views + '\'' +
", appreciation=" + appreciation +
", shareStatement=" + shareStatement +
", commentabled=" + commentabled +
", published=" + published +
", recommend=" + recommend +
", createTime=" + createTime +
", updateTime=" + updateTime +
", type=" + type +
", user=" + user +
", tags=" + tags +
", tagIds='" + tagIds + '\'' +
", description='" + description + '\'' +
'}';
}
}
po/Tag.java
package com.zr.news.po;
import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;
@Entity
@Table(name = “t_tag”)
public class Tag {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank(message = "标签名称不能为空")
private String name;
public List<News> getNewsList() {
return newsList;
}
public void setNewsList(List<News> newsList) {
this.newsList = newsList;
}
@ManyToMany(mappedBy = "tags")
private List<News> newsList = new ArrayList<>();
public Tag() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Tag{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
service/NewService.java
package com.zr.news.service;
import com.zr.news.po.News;
import com.zr.news.vo.NewQuery;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
public interface NewService {
Page<News> listNew(Pageable pageable, NewQuery newQuery);
News saveNew(News news);
News getNew(Long id);
News updateNew(Long id,News news);
}
service/TagService.java
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
public interface TagService {
Page<Tag> listTag(Pageable pageable);
@@ -18,4 +20,10 @@
Tag updateTag(Long id,Tag tag);
List<Tag> listTag();
List<Tag> listTag(String ids);
}
impl/NewServiceImpl.java
package com.zr.news.service.impl;
import com.zr.news.dao.NewRepository;
import com.zr.news.po.News;
import com.zr.news.service.NewService;
import com.zr.news.vo.NewQuery;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
public class NewServiceImpl implements NewService {
@Autowired
private NewRepository newRepository;
//博客管理中的博客列表(包含了查询)
@Override
public Page<News> listNew(Pageable pageable, NewQuery newQuery) {
return newRepository.findAll(new Specification<News>() {
@Override
public Predicate toPredicate(Root<News> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
List<Predicate> predicates = new ArrayList<>();
if(!"".equals(newQuery.getTitle())&&newQuery.getTitle()!=null){
predicates.add(cb.like(root.<String>get("title"),"%"+newQuery.getTitle()+"%"));
}
if(newQuery.getTypeId()!=null){
predicates.add(cb.equal(root.get("id"),newQuery.getTypeId()));
}
if(newQuery.isRecommend()){
predicates.add(cb.equal(root.get("recommend"),newQuery.isRecommend()));
}
cq.where(predicates.toArray(new Predicate[predicates.size()]));
return null;
}
},pageable);
}
@Override
public News saveNew(News news) {
if(news.getId()==null){
news.setCreateTime(new Date());
news.setUpdateTime(new Date());
}
return newRepository.save(news);
}
@Override
public News getNew(Long id) {
return newRepository.findById(id).orElse(null);
}
@Override
public News updateNew(Long id, News news) {
News news1 = newRepository.findById(id).orElse(null);
if(news1==null){
System.out.println("未获得更新对象");
}
BeanUtils.copyProperties(news,news1);
news1.setUpdateTime(new Date());
return newRepository.save(news1);
}
}
vo/NewQuery.java
package com.zr.news.vo;
public class NewQuery {
private String title;
private Long typeId;
private boolean recommend;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Long getTypeId() {
return typeId;
}
public void setTypeId(Long typeId) {
this.typeId = typeId;
}
public boolean isRecommend() {
return recommend;
}
public void setRecommend(boolean recommend) {
this.recommend = recommend;
}
@Override
public String toString() {
return "NewQuery{" +
"title='" + title + '\'' +
", typeId=" + typeId +
", recommend=" + recommend +
'}';
}
}