一、搭建SpringBoot项目
这个项目,可以作为种子项目,我打算把它放置Gitee上。包含大部分web开发的相关功能,后期所有的Spring Boot项目都可以用这个项目,简单修改一下配置,就可以快速开发了。
- 选择Spring initializr,如果服务器URL这一栏一直在转圈(国外地址服务慢),可以使用https://start.aliyun.com/或者使用科学上网,然后完成组织名称、项目名称和项目描述,点击下一步。
-
创建项目时,选择稳定版本的就行(注意:版本不用太新)。依赖先都不勾选,后期一项一项加。
-
添加maven镜像
添加maven镜像加快依赖下载速度(可以选择在maven配置文件中设置或者在项目的pom文件中配置)
<repositories> <repository> <id>aliyun</id> <name>Aliyun Maven Mirror</name> <url>https://maven.aliyun.com/repository/public</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>aliyun</id> <name>Aliyun Maven Mirror</name> <url>https://maven.aliyun.com/repository/public</url> </pluginRepository> </pluginRepositories>
-
pom文件
pom文件内容如下
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.12</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.guizhan</groupId> <artifactId>demo1</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>aliyun</id> <name>Aliyun Maven Mirror</name> <url>https://maven.aliyun.com/repository/public</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>aliyun</id> <name>Aliyun Maven Mirror</name> <url>https://maven.aliyun.com/repository/public</url> </pluginRepository> </pluginRepositories> </project>
二、Spring Boot 常用的依赖
(1) WEB 相关
-
web
用于web开发场景,包含了 RESTful 和 Spring MVC,并且默认使用了内置的Tomcat
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
-
aop
使用 Spring AOP 和 AspectJ的面向切面编程场景。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
-
thymeleaf
hymeleaf 是一个很强大的
视图解析
工具<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
-
test
用于测试,提供了多个测试库,包括 JUnit Jupiter、Hamcrest 和 Mockito
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency>
-
security
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
(2) DB 相关
-
mybatis
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.2</version> </dependency> <!-- mybatis-plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.3.4</version> </dependency>
-
jdbc
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency>
-
mysql
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
-
redis
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- redisson分布式锁 --> <dependency> <groupId>org.redisson</groupId> <artifactId>redisson</artifactId> <version>3.16.6</version> </dependency>
-
mongodb
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency>
(3) 辅助依赖
-
lombok
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency>
-
swagger
<!-- swagger-ui --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> <!-- swagger2 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency>
-
log
这个依赖会引入 Spring Boot 推荐的日志框架,默认情况下是 Logback
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </dependency>
-
commons-lang3
commons-lang3是一个小而全的Java工具类库,类里面有很多对
时间、数组、数字、字符串
等的处理<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.12.0</version> </dependency>
-
commons-c
Apache Commons Codec 是一个用于编解码、加解密和哈希算法的 Java 库,提供了各种常见编码和加解密算法的实现,包括 Base64、URL 编码、MD5、SHA 等
<dependency> <groupId>commons-codec</groupId> <artifactId>commons-c</artifactId> <version>1.11</version> </dependency>
三、配置文件
application.yaml配置文件的内容
server:
port: 8080
spring:
application:
name: demo1
datasource:
url: jdbc:mysql://localhost:3306/test?characterEncoding=UTF8&autoReconnect=true&serverTimezone=Asia/Shanghai&allowMultiQueries=true
driver-class-name: com.mysql.cj.jdbc.Driver
username: test
password: test
redis:
host: 127.0.0.1
port: 6379
# password:
mybatis:
mapper-locations: classpath:/mapper/**/*.xml
logging:
level:
com:
java:
wiki:
mapper: trace