Springboot新闻管理系统1

一、自动生成数据库表
Springboot 之 Hibernate自动建表(Mysql)
先引入Maven依赖包

org.springframework.boot
spring-boot-starter-data-jpa


mysql
mysql-connector-java

创建配置文件application.yml
server:
port: 8082
spring:
datasource:
url: jdbc:mysql://localhost:3306/tnews?characterEncoding=utf-8&serverTimezone=Asia/Shanghai
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
//自动创建数据库表
jpa:
hibernate:
ddl-auto: update
show-sql: true//在控制台显示sql语句
本次项目po包中主要有五个实体类:
1、 在主建Id上需要加注释:@Id和@GeneratedValue(strategy = GenerationType.AUTO)才会自动增长
2、 在需要重新设置表字段名的属性上加注释@Column(name = “字段名”)即可。
3、 在类名上添加注释:@Entity和@Table(name = “t_user”),t_user是表名
Comment.java
package com.wzx.po;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Entity
@Table(name = “t_comment”)
public class Comment {

@Id
@GeneratedValue
private Long id;
private String nickname;
private String email;
private String content;
private String avatar;
@Temporal(TemporalType.TIMESTAMP)
private Date createTime;

@ManyToOne
private News news;

@OneToMany(mappedBy = "parentComment")
private List<Comment> replyComments = new ArrayList<>();

@ManyToOne
private Comment parentComment;

private boolean adminComment;

public Comment() {
}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getNickname() {
    return nickname;
}

public void setNickname(String nickname) {
    this.nickname = nickname;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getContent() {
    return content;
}

public void setContent(String content) {
    this.content = content;
}

public String getAvatar() {
    return avatar;
}

public void setAvatar(String avatar) {
    this.avatar = avatar;
}

public Date getCreateTime() {
    return createTime;
}

public void setCreateTime(Date createTime) {
    this.createTime = createTime;
}



public List<Comment> getReplyComments() {
    return replyComments;
}

public void setReplyComments(List<Comment> replyComments) {
    this.replyComments = replyComments;
}

public Comment getParentComment() {
    return parentComment;
}

public void setParentComment(Comment parentComment) {
    this.parentComment = parentComment;
}

public boolean isAdminComment() {
    return adminComment;
}

public void setAdminComment(boolean adminComment) {
    this.adminComment = adminComment;
}

public News getNews() {
    return news;
}

public void setNews(News news) {
    this.news = news;
}

@Override
public String toString() {
    return "Comment{" +
            "id=" + id +
            ", nickname='" + nickname + '\'' +
            ", email='" + email + '\'' +
            ", content='" + content + '\'' +
            ", avatar='" + avatar + '\'' +
            ", createTime=" + createTime +
            ", news=" + news +
            ", replyComments=" + replyComments +
            ", parentComment=" + parentComment +
            ", adminComment=" + adminComment +
            '}';
}

}
News.java
package com.wzx.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
private Long id;

private String title;

@Basic(fetch = FetchType.LAZY)
@Lob
private String content;
private String firstPicture;
private String flag;
private Integer 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;

@ManyToMany(cascade = {CascadeType.PERSIST})
private List<Tag> tags = new ArrayList<>();


@ManyToOne
private User user;

@OneToMany(mappedBy = "news")
private List<Comment> comments = 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 Integer getViews() {
    return views;
}

public void setViews(Integer 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 List<Tag> getTags() {
    return tags;
}

public void setTags(List<Tag> tags) {
    this.tags = tags;
}


public User getUser() {
    return user;
}

public void setUser(User user) {
    this.user = user;
}


public List<Comment> getComments() {
    return comments;
}

public void setComments(List<Comment> comments) {
    this.comments = comments;
}


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

//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 +
            ", tags=" + tags +
            ", user=" + user +
            ", comments=" + comments +
            ", tagIds='" + tagIds + '\'' +
            ", description='" + description + '\'' +
            '}';
}

public void initTags(Long id) {
    //3,4,5
    List<Tag> tags = this.getTags();
    StringBuffer ids=new StringBuffer();
    if(!tags.isEmpty()){
        Boolean flag=false;
        for(Tag t:tags){
            if(flag){
                ids.append(t.getId());
                flag=true;
            }else {
                ids.append(",");
                ids.append(t.getId());
            }

        }
        this.setTagIds(ids.toString());
    }

}

}

