springboot3基础开发文档
版本号
jdk 17
springboot 3
mysql 8.0
maven idea自带
springboot依赖注入pom.xml
命名规范:
- 官方提供的场景:命名为:
spring-boot-starter-*
- 第三方提供场景:命名为:
*-spring-boot-starter
父工程坐标
对各种常用依赖的版本进行了管理 ,我们的项目需要以这个项目为父工程,这样我们就不用操心依赖的版本问题了,需要什么依赖,直接引入坐标(不需要添加版本)即可。
<!-- 所有springboot项目都必须继承自 spring-boot-starter-parent -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
场景启动器
测试
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter-test</artifactId>
<version>3.0.3</version>
<scope>test</scope>
</dependency>
web启动器
提供了嵌入的Tomcat以及Spring MVC的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
spring-context
提供了Model类
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.1.4</version>
</dependency>
lombok
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
mybatis
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter-test</artifactId>
<version>3.0.3</version>
<scope>test</scope>
</dependency>
mysql8数据库
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
数据库连接池
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.1</version>
</dependency>
thymeleaf
前后端不分离,前端和后端进行数据传输的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
pagehelper
提供数据分页操作
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.6</version>
</dependency>
打包
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
application.yml配置文件
# 需要层次分明
# 固定的key
# 启动端口号
server:
port: 80 # 端口号设置
servlet:
context-path: /boot # 项目根路径
# mysql配置
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/${数据库名} # 自定义
username: root # 自定义
password: 123456 #自定义
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 基于druid配置
druid:
url: jdbc:mysql:///day01 # 自定义
username: root # 自定义
password: root # 自定义
driver-class-name: com.mysql.cj.jdbc.Driver
//url: jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT&useSSL=false
# mybatis配置
mybatis:
configuration: # setting配置
auto-mapping-behavior: full
map-underscore-to-camel-case: true #开启驼峰命名(java)和下划线命名(mysql)的自动转换
log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl
type-aliases-package: com.atguigu.pojo # 配置别名(自定义)
mapper-locations: classpath:/mapper/*.xml # mapper.xml位置(自定义)
多环境配置
创建开发、测试、生产三个环境的配置文件
application-dev.yml(开发):
spring:
jdbc:
datasource:
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://dev #自定义
username: root # 自定义
password: root # 自定义
application-test.yml(测试):
spring:
jdbc:
datasource:
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://test #自定义
username: root #自定义
password: root #自定义
application-prod.yml(生产):
spring:
jdbc:
datasource:
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://prod #自定义
username: root #自定义
password: root #自定义
application.yml
spring:
profiles:
active: dev
静态资源映射
默认将 /** 所有访问映射到以下目录
classpath:/static
classpath:/public
classpath:/resources
classpath:/META-INF/resources src/main/resources
正常情况下,我们只需要将我们的静态资源放到src/main/resource/static这个目录下即可正常访问,也不需要额外再去创建其他静态资源目录。
但是如果我们想自定义一下目录,则可以在application.properties添加spring.resources.static-locations:来指定位置
spring:
web:
resources:
static-locations: /META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
如果我们想将html页面资源放在src/main/resource/webapp下,只需设置spring.resources.static-locations=classpath:/webapp/,即可直接访问到,但是会覆盖前面的四条设置。所以直接在后面追加一条即可。
spring:
web:
resources:
static-locations: /META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/webapp/
直接指定一个硬盘上的任意目录
#自定义的属性,指定了一个路径,注意要以/结尾
upload-path=D:/verifies/
#会覆盖默认配置,所以需要将默认的也加上否则static、public等这些默认静态资源路径将不能再被使用
spring.web.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/webapp/,file:${upload-path}