文章摘要: SpringBoot项目的配置文件热门推荐方案。
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}