<> one , to configure druid data source :
introduce druid package : What is introduced is druid-spring-boot-starter <properties> <java.version>1.8</
java.version> </properties> <dependencies> <dependency> <groupId>
org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</
artifactId> </dependency> <!-- Introduction of Druid data source , Direct introduction starter no jar--> <dependency> <groupId
>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <
version>1.1.14</version> </dependency> <dependency> <groupId>
org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</
artifactId> </dependency> <!-- Import mysql rely on --> <dependency> <groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </
dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId
>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <
exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine
</artifactId> </exclusion> </exclusions> </dependency> <!-- springboot Hot deployment --> <
dependency> <groupId>org.springframework.boot</groupId> <artifactId>
spring-boot-devtools</artifactId> <optional>true</optional> <scope>true</scope>
</dependency> </dependencies> <!-- Packaged as executable jar--> <build> <plugins> <plugin> <
groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin
</artifactId> </plugin> </plugins> </build>
<>application.yml:
spring: datasource: url: jdbc:mysql://localhost:3306/webstore?
serverTimezone=UTCusername: root # Be careful not to use data-username and data-password password:
123456 driver-class-name: com.mysql.cj.jdbc.Driver
<> test : stay test Class . You can see that the configuration is successful

<> two , principle : Why is one introduced starter You can use it directly druid The data source has changed ?

<>1.pom Introduced in starter The following packages are automatically introduced :

