springboot项目数据库配置类DatabaseConfig实现代码

1:yml配置类

yaml 复制代码
spring:
  datasource:
    name: text
    url: jdbc:mysql://192.168.11.50:3306/dsdd?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true&serverTimezone=UTC
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver

这样启动项目,没有检查到是否连接数据库成功

2:数据库配置类(添加数据库检查)

java 复制代码
package com.example.poi.utils;

/**
 * @Author xu
 * @create 2023/8/22 21
 */
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

@Configuration
public class DatabaseConfig {

    @Value("${spring.datasource.url}")
    private String dataSourceUrl;

    @Value("${spring.datasource.username}")
    private String dataSourceUsername;

    @Value("${spring.datasource.password}")
    private String dataSourcePassword;

    @Value("${spring.datasource.driver-class-name}")
    private String dataSourceDriverClassName;

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setUrl(dataSourceUrl);
        dataSource.setUsername(dataSourceUsername);
        dataSource.setPassword(dataSourcePassword);
        dataSource.setDriverClassName(dataSourceDriverClassName);
        testConnection(dataSource); // 调用检查连接方法
        return dataSource;
    }

    private void testConnection(DataSource dataSource) {
        try (Connection connection = dataSource.getConnection()) {
            System.out.println("数据库连接正常!");
        } catch (SQLException e) {
            // 连接异常处理
            //e.printStackTrace();
            throw new RuntimeException("数据库连接异常!");
        }
    }
}

3:或者通过在启动类添加数据库检测

java 复制代码
@SpringBootApplication
public class YourApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(YourApplication.class, args);

        // 检查数据库连接是否正常
        try {
            /** 获取DataSource bean,并调用getConnection()方法测试连接*/
            context.getBean(javax.sql.DataSource.class).getConnection();
            System.out.println("数据库连接正常!");
        } catch (Exception e) {
            System.err.println("数据库连接异常:" + e.getMessage());
            // 处理连接异常的逻辑
        }
    }
}
相关推荐
WeiLai11121 分钟前
面试基础----ReentrantLock vs Synchronized
java·分布式·后端·面试·职场和发展·架构
小林熬夜学编程35 分钟前
【MySQL】第十一弹---复合查询全攻略:多表、自连接、子查询与合并查询
android·linux·开发语言·数据库·mysql·算法
老友@1 小时前
在 Spring Boot 中使用异步线程时的 HttpServletRequest 复用问题
java·spring boot·后端·tomcat·多线程·request·异步线程
qq_5470261791 小时前
Spring Boot 事件机制
java·spring boot·后端
web130933203981 小时前
Spring Cloud Alibaba与Spring Boot、Spring Cloud版本对应关系
java·spring boot·spring cloud
qq_13948428821 小时前
springboot417-基于Spring Boot的酒店后台管理系统(源码+数据库+纯前后端分离+部署讲解等)
java·数据库·spring boot·后端·spring·maven·intellij-idea
钝挫力PROGRAMER1 小时前
Linux中停服导致问题的思考
linux·运维·后端
m0_748254091 小时前
【MySQL 的数据目录】
数据库·mysql·adb
码熔burning1 小时前
Spring Boot全局异常处理:“危机公关”团队
java·spring boot·后端
一个热爱生活的普通人2 小时前
浅谈 Go 的 Web 框架 Echo 是如何处理 RESTful 调用的
后端·go