| | |
| | | package com.iailab.framework.mybatis.core.util; |
| | | |
| | | import com.baomidou.dynamic.datasource.DynamicRoutingDataSource; |
| | | import com.baomidou.mybatisplus.annotation.DbType; |
| | | import com.iailab.framework.common.util.spring.SpringUtils; |
| | | import com.iailab.framework.mybatis.core.enums.DbTypeEnum; |
| | | |
| | | import javax.sql.DataSource; |
| | | import java.sql.Connection; |
| | | import java.sql.DriverManager; |
| | | import java.sql.SQLException; |
| | | |
| | | /** |
| | | * JDBC 工具类 |
| | |
| | | * @return DB 类型 |
| | | */ |
| | | public static DbType getDbType(String url) { |
| | | String name = com.alibaba.druid.util.JdbcUtils.getDbType(url, null); |
| | | return DbType.getDbType(name); |
| | | return com.baomidou.mybatisplus.extension.toolkit.JdbcUtils.getDbType(url); |
| | | } |
| | | |
| | | /** |
| | | * 通过当前数据库连接获得对应的 DB 类型 |
| | | * |
| | | * @return DB 类型 |
| | | */ |
| | | public static DbType getDbType() { |
| | | DynamicRoutingDataSource dynamicRoutingDataSource = SpringUtils.getBean(DynamicRoutingDataSource.class); |
| | | DataSource dataSource = dynamicRoutingDataSource.determineDataSource(); |
| | | try (Connection conn = dataSource.getConnection()) { |
| | | return DbTypeEnum.find(conn.getMetaData().getDatabaseProductName()); |
| | | } catch (SQLException e) { |
| | | throw new IllegalArgumentException(e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | } |