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());
            // 处理连接异常的逻辑
        }
    }
}
相关推荐
鬼先生_sir几秒前
MySQL进阶-事务与锁机制
数据库·mysql·mvcc
treacle田25 分钟前
达梦数据库-达梦数据库中link链接访问远程Sql Sever-记录总结
数据库·达梦-sqlserver
神奇小汤圆25 分钟前
Spring-Boot-泛型封装-这8个坑让我调了3天
后端
深挖派26 分钟前
GoLand 2026.1 安装配置与环境搭建 (保姆级图文教程)
后端·golang·编辑器·go·goland
ClouGence27 分钟前
不用搭复杂系统,也能做跨地域数据迁移?
大数据·数据库·saas
IT枫斗者27 分钟前
构建具有执行功能的 AI Agent:基于工作记忆的任务规划与元认知监控架构
android·前端·vue.js·spring boot·后端·架构
xcjbqd029 分钟前
SQL中视图能否嵌套存储过程_实现复杂自动化报表逻辑
jvm·数据库·python
神奇小汤圆31 分钟前
一文吃透 MySQL 性能优化:从执行计划到架构设计
后端
听*雨声41 分钟前
软件设计师上午题5:数据库
数据库
开心就好202541 分钟前
苹果iOS应用开发上架与推广完整教程
后端·ios