<>2. There is one under the bag DruidDataSourceAutoConfigure Is automatically configured in the druid Default configuration for :
public class DruidDataSourceAutoConfigure { private static final Logger LOGGER
= LoggerFactory.getLogger(DruidDataSourceAutoConfigure.class); public
DruidDataSourceAutoConfigure() { } @Bean( initMethod = "init" )
@ConditionalOnMissingBean // When there is no such thing in the container bean It will be assembled automatically when it is finished public DataSource dataSource()
{ LOGGER.info("Init DruidDataSource"); return new DruidDataSourceWrapper();
// Back to packaging druid Data sources for } } // Click in DruidDataSourceWrapper Go inside the class : @ConfigurationProperties(
"spring.datasource.druid")//@ConfigurationProperties annotation : Bind the fields and annotated classes specified in the configuration file ( method )
class DruidDataSourceWrapper extends DruidDataSource implements InitializingBean
{ // Loaded druid Of starter And then it's assembled automatically spring.datasource.druid Configuration of @Autowired private
DataSourceProperties basicProperties; // It's assembled here springboot default DataSourceProperties
// Click in DataSourceProperties inside @ConfigurationProperties(prefix =
"spring.datasource") public class DataSourceProperties implements
BeanClassLoaderAware, InitializingBean { // stay afterPropertiesSet Method public
void afterPropertiesSet() throws Exception {
/* hold springboot Some basic information of the default data source is synchronized , as username,password,url,driver.
That is, these properties can be used directly in the configuration file spring.datasource appoint , Equivalent to spring.datasource.druid Configure the information above .
However, if you configure data source information other than this information, you must use spring.datasource.druid appoint .
It is recommended here druid Data source is used directly spirng.datasource.druid appoint */ if (super.getUsername() == null
) { super.setUsername(this.basicProperties.determineUsername()); } if (super.
getPassword() == null) { super.setPassword(this.basicProperties.
determinePassword()); } if (super.getUrl() == null) { super.setUrl(this.
basicProperties.determineUrl()); } if (super.getDriverClassName() == null) {
super.setDriverClassName(this.basicProperties.getDriverClassName()); } }

The top two @ConfigurationProperties It can be found that druid-spring-boot-starter These basic properties have been configured by default username,password,url,driver,(spring.dataSource==spring.datasource.druid), You just need to add a dependency , All operations are handled as before . But if you use other properties , Must be used spring.datasource.druid

<> three ,druid Other attribute configuration of data source :

<>application.yml: Must use spring.datasource.druid:
spring: datasource: druid:
# Pay attention to the configuration url,username,password,driver Other configurations must be in the druid Write down , This will automatically package to DruidDataSourceWrapper inside
url: jdbc:mysql://localhost:3306/webstore?serverTimezone=UTC username: root
# Be careful not to use data-username and data-password password: 123456 driver-class-name:
com.mysql.cj.jdbc.Driver# open sql Statement auto loading , Only known as :
schema-all.sql and schema.sql and data-all.sql and data.sql Of sql sentence # initialization-mode:
always # Other configuration of data source 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
maxPoolPreparedStatementPerConnectionSize: 20 useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500 #
Configuration of monitoring statistics interception filters, Monitoring interface after removal sql Can't count ,'wall' For firewalls filters: stat,wall
# custom sql file name , After you click in, this is one list schema: - classpath:department.sql # Multiple files can be specified
test test : Whether breakpoint debugging loads the property information in the configuration file into the container :

<> four ,druid Monitoring configuration of :

druid The role of monitoring :
Yes druid Database connection pool for monitoring , Can view SQL/ URL / method Number of requests for , And so on , It is helpful to find and check the problems in the system in advance .

* druid The monitoring configuration properties of the DruidStatProperties
In the inner class of a class , In the configuration file , Must use spring.datasource.druid appoint .
*
If you use druid data source , The prefix of the required configuration file must be spirng.datasource.druid. The default is spring.datasource. Look at the principle analysis above .
Let's feel it first druid The use of monitoring ,
as : After a data query ,druid There will be monitoring sql record :
@Controller public class MyTemplateController { @Autowired
// Because you have a data source, you automatically assemble one JdbcTemplate, therefore JdbcTemplate It's already in the container JdbcTemplate jdbcTemplate;
@ResponseBody @GetMapping("query") public Map<String ,Object> query(){ List<Map<
String, Object>> list = jdbcTemplate.queryForList("select * from department");
return list.get(0); } }

druid Background page :

<>application.yml:
spring: datasource: druid: # Configure monitoring properties : stay druid-starter Of :
com.alibaba.druid.spring.boot.autoconfigure.stat Logical configuration under package web-stat-filter: #
WebStatFilter to configure , enabled: true # Default to false, Indicates not to be used WebStatFilter to configure , Is the attribute name to the short line
url-pattern: /* # Intercept all requests under the project exclusions: /druid/*,*.js,*.gif,*.jpg,*.bmp,*.png,
*.css,*.ico # Let go of these requests session-stat-enable: true principal-session-name:
session_nameprincipal-cookie-name: cookie_name stat-view-servlet: #
StatViewServlet to configure enabled: true # Default to false, Indicates not to be used StatViewServlet to configure , Is the attribute name to the short line
url-pattern: /druid/* # to configure DruidStatViewServlet Access address for . Access address of background monitoring page reset-enable:
false# Disable HTML On the page “ Reset ” function , All the monitored data will be cleared , Generally not used login-username: admin # Login user name of monitoring page
login-password: 123456 # Login password of monitoring page allow: 127.0.0.1,192.168.2.111
#IP White list ( Not configured or empty , All access is allowed ). Who is allowed to access druid backstage , All users are allowed to access by default . deny: #IP blacklist
( There is a common time ,deny Prior to allow). Who is not allowed to access druid backstage aop-patterns: com.lcf.service.*
#Spring Monitoring configuration , Please refer to Druid Github Wiki, to configure _Druid and Spring Associated monitoring configuration
<>application.properties: Use one of two
# Configuration of monitoring statistics interception filters, Monitoring interface after removal sql Can't count ,'wall' For firewalls spring.datasource.druid.filters=
stat,wall ####### Monitoring configuration #WebStatFilter to configure , Please refer to Druid Wiki, to configure _ to configure WebStatFilter
spring.datasource.druid.web-stat-filter.enabled=true
spring.datasource.druid.web-stat-filter.url-pattern=/*
spring.datasource.druid.web-stat-filter.exclusions=/druid/*,*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico
spring.datasource.druid.web-stat-filter.session-stat-enable=true
spring.datasource.druid.web-stat-filter.session-stat-max-count=10
spring.datasource.druid.web-stat-filter.principal-session-name=session_name
spring.datasource.druid.web-stat-filter.principal-cookie-name=cookie_name
spring.datasource.druid.web-stat-filter.profile-enable=
#StatViewServlet to configure , Please refer to Druid Wiki, to configure _StatViewServlet Configure default false
spring.datasource.druid.stat-view-servlet.enabled=true # to configure DruidStatViewServlet
spring.datasource.druid.stat-view-servlet.url-pattern=/druid/*
# Disable HTML On the page “Reset All” function
spring.datasource.druid.stat-view-servlet.reset-enable=false
spring.datasource.druid.stat-view-servlet.login-username=admin # Login user name of monitoring page
spring.datasource.druid.stat-view-servlet.login-password=123456 # Login password of monitoring page
#IP White list ( Not configured or empty , All access is allowed )
spring.datasource.druid.stat-view-servlet.allow=127.0.0.1,192.168.0.119 #IP blacklist
( There is a common time ,deny Prior to allow) spring.datasource.druid.stat-view-servlet.deny=
#Spring Monitoring configuration , Please refer to Druid Github Wiki, to configure _Druid and Spring Associated monitoring configuration
spring.datasource.druid.aop-patterns= com.lcf.service.*
<>application.yml Full configuration of :
spring: datasource: druid: url: jdbc:mysql://localhost:3306/webstore?
serverTimezone=UTCusername: root # Be careful not to use data-username and data-password password:
123456 driver-class-name: com.mysql.cj.jdbc.Driver # open sql Statement auto loading , Only known as :
schema-all.sql and schema.sql and data-all.sql and data.sql Of sql sentence # initialization-mode:
always # Other configuration of data source 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
maxPoolPreparedStatementPerConnectionSize: 20 useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500: #
Configuration of monitoring statistics interception filters, Monitoring interface after removal sql Can't count ,'wall' For firewalls filters: stat,wall # Configure monitoring properties :
stay druid-starter Of : com.alibaba.druid.spring.boot.autoconfigure.stat Logical configuration under package
web-stat-filter: # WebStatFilter to configure , enabled: true
# Default to false, Indicates not to be used WebStatFilter to configure , Is the attribute name to the short line url-pattern: /* # Intercept all requests under the project exclusions:
/druid/*,*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico # Let go of these requests session-stat-enable:
true principal-session-name: session_name principal-cookie-name: cookie_name
stat-view-servlet: # StatViewServlet to configure enabled: true
# Default to false, Indicates not to be used StatViewServlet to configure , Is the attribute name to the short line url-pattern: /druid/*
# to configure DruidStatViewServlet Access address for . Access address of background monitoring page reset-enable: false
# Disable HTML On the page “ Reset ” function , All the monitored data will be cleared , Generally not used login-username: admin # Login user name of monitoring page
login-password: 123456 # Login password of monitoring page allow: 127.0.0.1,192.168.2.111
#IP White list ( Not configured or empty , All access is allowed ). Who is allowed to access druid backstage , All users are allowed to access by default . deny: #IP blacklist
( There is a common time ,deny Prior to allow). Who is not allowed to access druid backstage , #Spring Monitoring configuration , Please refer to Druid Github
Wiki, to configure _Druid and Spring Associated monitoring configuration aop-patterns: com.lcf.service.*
# custom sql file name , After you click in, this is one list schema: - classpath:department.sql # Multiple files can be specified
If you have any questions, please point out

Technology
©2019-2020 Toolsou All rights reserved,
( Super detail )Eclipse Using tutorials —— use Eclipse Create first HelloWorld! Database operation 5 code implementation mysql Addition, deletion, modification and query of database What can MCU do , Do you have any interesting works made by MCU or open source hardware Go to the interview after reading this , Steady over ~~ Single linked list of primary data structure (C Language implementation )SQL Comprehensive questions Employee unit comprehensive questions Python Implementation of Hanoi Tower code VHDL——JK trigger It's over , Starting salary 30k