Tag.java

package com.wzx.po;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Entity
@Table(name = “t_tag”)
public class Tag {

@Id
@GeneratedValue
private Long id;
private String name;

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

public List<News> getNewsList() {
    return newsList;
}

public void setNewsList(List<News> newsList) {
    this.newsList = newsList;
}

@Override
public String toString() {
    return "Tag{" +
            "id=" + id +
            ", name='" + name + '\'' +
            '}';
}

}

Type.java

package com.wzx.po;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Entity
@Table(name = “t_type”)
public class Type {

@Id
@GeneratedValue
private Long id;

private String name;

@OneToMany(mappedBy = "type")
private List<News> newsList = new ArrayList<>();

public Type() {
}

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

public List<News> getNewsList() {
    return newsList;
}

public void setNewsList(List<News> newsList) {
    this.newsList = newsList;
}

@Override
public String toString() {
    return "Type{" +
            "id=" + id +
            ", name='" + name + '\'' +
            '}';
}

}

User.java

package com.wzx.po;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Entity
@Table(name = “t_user”)
public class User {

@Id
@GeneratedValue
private Long id;
private String nickname;
private String username;
private String password;
private String email;
private String avatar;
private Integer type;
@Temporal(TemporalType.TIMESTAMP)
private Date createTime;
@Temporal(TemporalType.TIMESTAMP)
private Date updateTime;

@OneToMany(mappedBy = "user")
private List<News> newsList = new ArrayList<>();

public User() {
}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getNickname() {
    return nickname;
}

public void setNickname(String nickname) {
    this.nickname = nickname;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getAvatar() {
    return avatar;
}

public void setAvatar(String avatar) {
    this.avatar = avatar;
}

public Integer getType() {
    return type;
}

public void setType(Integer type) {
    this.type = type;
}

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<News> getNewsList() {
    return newsList;
}

public void setNewsList(List<News> newsList) {
    this.newsList = newsList;
}

@Override
public String toString() {
    return "User{" +
            "id=" + id +
            ", nickname='" + nickname + '\'' +
            ", username='" + username + '\'' +
            ", password='" + password + '\'' +
            ", email='" + email + '\'' +
            ", avatar='" + avatar + '\'' +
            ", type=" + type +
            ", createTime=" + createTime +
            ", updateTime=" + updateTime +
            '}';
}

}

启动项目后在tnews数据库中将出现数据表,且存在相应的表字段。
在这里插入图片描述
Springboot中先在实体类中建立与存储过程的联系,dao层调用存储过程,控制层调用dao层方法,dao层继承JpaRepository类实现简单的对数据库的操作。

二、登录注销功能
Springboot项目与SSM项目类似,只不过Springboot中的一些sql操作不用自己实现
(一)UserDao.java

package com.wzx.dao;

import com.wzx.po.User;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserDao extends JpaRepository<User,Long> {
User findByUsernameAndPassword(String username, String password);
}

(二)UserService.java

package com.wzx.service;

import com.wzx.po.User;
import org.springframework.stereotype.Service;

public interface UserService {
User CheckUser(String username, String password);
}

UserServiceImpl.java

package com.wzx.service.impl;

import com.wzx.dao.UserDao;
import com.wzx.po.User;
import com.wzx.service.UserService;
import com.wzx.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;

@Override
public User CheckUser(String username, String password) {
    return userDao.findByUsernameAndPassword(username, MD5Util.code(password));
}

}

(三)LoginController.java

package com.wzx.controller;

import com.wzx.po.User;
import com.wzx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.GetMapping;
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;

