新建maven的project
image.png
image.png
创建包名
image.png
config:存放配置项,如安全配置、错误码配置等。
controller:控制层,其实就是api层,定义外部使用的接口。
dao:数据库交互层,用于获取和处理MySQL的数据。
pojo:实体类层,存放各种各样的实体类
response:定义返回结果的结构,便于统一使用。
services:有接口与接口对应的实现类,其中的impl就是真正实现逻辑的地方。
utils:工具类
为什么需要controller+services+servicesImpl这种结构?
其实主要是为了扩展和修改。其实就是把实现层和接口层分离出来。
如果只是controller+services,具体的逻辑全都在services层中编写,那万一需要更换数据库;或者扩展为另一种实现逻辑,但是旧逻辑又暂时不能删;那就只能一个个service改内部逻辑了。
但如果是controller+services+servicesImpl,那就只需要增加一个impl实现类,对接其他数据库也好,新逻辑也罢,都可以简单完成。
创建启动类
先配置好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>
<!--数据库-->
<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相关-->
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
<!--Redis 非关系型数据库-->
<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>
<!--构建配置-->
<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>
创建自己的App启动类
@SpringBootApplication
public class MallApp {
public static void main(String[] args) {
SpringApplication.run(MallApp.class,args);
}
}
自定义访问端口,需要创建application.ymlimage.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: 你数据库的用户名
password: 数据库密码
jpa:
show-sql: true
redis:
host: 192.168.1.6 # Redis服务器地址
port: 6379 # Redis服务器连接端口
password: 000000 # Redis服务器连接密码
大致上的配置算是配玩了,后面需要用到的,再加上。