介绍
emmmm,最近看了一些网络资料,也是心血来潮,想自己手工搭建一个java后端的初始化项目模板来简化一下开发,也就发一个模板的具体制作流程,(一步一步搭建,从易到难)
ok,现在开始搭建
第一步:创建SpringBoot项目
这一步大家应该都知道,然后呢,加上Spring Web,MyBatis,和MySQL Driver这三个依赖(后面会添加其他依赖,就先引入这几个吧)
添加完了之后查看一下pom文件,
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<artifactId>cetide</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.14</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.10</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.23</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.13</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
这就是项目我目前引入的依赖了,然后呢,先配置一下配置文件(这一点很重要)(这里用yml文件感觉更好一些)
spring.datasource.url=jdbc:mysql://localhost:3306/user_db?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=1234
mybatis.mapper-locations=classpath:com/cetide/init/dao/*.xml
spring.datasource.druid.stat-view-servlet.enabled=true
spring.datasource.druid.stat-view-servlet.url-pattern=/druid/*
spring.datasource.druid.stat-view-servlet.login-username=druid
spring.datasource.druid.stat-view-servlet.login-password=druid
spring.datasource.druid.stat-view-servlet.allow=
spring.datasource.druid.stat-view-servlet.deny=
spring.jackson.deserialization.fail-on-unknown-properties=false
spring.jackson.default-property-inclusion=non_null
emmm,连接上mysql之后可以试试启动一下项目,看看能不能正常运行,(我觉得这点就很重要,写一段就运行一下,不然写多了,运行失败找问题感觉就很麻烦)
行,也是成功运行了,这一步完成。
第二步:使用swagger规范一下
步骤一:引入依赖
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.2</version>
</dependency>
步骤二.在配置类中加入knife4j的相关配置
这里创建一个包config放置配置类,
创建一个WebConfig配置文件
package com.cetide.init.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
@Configuration
public class WebConfig {
@Bean
public Docket docket(){
ApiInfo apiInfo = new ApiInfoBuilder()
.title("cetide的接口文档")
.version("2.0")
.description("cetide的初始项目")
.build();
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo)
.select()
//指定生成接口需要扫描的包
.apis(RequestHandlerSelectors.basePackage("com.cetide.init"))
.paths(PathSelectors.any())
.build();
return docket;
}
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
做好扫描包和资源映射也就ok了,这个时候可以启动项目并且打开浏览器,
输入localhost:8080/doc.html#/home (这里要先确认自己的端口号是8080,如果没设置的话默认是8080)
出现了接口文档的话也就成功了
第三步:创建实体类
这里我以用户模块为例,目录新建model包,并创建dto,entity,enums,vo
在entity设计User实体类:
public class User implements Serializable {
private long id;
private String userAccount;
private String userName;
@JsonSerialize(using = NullSerializer.class)
private String pwd;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime gmtCreated;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime gmtModified;
private String userAvatar;
private String userProfile;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getUserAccount() {
return userAccount;
}
public void setUserAccount(String userAccount) {
this.userAccount = userAccount;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public LocalDateTime getGmtCreated() {
return gmtCreated;
}
public void setGmtCreated(LocalDateTime gmtCreated) {
this.gmtCreated = gmtCreated;
}
public LocalDateTime getGmtModified() {
return gmtModified;
}
public void setGmtModified(LocalDateTime gmtModified) {
this.gmtModified = gmtModified;
}
public String getUserAvatar() {
return userAvatar;
}
public void setUserAvatar(String userAvatar) {
this.userAvatar = userAvatar;
}
public String getUserProfile() {
return userProfile;
}
public void setUserProfile(String userProfile) {
this.userProfile = userProfile;
}
}
这里就不用lombok了手动getter和setter(主要还是之前遇到一些lombok的问题太过麻烦,虽然lombok方便,但是出点问题有时候也挺麻烦。)
这里也是设置了id,userAccount账号,userName昵称,密码pwd,用户头像avator,用户简介,更新时间,创建时间这些属性。(这些主要还是要在创建数据库表那会设计好有con)
创建完就OK了,至于dto和vo这块可以先放着,等到需要实现具体功能的时候再根据需求设计输入和需要输出的。
创建统一响应类:
public class Result<D> implements Serializable {
@JsonProperty("isSuccess")
private boolean success = false;
private String code;
private String message;
private D data;
public static <T> Result<T> create() {
return new Result<T>();
}
public boolean isSuccess() {
return success;
}
public Result setSuccess(boolean success) {
this.success = success;
return this;
}
public String getCode() {
return code;
}
public Result<D> setCode(String code) {
this.code = code;
return this;
}
public String getMessage() {
return message;
}
public Result<D> setMessage(String message) {
this.message = message;
return this;
}
public D getData() {
return data;
}
public Result<D> setData(D data) {
this.data = data;
return this;
}
}
这里用Result类作为统一响应类,emmmm,网上大多数好像都是这种,大差不差吧。
然后创建好controller,service(service.impl),dao这三层架构
目前的结构差不多就是这样了,没讲的其它包,之后也会讲到。目前就先这样吧