@Controller
@RequestMapping(“admin”)
public class LoginController {
@Autowired
private UserService userService;

@GetMapping
public String toLogin(){
return “admin/login”;
}

@PostMapping("login")
public String login(String username, String password, HttpSession session, RedirectAttributes redirectAttributes){
    User user=userService.CheckUser(username,password);
            if(user!=null){
                session.setAttribute("user",user);
                return "admin/index";
            }else {
                redirectAttributes.addFlashAttribute("message","用户名和密码错误1");
                return "redirect:/admin";
            }
}

@GetMapping("logout")
public String logout(HttpSession session){
    session.removeAttribute("user");
    return "admin/login";

}

}

(四)login.html

新闻管理登录


管理后台登录

登 录
       <div class="ui error mini message"></div>
       <div class="ui mini negative message" th:unless="${#strings.isEmpty(message)}" th:text="${message}">用户名密码错误</div>

     </form>

   </div>
 </div>

<th:block th:fragment=“script”>

</th:block>

index.html

新闻管理

Hi,

hualili,欢迎登录!

  <img src="../static/images/wechat.jpg" alt="" class="ui rounded bordered fluid image">
</div>


三、分类显示
SSM项目中分页是通过新建了一个PageInfo实体类来实现的,Springboot中是通过@PageableDefault来实现的
TypeController.java

package com.wzx.controller;

import com.wzx.po.Type;
import com.wzx.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
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;

@Controller
@RequestMapping("/admin/types")
public class TypeController {

@Autowired
private TypeService typeService;

@RequestMapping
public String list(@PageableDefault(size = 5,sort = {"id"},direction = Sort.Direction.DESC) Pageable pageable, Model model){
    Page<Type> page=typeService.listType(pageable);
    model.addAttribute("page",page);
    return "admin/types";
}

}

TypeService.java

import com.wzx.po.Type;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;
import java.util.Optional;

public interface TypeService {
Page listType(Pageable pageable);
}

TypeServiceImpl.java

package com.wzx.service.impl;

import com.wzx.dao.TypeDao;
import com.wzx.po.Type;
import com.wzx.service.TypeService;
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.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class TypeServiceImpl implements TypeService {
@Autowired
private TypeDao typeDao;

@Override
public Page<Type> listType(Pageable pageable) {
    return typeDao.findAll(pageable);
}

}

types.html

分类管理
提示:

恭喜,操作成功!

名称操作
1刻意练习清单 编辑 删除
新增


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot是一个用于构建独立的、生产级的Spring应用程序的框架。它简化了Spring应用程序的配置和部署过程,并提供了一套强大的开发工具和约定,使开发人员能够更专注于业务逻辑的实现。 对于新闻管理系统的搭建,使用Spring Boot可以提供以下优势: 1. 快速启动和开发:Spring Boot提供了自动配置和快速启动的特性,可以快速搭建一个基本的新闻管理系统,并且可以通过添加所需的依赖来扩展功能。 2. 简化配置:Spring Boot通过自动配置和约定大于配置的原则,减少了繁琐的配置工作,开发人员可以更专注于业务逻辑的实现。 3. 内嵌服务器:Spring Boot内置了Tomcat、Jetty等常用的Web服务器,可以方便地进行开发和测试,无需额外安装和配置。 4. 微服务支持:Spring Boot与Spring Cloud等微服务框架结合使用,可以构建分布式的新闻管理系统,实现高可用性和可扩展性。 对于新闻管理系统的首页信息,可以包含以下内容: - 首页:展示系统的概览信息和导航链接。 - 公告消息:发布系统的公告和通知信息。 - 交流论坛:提供用户之间交流和讨论的平台。 - 新闻资讯:展示最新的新闻和资讯信息。 - 意见反馈:接收用户对系统的意见和建议。 - 视频分享:分享与新闻相关的视频内容。 通过使用Spring Boot和MySQL,可以实现新闻管理系统的整体程序运行。MySQL作为关系型数据库,可以存储和管理新闻、用户、评论等相关数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值