Spring boot connects to database configuration and switches druid data source

We are using spring boot to connect to the data source.
First, we need to introduce dependencies

<!--        导入 jdbc-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
        </dependency>
        <!--        导入 mysql 驱动
        数据库版本要和区别版本一致
        -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.49</version>
        </dependency>

<!--        引入 druid 数据源-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.23</version>
        </dependency>

The second step is to write the database configuration file.
I use the properties style

#数据源
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/java_test?useSSL=false
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

#jdbc 请求超时配置
spring.jdbc.template.query-timeout=1000

test
insert image description here

This is using the default data source HikariDataSource

If you switch to use a third-party data source druid

First of all, introduce the dependency first

<!--        引入 druid 数据源-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.23</version>
        </dependency>

Write druid configuration file

insert image description here

package com.spring.bootinit.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * @User: Json
 * @Date: 2022/9/6
 **/
@Configuration
public class DruidConfig {
    
    

    // 自己设置一个 数据源
    // 当容器中 没有 自定义数据源  会导入 HikariPool 数据源
    @ConfigurationProperties("spring.datasource")
    // 写上这个注解 表示  这个 Druid 数据源的 配置参数 从配置文件中里面 spring.datasource 获取
    @Bean
    public DataSource dataSource() throws SQLException {
    
    
        DruidDataSource druidDataSource = new DruidDataSource();
        // 数据源配置信息 配置在这也不合适  一般会写在 配置文件中
//        druidDataSource.setUsername();
//        druidDataSource.setPassword();
//        等等
        druidDataSource.setFilters("stat"); //开启sql监控功能
        return  druidDataSource;
    }
    /**
     * 配置 Druid 的监控页
     * */
    @Bean
    public ServletRegistrationBean servletRegistrationBean(){
    
    

        StatViewServlet statViewServlet = new StatViewServlet();
        ServletRegistrationBean<StatViewServlet> statViewServletServletRegistrationBean = new ServletRegistrationBean<>(statViewServlet, "/druid/*");

        return statViewServletServletRegistrationBean;
    }
}

Test results
insert image description here
have switched the data source to DruidDataSource

The above is how the shared spring boot switches the data source of druid

Guess you like

Origin blog.csdn.net/Drug_/article/details/126717065