新建maven的project

image.png

image.png
創(chuàng)建包名

image.png
config:存放配置項,如安全配置、錯誤碼配置等。
controller:控制層,其實就是api層,定義外部使用的接口。
dao:數(shù)據(jù)庫交互層,用于獲取和處理MySQL的數(shù)據(jù)。
pojo:實體類層,存放各種各樣的實體類
response:定義返回結(jié)果的結(jié)構(gòu),便于統(tǒng)一使用。
services:有接口與接口對應(yīng)的實現(xiàn)類,其中的impl就是真正實現(xiàn)邏輯的地方。
utils:工具類
為什么需要controller+services+servicesImpl這種結(jié)構(gòu)?
其實主要是為了擴展和修改。其實就是把實現(xiàn)層和接口層分離出來。
如果只是controller+services,具體的邏輯全都在services層中編寫,那萬一需要更換數(shù)據(jù)庫;或者擴展為另一種實現(xiàn)邏輯,但是舊邏輯又暫時不能刪;那就只能一個個service改內(nèi)部邏輯了。
但如果是controller+services+servicesImpl,那就只需要增加一個impl實現(xiàn)類,對接其他數(shù)據(jù)庫也好,新邏輯也罷,都可以簡單完成。
創(chuàng)建啟動類
先配置好pom.xml文件,增加以下配置項:
<!--spring-boot-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
</parent>
<groupId>org.example</groupId>
<artifactId>BeautifulBoxSystem</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging><!--增加打包模式,使用jar包打包方式-->
<!--配置名稱、url、有效年份-->
<name>beautiful-box-mall</name>
<url>https://www.beautifulbox.net</url>
<inceptionYear>2021-Now</inceptionYear>
<!--增加配置:java版本、編碼等-->
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<!--依賴管理-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!--添加依賴-->
<dependencies>
<!--spring-web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--數(shù)據(jù)庫-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- lombok log、自動get、set等-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
<!-- RESTful APIs swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!-- 集成security 密碼加密-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!--集成圖靈驗證碼-->
<dependency>
<groupId>com.github.whvcse</groupId>
<artifactId>easy-captcha</artifactId>
<version>1.6.2</version>
</dependency>
<!--mail相關(guān)-->
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
<!--Redis 非關(guān)系型數(shù)據(jù)庫-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--JWT token token生成器-->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.6.0</version>
</dependency>
</dependencies>
<!--構(gòu)建配置-->
<build>
<finalName>beautiful-box-1.0.0</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>net.beautifulbox.mall.MallApp</mainClass>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/libs-release</url>
</repository>
<repository>
<id>org.jboss.repository.releases</id>
<name>JBoss Maven Release Repository</name>
<url>https://repository.jboss.org/nexus/content/repositories/releases</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
創(chuàng)建自己的App啟動類
@SpringBootApplication
public class MallApp {
public static void main(String[] args) {
SpringApplication.run(MallApp.class,args);
}
}
自定義訪問端口,需要創(chuàng)建application.yml
image.png
server:
port: 2021
spring:
application:
name: beautiful_box_system
datasource:
url: jdbc:mysql://192.168.1.6:3306/beautiful_box_system?characterEncoding=utf-8&useSSL=false
driver-class-name: com.mysql.jdbc.Driver
username: 你數(shù)據(jù)庫的用戶名
password: 數(shù)據(jù)庫密碼
jpa:
show-sql: true
redis:
host: 192.168.1.6 # Redis服務(wù)器地址
port: 6379 # Redis服務(wù)器連接端口
password: 000000 # Redis服務(wù)器連接密碼
大致上的配置算是配玩了,后面需要用到的,再加上。