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());
            // 处理连接异常的逻辑
        }
    }
}
相关推荐
砍材农夫27 分钟前
TCP和UDP区别
后端
IvorySQL1 小时前
PostgreSQL 技术日报 (3月7日)|生态更新与内核性能讨论
数据库·postgresql·开源
千寻girling1 小时前
一份不可多得的 《 Django 》 零基础入门教程
后端·python·面试
千寻girling1 小时前
Python 是用来做 AI 人工智能 的 , 不适合开发 Web 网站 | 《Web框架》
人工智能·后端·算法
贾铭1 小时前
如何实现一个网页版的剪映(三)使用fabric.js绘制时间轴
前端·后端
xiaoye20181 小时前
Spring 自定义 Redis 超时:TTL、TTI 与 Pipeline 实战
后端
赵渝强老师1 小时前
【赵渝强老师】金仓数据库的数据文件
数据库·国产数据库·kingbase·金仓数据库
程序员爱钓鱼4 小时前
GoHTML解析利器:github.com/PuerkitoBio/goquery实战指南
后端·google·go
golang学习记4 小时前
从“大泥球“到模块化单体:Spring Modulith + IntelliJ IDEA 拯救你的代码
后端·intellij idea
颜酱4 小时前
一步步实现字符串计算器:从「转整数」到「带括号与优化」
javascript·后端·算法