一、开发模式。
前后端分离,前端发送请求到后端,后端接收请求后将数据传回前端,完成响应。开发前预备好接口文档给前后端同时使用。
二、环境搭建。
1.准备数据库:
2.利用SpringBoot创建后端工程文件:
3.引入所需依赖文件(web、mybatis、mysql驱动)):
4.配置文件application.yml中引入mybatis的配置信息:\
5.创建包结构,创建实体类:
三、实操。
1.新建数据库,使用提供的数据库sql在IDEA中执行,具体参考Spring Boot整合Mybatis内的数据库创建流程。创建完成如下所示:
-- 创建数据库
create database big_event;
-- 使用数据库
use big_event;
-- 用户表
create table user (
id int unsigned primary key auto_increment comment 'ID',
username varchar(20) not null unique comment '用户名',
password varchar(32) comment '密码',
nickname varchar(10) default '' comment '昵称',
email varchar(128) default '' comment '邮箱',
user_pic varchar(128) default '' comment '头像',
create_time datetime not null comment '创建时间',
update_time datetime not null comment '修改时间'
) comment '用户表';
-- 分类表
create table category(
id int unsigned primary key auto_increment comment 'ID',
category_name varchar(32) not null comment '分类名称',
category_alias varchar(32) not null comment '分类别名',
create_user int unsigned not null comment '创建人ID',
create_time datetime not null comment '创建时间',
update_time datetime not null comment '修改时间',
constraint fk_category_user foreign key (create_user) references user(id) -- 外键约束
);
-- 文章表
create table article(
id int unsigned primary key auto_increment comment 'ID',
title varchar(30) not null comment '文章标题',
content varchar(10000) not null comment '文章内容',
cover_img varchar(128) not null comment '文章封面',
state varchar(3) default '草稿' comment '文章状态: 只能是[已发布] 或者 [草稿]',
category_id int unsigned comment '文章分类ID',
create_user int unsigned not null comment '创建人ID',
create_time datetime not null comment '创建时间',
update_time datetime not null comment '修改时间',
constraint fk_article_category foreign key (category_id) references category(id),-- 外键约束
constraint fk_article_user foreign key (create_user) references user(id) -- 外键约束
)
2.创建Maven工程,引入所需依赖文件,添加resources目录与application.yml配置文件:
<!--继承父工程-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.5.0</version>
</parent>
<!--Web依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--Mybatis依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
<!--Mysql依赖-->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
</dependency>
3.在applocation.yml文件内编写有关数据库的配置信息:
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/big_event
username: xxxx
password: xxxx
4.创建外部的框架目录:
5. 在entity目录下编写实体类User.java,Category.java,Article.java,分别对应数据库建立的三张表:
User.java:
package org.azhyyi.pojo;
import java.time.LocalDateTime;
public class User {
private Integer id;//主键ID
private String username;//用户名
private String password;//密码
private String nickname;//昵称
private String email;//邮箱
private String userPic;//用户头像地址
private LocalDateTime createTime;//创建时间
private LocalDateTime updateTime;//更新时间
}
Category.java:
package org.azhyyi.pojo;
import java.time.LocalDateTime;
public class Category {
private Integer id;//主键ID
private String categoryName;//分类名称
private String categoryAlias;//分类别名
private Integer createUser;//创建人ID
private LocalDateTime createTime;//创建时间
private LocalDateTime updateTime;//更新时间
}
Article.java:
package org.azhyyi.pojo;
import java.time.LocalDateTime;
public class Article {
private Integer id;//主键ID
private String title;//文章标题
private String content;//文章内容
private String coverImg;//封面图像
private String state;//发布状态 已发布|草稿
private Integer categoryId;//文章分类id
private Integer createUser;//创建人ID
private LocalDateTime createTime;//创建时间
private LocalDateTime updateTime;//更新时间
}
6.修改启动类文件,重构文件名为:BigEventApplication,添加注解与固定的启动代码并测试运行是否有误,经过测试,发现项目正常运行且没有报错:
@SpringBootApplication
public class BigEventApplication
{
public static void main( String[] args )
{
SpringApplication.run(BigEventApplication.class,args);
}
}
7.至此,项目的整个后端工程框架完成。