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());
            // 处理连接异常的逻辑
        }
    }
}
相关推荐
爱吃羊的老虎11 小时前
【大模型】向量数据库:Chroma、Weaviate、Qdrant
数据库·语言模型
数据大魔方11 小时前
【期货量化实战】跨期套利策略:价差交易完整指南(TqSdk源码详解)
数据库·python·算法·github·程序员创富
l1t11 小时前
NineData第三届数据库编程大赛:用一条 SQL 解数独问题我的参赛程序
数据库·人工智能·sql·算法·postgresql·oracle·数独
smileNicky12 小时前
SpringBoot系列之集成Pulsar教程
java·spring boot·后端
麦麦大数据12 小时前
J009 美食推荐可视化大数据系统vue+springboot
vue.js·spring boot·mysql·推荐算法·美食·可视化分析·沙箱支付
rfidunion12 小时前
springboot+VUE+部署(1。新建项目)
java·vue.js·spring boot
小翰子_12 小时前
Spring Boot整合Sharding-JDBC实现日志表按月按周分表实战
java·spring boot·后端
千寻技术帮13 小时前
10347_基于Springboot的新疆旅游管理系统
spring boot·mysql·旅游·在线旅游
施嘉伟13 小时前
一次生产环境 SQL 不走索引的排查过程
数据库·sql
踏浪无痕13 小时前
SQLInsight:从JDBC底层到API调用的零侵入SQL监控方案
数据库·后端·开源