#1、New Project --> 选择 Spring Initializr -->依次填写图中信息
Name : 项目名 (b0902)
Location : 项目存储的位置 (我将它存储在 ~/Java/Practice)
Type : 依赖管理 (我选择用 Maven)
Group: 当前项目属于哪个公司、哪个组织 (com.huizhouyiren)
Artifact: 这个与 Name 同名 (IDEA自动填充)
Package name : 如果不指定 IDEA自动填充
Java : java 的版本,这里我选择 Java 8
Packaging: Jar (默认就好)
2、添加 spring boot 依赖
- 以下是我添加的依赖
- 最终生成的目录结构如下
项目目录结构 - src/main/java :项目的Java源代码存放目录
- B0902Application.java :项目的主文件
- resources:用于存放项目的资源文件
- resources/static:用于存放项目中的静态资源文件,例如css,js,image等
- resources/templates:存放项目中的视图模板页面
- application.properties:项目的主配置文件,也可以替换为application.yml文件(推荐)
- test/java:项目的Java测试源代码目录
- pom.xml:项目的依赖管理文件
3、将 application.properties 修改为 application.yml
- 我的
application.yml
配置文件内容
#Tomcat 配置(服务器配置)
server:
#设置请求端口
port: 8080
servlet:
#指定 Tomcat的请求路径
context-path: /index
#设置 Tomcat 编码格式
encoding:
charset: UTF-8
# 数据源配置
spring:
datasource:
#mysql的配置加载驱动类信息
driver-class-name: com.mysql.jdbc.Driver
#mysql的连接信息
url: jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone = GMT
#用户名
username: root
#密码
password: 123456
# Type 设置使用何种类型的数据源
type: com.alibaba.druid.pool.DruidDataSource
#redis配置
redis:
database: 0
# Redis服务器地址
host: 127.0.0.1
# Redis服务器连接端口
port: 6379
# Redis服务器连接密码(默认为空)
password:
jedis:
pool:
# 连接池最大连接数(使用负值表示没有限制)
max-active: 8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: -1
# 连接池中的最大空闲连接
max-idle: 8
# 连接池中的最小空闲连接
min-idle: 0
# 连接超时时间(毫秒)默认是2000ms
timeout: 2000ms
#Druid 数据源属性配置 (需要创建数据源配置类,进行配置才会生效)
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
#mybatis配置
mybatis:
#加载 mapper.xml 文件到容器中 (配置SQL映射文件路径)
mapper-locations: classpath:mapper/*.xml
# 别名,简化 mapper.xml 中请求响应参数类型
type-aliases-package: com.b0902.springboot.model
configuration:
#开启驼峰映射
map-underscore-to-camel-case: true
# sql日志的打印
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
#自定义注解
my:
name: forezp
age: 12
number: ${random.int}
uuid : ${random.uuid}
max: ${random.int(10)}
value: ${random.value}
greeting: hi,i'm ${my.name}
- 创建一个
javabean
来接收这个配置文件中的内容 参考链接
// 这里以解析自定义配置项为例
@ConfigurationProperties(prefix = "my") //加上这个注解,前缀是 :my
@Component
public class ConfigBean {
private String name;
private int age;
private int number;
private String uuid;
private int max;
private String value;
private String greeting;
- 在在应用类或者application类,加EnableConfigurationProperties注解(表示:我要使用这个配置)
这里我是加在 接口中
@RestController
@EnableConfigurationProperties({ConfigBean.class})
public class LucyController {
@Autowired
ConfigBean configBean;
@RequestMapping(value = "/lucy")
public String miya(){
return configBean.getGreeting()+" >>>>"+configBean.getName()+" >>>>"+ configBean.getUuid()+" >>>>"+configBean.getMax();
}
}
spring boot datasource 参数设置