druiddatasource配置_springboot2入门教程-2-配置

通过上一篇spring boot hello world教程,我们对spring boot有了一定了解,下面说说配置。本教程以springboot2.1.3.RELEASE版本讲解

默认配置文件

intellij idea中springboot项目会默认创建application.properties文件,application.yml文件替换application.properties文件,springboot启动默认读取这两个文件,留一个就好了。

application.properties示例:

server.port=8081

application.yml示例:

#注意:在yml文件中添加value值时,value前面需要加一个空格server: port: 8081

多环境配置,配置几个以application-开头的文件,如下

299a58a2a2534566bd5b3aed62616516

application.yml,在这个文件中spring.profiles.active意思是激活对应的文件,对应的文件,例如application-dev.yml中放着响应的ip地址、数据库账户和密码等。这里配置数据源只是为了演示需要。

# 请求配置

server:

port: 7725

servlet:

context-path: /learn2

# 数据库配置

spring:

profiles:

active: dev

application:

name: learn2

jackson:

date-format: yyyy-MM-dd HH:mm:ss

time-zone: GMT+8

# default-property-inclusion: non_null

mvc:

view:

prefix: classpath:/templates/

suffix: .html

thymeleaf:

encoding: UTF-8

cache: false

datasource:

url: jdbc:mysql://${database-ip}:${database-port}/shianyun?useUniCode=true&characterEncoding=UTF-8&zeroDataTimeBehavior=round&transformedBitIsBoolean=true&autoReconnect=true&failOverReadOnly=false&allowMultiQueries=true&serverTimezone=GMT%2B8

driver-class-name: com.mysql.cj.jdbc.Driver

type: com.alibaba.druid.pool.DruidDataSource

# 下面为连接池的补充设置,应用到上面所有数据源中

# 初始化大小,最小,最大

  initialSize: 1

  minIdle: 3

  maxActive: 20

# 配置获取连接等待超时的时间

  maxWait: 60000

# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒

  timeBetweenEvictionRunsMillis: 60000

# 配置一个连接在池中最小生存的时间,单位是毫秒

  minEvictableIdleTimeMillis: 30000

  validationQuery: select '

  testWhileIdle: true

  testOnBorrow: false

  testOnReturn: false

# 打开PSCache,并且指定每个连接上PSCache的大小

  poolPreparedStatements: true

  maxPoolPreparedStatementPerConnectionSize: 20

# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙

  filters: stat,wall,slf4j

# 通过connectProperties属性来打开mergeSql功能;慢SQL记录

  connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

# 合并多个DruidDataSource的监控数据

  useGlobalDataSourceStat: true

application-dev.yml,本地开发环境

database-ip: localhostdatabase-port: 3306​spring: datasource: username: root password: root

application-prod.yml,线上生产环境

database-ip: 47.110.123.3database-port: 3306​spring: datasource: username: xxx password: xxx​

application-test.yml,测试环境

database-ip: mysql

database-port: 3306

spring:

datasource:

username: root

password: root

有人可能不明白database-ip: mysql中mysql是什么意思,这是系统别名,mysql相当于一个key,即键,对应的值是ip地址,在Windows下如图

7f0acc6f806e439ca51a15b0d3015d6b
ddb7586afcbc47f88af8f98a8944f6da

在linux下如图,红框中 /etc/hosts为地址

e1ef97c8dd064246a3247a5682321c92

自定义值配置,即在上述yml配置文件中写值,然后在java文件中读取,如下

b99f7b611f124199b5b2c70fd5398594

读取上图中spring.application.name的值,启动项目,在postman中请求

d857afd621714635b2eeccc39c287344

@ConfigurationProperties,通过注解获取配置中的写入值,然后在对象中得到值,我们在application.yml中加如下代码

#自定义对象user: age: 11 username: 智谷说

定义UserProperties类

package com.zhigushuo.learn2;

import org.springframework.boot.context.properties.ConfigurationProperties;

import org.springframework.stereotype.Component;

@Component

@ConfigurationProperties(prefix = "user")

public class UserProperties {

private String username;

private Integer age;

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username= username;

}

public Integer getAge() {

return age;

}

public void setAge(Integer age) {

this.age = age;

}

}

注意,@ConfigurationProperties,pom.xm中需要加入以下依赖:

 org.springframework.boot spring-boot-configuration-processor true

不然会出现以下提示,这个提示不影响项目运行,也就说,可以不理会,想去掉这个提示,就加入上面的代码

9efd29bd81d94d9c907fa240aff40789

启动项目,在postman中请求

f92cdd645893461c85673e7056b52831

git地址:https://github.com/robot-king/springboot2.git 下learn2

欢迎关注、转发、收藏

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值