目 录
①导入系统所需要的jar包依赖:
②引入提供的静态文件(项目页面)
③项目配置文件说明
④引入实体类——在entity包下建立新闻、类型、评论、用户实体类
⑤建立实体类
⑥在项目的启动类加上包扫描注解——扫描mapper类
⑦编写主页面
⑧效果图
①导入系统所需要的jar包依赖:
pom.xml
4.0.0 org.springframework.boot spring-boot-starter-parent2.6.2 com.gec news-system0.0.1-SNAPSHOT news-system Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-thymeleaforg.springframework.boot spring-boot-devtoolsruntime true org.projectlombok lomboktrue org.springframework.boot spring-boot-starter-testtest mysql mysql-connector-javaruntime com.baomidou mybatis-plus-boot-starter3.4.1 commons-io commons-io2.6 commons-fileupload commons-fileupload1.3.3 org.springframework.boot spring-boot-maven-plugin
②引入提供的静态文件(项目页面)
-- mapper 是mybatis数据库的映射文件
-- static 是项目的静态文件(js包、css样式、图片等文件)
-- templates 是项目的页面模板
在resources文件夹下建立mapper文件夹,分别创建CommentsMapper.xml、NewsMapper.xml、TopicMapper.xml和UsersMapper.xml配置文件。
CommentsMapper.xml
cid, cnid, ccontent, cdate, cip, cauthor
NewsMapper.xml
nid, ntid, ntitle, nauthor, ncreateDate, npicPath, ncontent, nmodifyDate, nsummary select * from news order by ncreateDate desc and ntid = #{news.ntid}
TopicMapper.xml
tid, tname
UsersMapper.xml
uid, uname, upwd
静态文件statich和模板templates文件夹里面的代码文件会在最后项目完成后源码中给出!
③项目配置文件说明
application.properties
#加载驱动 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver #数据库连接路径 spring.datasource.url=jdbc:mysql://localhost:3306/newsmanagersystem?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC #数据库用户名 spring.datasource.username=root #数据库密码 spring.datasource.password=123456 #设置日志级别 logging.level.com.gec = debug #加载映射文件 mybatis-plus.mapper-locations=classpath*:/mapper*.xml #设置别名 mybatis-plus.type-aliases-package=com.gec.newssystem.entity #设置自动关联时允许按驼峰命名规则匹配(取消列名下划线设置) mybatis-plus.configuration.map-underscore-to-camel-case=false #关闭Thymeleaf缓存 spring.thymeleaf.cache=false
④引入实体类——在entity包下建立新闻、类型、评论、用户实体类
News.java
package com.gec.newssystem.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableField;
import java.io.Serializable;
import java.util.Date;
@TableName("news")
public class News implements Serializable {
private static final long serialVersionUID=1L;
//新闻编号
@TableId(value = "nid", type = IdType.AUTO)
private Integer nid;
//新闻分类编号 外键关联到新闻分类表
private Integer ntid;
//新闻标题
private String ntitle;
//新闻作者
private String nauthor;
//新闻创建时间
@TableField("ncreateDate")
private Date ncreateDate;
//新闻的图片地址
@TableField("npicPath")
private String npicPath;
//新闻内容
private String ncontent;
//新闻修改时间
@TableField("nmodifyDate")
private Date nmodifyDate;
//新闻摘要
private String nsummary;
public Integer getNid() {
return nid;
}
public void setNid(Integer nid) {
this.nid = nid;
}
public Integer getNtid() {
return ntid;
}
public void setNtid(Integer ntid) {
this.ntid = ntid;
}
public String getNtitle() {
return ntitle;
}
public void setNtitle(String ntitle) {
this.ntitle = ntitle;
}
public String getNauthor() {
return nauthor;
}
public void setNauthor(String nauthor) {
this.nauthor = nauthor;
}
public Date getNcreateDate() {
return ncreateDate;
}
public void setNcreateDate(Date ncreateDate) {
this.ncreateDate = ncreateDate;
}
public String getNpicPath() {
return npicPath;
}
public void setNpicPath(String npicPath) {
this.npicPath = npicPath;
}
public String getNcontent() {
return ncontent;
}
public void setNcontent(String ncontent) {
this.ncontent = ncontent;
}
public Date getNmodifyDate() {
return nmodifyDate;
}
public void setNmodifyDate(Date nmodifyDate) {
this.nmodifyDate = nmodifyDate;
}
public String getNsummary() {
return nsummary;
}
public void setNsummary(String nsummary) {
this.nsummary = nsummary;
}
@Override
public String toString() {
return "News{" +
"nid=" + nid +
", ntid=" + ntid +
", ntitle=" + ntitle +
", nauthor=" + nauthor +
", ncreateDate=" + ncreateDate +
", npicPath=" + npicPath +
", ncontent=" + ncontent +
", nmodifyDate=" + nmodifyDate +
", nsummary=" + nsummary +
"}";
}
}
Conments.java
package com.gec.newssystem.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import java.util.Date;
import java.io.Serializable;
@TableName("comments")
public class Comments implements Serializable {
private static final long serialVersionUID=1L;
//评论编号
@TableId(value = "cid", type = IdType.AUTO)
private Integer cid;
//新闻编号 外键跟新闻表作关联
private Integer cnid;
//评论内容
private String ccontent;
//评论时间
private Date cdate;
//发表评论的地址
private String cip;
//评论人的名称
private String cauthor;
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
public Integer getCnid() {
return cnid;
}
public void setCnid(Integer cnid) {
this.cnid = cnid;
}
public String getCcontent() {
return ccontent;
}
public void setCcontent(String ccontent) {
this.ccontent = ccontent;
}
public Date getCdate() {
return cdate;
}
public void setCdate(Date cdate) {
this.cdate = cdate;
}
public String getCip() {
return cip;
}
public void setCip(String cip) {
this.cip = cip;
}
public String getCauthor() {
return cauthor;
}
public void setCauthor(String cauthor) {
this.cauthor = cauthor;
}
@Override
public String toString() {
return "Comments{" +
"cid=" + cid +
", cnid=" + cnid +
", ccontent=" + ccontent +
", cdate=" + cdate +
", cip=" + cip +
", cauthor=" + cauthor +
"}";
}
}
Topic.java
package com.gec.newssystem.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import java.io.Serializable;
@TableName("topic")
public class Topic implements Serializable {
private static final long serialVersionUID=1L;
//分类编号-主键
@TableId(value = "tid", type = IdType.AUTO)
private Integer tid;
private String tname;
public Integer getTid() {
return tid;
}
public void setTid(Integer tid) {
this.tid = tid;
}
public String getTname() {
return tname;
}
public void setTname(String tname) {
this.tname = tname;
}
@Override
public String toString() {
return "Topic{" +
"tid=" + tid +
", tname=" + tname +
"}";
}
}
Users.java
package com.gec.newssystem.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import java.io.Serializable;
@TableName("news_users")
public class Users implements Serializable {
private static final long serialVersionUID=1L;
@TableId(value = "uid", type = IdType.AUTO)
private Integer uid;
private String uname;
private String upwd;
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUpwd() {
return upwd;
}
public void setUpwd(String upwd) {
this.upwd = upwd;
}
@Override
public String toString() {
return "Users{" +
"uid=" + uid +
", uname=" + uname +
", upwd=" + upwd +
"}";
}
}
在项目文件夹下新建一个vo文件夹,创建NewsQueryVo.java和TopicQueryVo.java文件
NewsQueryVo.java
package com.gec.newssystem.vo;
import lombok.Data;
@Data
public class NewsQueryVo {
private Integer ntid;
private Integer pageNo=1;
private Integer pageSize=10;
}
TopicQueryVo.java
package com.gec.newssystem.vo;
import lombok.Data;
@Data
public class NewsQueryVo {
private Integer ntid;
private Integer pageNo=1;
private Integer pageSize=10;
}
⑤建立实体类
在controller文件夹下建立HelloController.java文件
⑥在项目的启动类加上包扫描注解——扫描mapper类
在项目的启动类加上包扫描注解 不然会找不到mapper类
package com.gec.newssystem;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.gec.newssystem.dao")
@SpringBootApplication
public class NewsSystemApplication {
public static void main(String[] args) {
SpringApplication.run(NewsSystemApplication.class, args);
}
}
⑦编写主页面
启动项目访问index.html页面找不到路径:
springboot项目前端页面放在thymeleaf中无法通过浏览器地址栏访问。
在springboot项目中的resources目录下有template,是存放thymeleaf前端页面的(想要使用需要加入thymeleaf起步依赖,并在html标签加上xmlns:th="http://www.thymeleaf.org")
但是通过浏览器的导航栏是无法直接访问的,F12后确定了访问地址是正确的,但还是会报404错误。而通过controller的return可以访问,也就是说,必须要在后端发送请求才能到达thymeleaf。
但是thymeleaf是依赖于html的,为什么会无法直接访问呢?
在之前的SSM项目中,会有一个WEB-INF目录,这个目录下的前端页面也是无法直接访问,
是不是springboot项目也有这种隐私保护机制,防止用户访问到不想让用户访问的页面?
而后我又突然想到在SSM中如果想要访问WEB-INF目录下的页面的话,需要走中央调度器dispatcherServlet,springboot项目是帮我们集成了SSM,简化了开发,也就是说springboot也是要走中央调度器的。所以最终情况就是,放在template中的thymeleaf模板引擎的页面必须要通过中央调度器,就和以前的JSP放到WEB-INF中的情况是一样的。
引入页面会报错 需要删除index.html页面的的代码,将index.html页面进行修改,先暂时移除掉不需要的代码,不然会报500错误,改成下面这样子就不会报错!
index.html
新闻管理系统
欢迎您:[[${session.loginUser.uname}]] 进入控制台 退出
幻想中穿越时空 国庆多变的发型 新技术照亮都市 群星闪耀红地毯



