一、搭建SpringBoot项目
这个项目,可以作为种子项目,我打算把它放置Gitee上。包含大部分web开发的相关功能,后期所有的Spring Boot项目都可以用这个项目,简单修改一下配置,就可以快速开发了。
- 选择Spring initializr,如果服务器URL这一栏一直在转圈(国外地址服务慢),可以使用https://start.aliyun.com/或者使用科学上网,然后完成组织名称、项目名称和项目描述,点击下一步。
-
创建项目时,选择稳定版本的就行(注意:版本不用太新)。依赖先都不勾选,后期一项一项加。
-
添加maven镜像
添加maven镜像加快依赖下载速度(可以选择在maven配置文件中设置或者在项目的pom文件中配置)
repositories> repository> id>aliyunid> name>Aliyun Maven Mirrorname> url>https://maven.aliyun.com/repository/publicurl> repository> repositories> pluginRepositories> pluginRepository> id>aliyunid> name>Aliyun Maven Mirrorname> url>https://maven.aliyun.com/repository/publicurl> pluginRepository> pluginRepositories>
-
pom文件
pom文件内容如下
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.0modelVersion> parent> groupId>org.springframework.bootgroupId> artifactId>spring-boot-starter-parentartifactId> version>2.7.12version> relativePath/> parent> groupId>com.guizhangroupId> artifactId>demo1artifactId> version>0.0.1-SNAPSHOTversion> properties> java.version>1.8java.version> properties> dependencies> dependency> groupId>org.springframework.bootgroupId> artifactId>spring-boot-starterartifactId> dependency> dependency> groupId>org.springframework.bootgroupId> artifactId>spring-boot-starter-testartifactId> scope>testscope> dependency> dependencies> build> plugins> plugin> groupId>org.springframework.bootgroupId> artifactId>spring-boot-maven-pluginartifactId> plugin> plugins> build> repositories> repository> id>aliyunid> name>Aliyun Maven Mirrorname> url>https://maven.aliyun.com/repository/publicurl> repository> repositories> pluginRepositories> pluginRepository> id>aliyunid> name>Aliyun Maven Mirrorname> url>https://maven.aliyun.com/repository/publicurl> pluginRepository> pluginRepositories> project>
二、Spring Boot 常用的依赖
(1) WEB 相关
-
web
用于web开发场景,包含了 RESTful 和 Spring MVC,并且默认使用了内置的Tomcat
dependency> groupId>org.springframework.bootgroupId> artifactId>spring-boot-starter-webartifactId> dependency>
-
aop
使用 Spring AOP 和 AspectJ的面向切面编程场景。
dependency> groupId>org.springframework.bootgroupId> artifactId>spring-boot-starter-aopartifactId> dependency>
-
thymeleaf
hymeleaf 是一个很强大的
视图解析
工具dependency> groupId>org.springframework.bootgroupId> artifactId>spring-boot-starter-thymeleafartifactId> dependency>
-
test
用于测试,提供了多个测试库,包括 JUnit Jupiter、Hamcrest 和 Mockito
dependency> groupId>org.springframework.bootgroupId> artifactId>spring-boot-starter-testartifactId> dependency>
-
security
dependency> groupId>org.springframework.bootgroupId> artifactId>spring-boot-starter-securityartifactId> dependency>
(2) DB 相关
-
mybatis
dependency> groupId>org.mybatis.spring.bootgroupId> artifactId>mybatis-spring-boot-starterartifactId> version>2.2.2version> dependency> dependency> groupId>com.baomidougroupId> artifactId>mybatis-plus-boot-starterartifactId> version>3.4.3.4version> dependency>
-
jdbc
dependency> groupId>org.springframework.bootgroupId> artifactId>spring-boot-starter-jdbcartifactId> dependency>
-
mysql
dependency> groupId>mysqlgroupId> artifactId>mysql-connector-javaartifactId> scope>runtimescope> dependency>
-
redis
dependency> groupId>org.springframework.bootgroupId> artifactId>spring-boot-starter-data-redisartifactId> dependency> dependency> groupId>org.redissongroupId> artifactId>redissonartifactId> version>3.16.6version> dependency>
-
mongodb
dependency> groupId>org.springframework.bootgroupId> artifactId>spring-boot-starter-data-mongodbartifactId> dependency>
(3) 辅助依赖
-
lombok
dependency> groupId>org.projectlombokgroupId> artifactId>lombokartifactId> optional>trueoptional> dependency>
-
swagger
dependency> groupId>io.springfoxgroupId> artifactId>springfox-swagger-uiartifactId> version>2.9.2version> dependency> dependency> groupId>io.springfoxgroupId> artifactId>springfox-swagger2artifactId> version>2.9.2version> dependency>
-
log
这个依赖会引入 Spring Boot 推荐的日志框架,默认情况下是 Logback
dependency> groupId>org.springframework.bootgroupId> artifactId>spring-boot-starter-loggingartifactId> dependency>
-
commons-lang3
commons-lang3是一个小而全的Java工具类库,类里面有很多对
时间、数组、数字、字符串
等的处理dependency> groupId>org.apache.commonsgroupId> artifactId>commons-lang3artifactId> version>3.12.0version> dependency>
-
commons-c
Apache Commons Codec 是一个用于编解码、加解密和哈希算法的 Java 库,提供了各种常见编码和加解密算法的实现,包括 Base64、URL 编码、MD5、SHA 等
dependency> groupId>commons-codecgroupId> artifactId>commons-cartifactId> version>1.11version> 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