您的位置:首页 > 编程语言 > Java开发

springboot开发:新闻管理系统之添加和修改新闻

2020-07-30 01:08 811 查看

前言

前一篇博客针对新闻系统的标签以及分类管理进行了设计,这篇博客主要设计新闻的主体,包括新闻的添加以及修改功能。

实现过程

实体类的设计

需要注意的是,一个标签会对应多个新闻,一个用户(新闻撰稿人)也会有多篇新闻报道,所以需要在必要的地方进行注释。

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 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 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;
}

public void init(){
this.tagIds=tagsToIds(this.getTags());
}
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 id.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 + '\'' +
'}';
}

}

vo文件夹

需要新建一个vo文件夹,这个文件夹用来存放工具类的,新建NewQuery类,用来对接下来的新闻查询功能,并进行一个预处理

package com.zr0726.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 +
'}';
}
}

dao接口以及Service层

public interface NewRepository extends JpaRepository<News,Long> , JpaSpecificationExecutor<News> {

@Query("select n from News n where n.title like ?1 or n.content like ?1")
Page<News> findByQuery(String query, Pageable 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
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
//查询一个对象映射成为root,criteriaquery是一个容器
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);
}
}

在前端设计中,查询方式是有多种的,所系需要区分各种查询条件的方法,这一步是比较麻烦的。

Controller层

@Controller
@RequestMapping("/admin")
public class NewController {

private static final String INPUT="admin/news-input";

private static final String LIST="admin/news";

private static final String REDIRECT_LIST="redirect:/admin/news";
@Autowired
private NewService newService;

@Autowired
private TypeService typeService;

@Autowired
private TagService tagService;

@GetMapping("/news")
public String news(@PageableDefault(size = 3,sort = {"updateTime"},direction = Sort.Direction.DESC)
Pageable pageable, NewQuery newquery, Model model){
model.addAttribute("types",typeService.listType());
model.addAttribute("page",newService.listNew(pageable,newquery));
return LIST;
}

@PostMapping("/news/search")
public String search(@PageableDefault(size = 3,sort = {"updateTime"},direction = Sort.Direction.DESC)
Pageable pageable, NewQuery newquery, Model model){
model.addAttribute("page",newService.listNew(pageable,newquery));
return "admin/news::newList";

}

public void setTypeAndTag(Model model){
model.addAttribute("type",typeService.listType());
model.addAttribute("tags",tagService.listTag());
}

@GetMapping("/news/input")
public String input(Model model){
setTypeAndTag(model);
model.addAttribute("news",new News());
return INPUT;
}

@GetMapping("/news/{id}/toUpdate")
public String toUpdate(@PathVariable Long id,Model model){
setTypeAndTag(model);
News news=newService.getNew(id);
news.init();
model.addAttribute("news",news);
return INPUT;
}

@PostMapping("/news/add")
public String post(News news, RedirectAttributes attributes, HttpSession session){
news.setUser((User) session.getAttribute("user"));
news.setType(typeService.getType(news.getType().getId()));
news.setType((Type) tagService.listTag(news.getTagIds()));
News news1;
if (news.getId()==null){
news1=newService.saveNew(news);
}else {
news1=newService.updateNew(news.getId(),news);
}
if (news1==null){
attributes.addFlashAttribute("message","操作失败");
}else {
attributes.addFlashAttribute("message","操作成功");
}
return REDIRECT_LIST;
}

}

在新建新闻的时候,会涉及到已存在的标签和类型,所以需要在Tag那边新建一个查询的接口并将其实现。

public void init(){
this.tagIds = tagsToIds(this.getTags());
}

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;
}
}

总结

这一部分内容主要的难点是新闻的各种属性展示,一个新闻对应的点比较多,每个方面都要进行详细的设计,才符合最后的规范,设计的结果才能真正得到好的效果。同时,在设计List或者String类型时要格外注意格式以及各种方法,还有各种属性的一对多、多对一关系,都是比较重要又难入手的点。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