欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java开发语言代码实例

程序员文章站 2022-03-26 16:52:29
package com.zr0726.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;p...

package com.zr0726.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 tagId) {
    this.tagIds = tagIds;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

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 + '\'' +
            '}';
} 

}

package com.zr0726.news.service.impl;

import com.zr0726.news.dao.NewRepository;
import com.zr0726.news.po.News;
import com.zr0726.news.service.NewService;
import com.zr0726.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("type").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);
} 

}

本文地址:https://blog.csdn.net/KanaseKanon/article/details/107888889

相关标签: java