Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Druid in Spring Boot

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "how to use Druid in Spring Boot". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn how to use Druid in Spring Boot.

There are many database connection pools, such as c3p0, Druid, Hikari, and so on. The commonly used connection pool should be Ali open source Druid,Druid, which is not only a connection pool, but also has monitoring. In Matrix-Web, select Druid as the database connection pool.

Using Druid in Spring Boot

Druid is a database connection pool developed by Ali, which is popular in China and has the following characteristics:

Database access performance can be monitored. Druid built-in provides a powerful StatFilter plug-in that can count the performance of SQL in detail, which is helpful for online analysis of database access performance. The database password is encrypted. Writing the database password directly in the configuration file is a bad behavior and can easily lead to security problems. Both DruidDruiver and DruidDataSource support PasswordCallback. SQL execution log, Druid provides different LogFilter, which can support Common-Logging, Log4j and JdkLog. You can select the corresponding LogFilter as needed to monitor the database access of your application. Expand JDBC, if you want to have programming requirements for the JDBC layer, you can use the Filter mechanism provided by Druid to facilitate the preparation of JDBC layer extensions.

So how do you integrate Durid into a Spring Boot project? Introduce mysql-connector-java dependency, version 5.1.38, introduce druid-spring-boot-starter, version 1.1.10.

Mysql

Mysql-connector-java

${mysql.version}

Com.alibaba

Druid-spring-boot-starter

${durid.version}

Make the following configuration in the configuration file of the project:

Spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

Spring.datasource.driver-class-name=com.mysql.jdbc.Driver

Spring.datasource.url=jdbc:mysql://localhost:3306/druid

Spring.datasource.username=root

Spring.datasource.password=root

This integrates Durid connection pooling in Spring Boot.

Using HikariCP in Spring Boot

HikariCP is a high-performance JDBC connection pool, which has been improved and optimized based on BoneCP.

From the above results, we can see that the performance of HikariCP is much higher than that of c3p0, tomcat and other connection pools, so that later BoneCP authors give up maintenance and recommend people to use HikariCP on the home page of the Github project. In addition, Spring Boot will use HikariCP as its default JDBC connection pool in version 2.0.

HikariCP connection pool is used in Spring Boot, and HikariCP dependency is introduced in the project pom file, version 3.3.1.

Com.zaxxer

HikariCP

${HikariCP.version}

Then add the following configuration to the configuration file of the project:

Spring.datasource.type=com.zaxxer.hikari.HikariDataSource

Spring.datasource.driver-class-name=com.mysql.jdbc.Driver

Spring.datasource.url=jdbc:mysql://localhost:3306/druid

Spring.datasource.username=root

Spring.datasource.password=root so far, I believe you have a deeper understanding of "how to use Druid in Spring Boot". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report