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.
<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.
# 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
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
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); } }
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!