Home > Java > javaTutorial > body text

How SpringBoot integrates Druid data sources

WBOY
Release: 2023-05-15 14:58:13
forward
1168 people have browsed it

SprintBoot uses the HikariDataSource data source by default. This time it integrates a third-party data source Druid. It is an open source data source developed by Alibaba and is considered by many to be the best database connection pool in the Java language. Because Druid can provide a powerful set of monitoring and expansion capabilities.

By default, sprintboot uses hikaridatasource data source. This time, Druid, a third-party data source, is integrated. It is an open source data source developed by Alibaba, and many people consider it to be the best database connection pool in the Java language because Druid can provide a powerful set of monitoring and expansion functions.

1. When creating the SpringBoot project, add dependencies in pom.xml maven:

            <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
 
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
             <version>5.1.47</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
          
        </dependency>
Copy after login

Note: druid relies on the log4j log jar package, but SpringBoot uses slf4j logback by default. So just import the log4j jar package.

2. Add the corresponding configuration in application.yml (or aproperties):

# 
server:
  port: 80
 # 数据库连接信息
spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=GMT
    driver-class-name: com.mysql.cj.jdbc.Driver # com.mysql.jdbc.Driver
#   使用 Druid 数据源
    type: com.alibaba.druid.pool.DruidDataSource
Copy after login

3. Log4j.properties configuration file:

log4j.rootLogger = debug,stdout, D
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.Threshold = INFO
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p %m%n
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = ./log4j.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern=%d %p %m%n
Copy after login

4. Run the test method , view the data source

public class SpringbootdemoApplicationTests {
 
     @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private DataSource dataSource;
     
    @Test
    public void contextLoads() throws SQLException {
        System.out.println("dataSource==" + dataSource.getClass());
        Connection con = dataSource.getConnection();
        System.out.println("con==" + con);
         List<Map<String, Object>> maps = jdbcTemplate.queryForList("select * from user");
        System.out.println(maps);
    }
 
}
Copy after login

5, run the test method

How SpringBoot integrates Druid data sources

The above is the detailed content of How SpringBoot integrates Druid data sources. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template