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>
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
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
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); } }
5, run the test method
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!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Linux new version
SublimeText3 Linux latest version