mirror of
https://gitee.com/log4j/pig.git
synced 2024-12-22 12:48:58 +08:00
commit
4408a794ce
@ -17,6 +17,7 @@ CREATE TABLE `gen_datasource_conf` (
|
||||
`username` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '用户名',
|
||||
`password` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '密码',
|
||||
`del_flag` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '0' COMMENT '删除标记',
|
||||
`driver_class_name` varchar (255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '数据驱动名称',
|
||||
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
|
||||
`create_by` varchar(64) DEFAULT NULL COMMENT '创建人',
|
||||
`update_time` datetime DEFAULT NULL COMMENT '修改时间',
|
||||
|
@ -47,6 +47,7 @@ public class JdbcDynamicDataSourceProvider extends AbstractJdbcDataSourceProvide
|
||||
|
||||
/**
|
||||
* 执行语句获得数据源参数
|
||||
*
|
||||
* @param statement 语句
|
||||
* @return 数据源参数
|
||||
* @throws SQLException sql异常
|
||||
@ -61,8 +62,9 @@ public class JdbcDynamicDataSourceProvider extends AbstractJdbcDataSourceProvide
|
||||
String username = rs.getString(DataSourceConstants.DS_USER_NAME);
|
||||
String password = rs.getString(DataSourceConstants.DS_USER_PWD);
|
||||
String url = rs.getString(DataSourceConstants.DS_JDBC_URL);
|
||||
String driverClassName = rs.getString(DataSourceConstants.DS_DRIVER_CLASS_NAME);
|
||||
DataSourceProperty property = new DataSourceProperty();
|
||||
property.setDriverClassName(DataSourceConstants.DS_DRIVER);
|
||||
property.setDriverClassName(driverClassName);
|
||||
property.setUsername(username);
|
||||
property.setLazy(true);
|
||||
property.setPassword(stringEncryptor.decrypt(password));
|
||||
@ -76,7 +78,7 @@ public class JdbcDynamicDataSourceProvider extends AbstractJdbcDataSourceProvide
|
||||
property.setPassword(properties.getPassword());
|
||||
property.setUrl(properties.getUrl());
|
||||
property.setLazy(true);
|
||||
property.setDriverClassName(DataSourceConstants.DS_DRIVER);
|
||||
property.setDriverClassName(property.getDriverClassName());
|
||||
map.put(DataSourceConstants.DS_MASTER, property);
|
||||
return map;
|
||||
}
|
||||
|
@ -29,11 +29,6 @@ public interface DataSourceConstants {
|
||||
*/
|
||||
String DS_NAME = "name";
|
||||
|
||||
/**
|
||||
* 默认驱动
|
||||
*/
|
||||
String DS_DRIVER = "com.mysql.cj.jdbc.Driver";
|
||||
|
||||
/**
|
||||
* 默认数据源(master)
|
||||
*/
|
||||
@ -54,4 +49,9 @@ public interface DataSourceConstants {
|
||||
*/
|
||||
String DS_USER_PWD = "password";
|
||||
|
||||
/**
|
||||
* 驱动包名称
|
||||
*/
|
||||
String DS_DRIVER_CLASS_NAME = "driver_class_name";
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,76 @@
|
||||
package com.pig4cloud.pig.common.datasource.support;
|
||||
|
||||
/**
|
||||
* 数据库驱动ClassName
|
||||
*
|
||||
* @author huyuanxin
|
||||
* @date 2021-10-08
|
||||
*/
|
||||
public enum DriverClassNameConstants {
|
||||
|
||||
/**
|
||||
* MySQL Driver
|
||||
* After mysql-connector-java 6
|
||||
*/
|
||||
MYSQL_DRIVER("com.mysql.cj.jdbc.Driver"),
|
||||
|
||||
/**
|
||||
* Old MySQL Driver
|
||||
* Before mysql-connector-java 5
|
||||
*/
|
||||
OLD_MYSQL_DRIVER("com.mysql.jdbc.Driver"),
|
||||
|
||||
/**
|
||||
* Oracle Driver
|
||||
*/
|
||||
ORACLE_DRIVER("oracle.jdbc.OracleDriver"),
|
||||
|
||||
/**
|
||||
* Mariadb Driver
|
||||
*/
|
||||
MARIADB("org.mariadb.jdbc.Driver"),
|
||||
|
||||
/**
|
||||
* SqlServer Driver
|
||||
* SqlServer 2005+
|
||||
*/
|
||||
SQLSERVER_DRIVER("com.microsoft.sqlserver.jdbc.SQLServerDriver"),
|
||||
|
||||
/**
|
||||
* Old SqlServer Driver
|
||||
* SqlServer 2000
|
||||
*/
|
||||
OLD_SQLSERVER_DRIVER(" com.microsoft.jdbc.sqlserver.SQLServerDriver"),
|
||||
|
||||
/**
|
||||
* DB2 Driver
|
||||
*/
|
||||
DB2("com.ibm.db2.jcc.DB2Driver"),
|
||||
|
||||
/**
|
||||
* PostgreSQL Driver
|
||||
*/
|
||||
POSTGRE_SQL("org.postgresql.Driver"),
|
||||
|
||||
/**
|
||||
* Neo4j Bolt Driver
|
||||
*/
|
||||
NEO4J_BOLT("org.neo4j.jdbc.bolt.BoltDriver"),
|
||||
|
||||
/**
|
||||
* Neo4j Http Driver
|
||||
*/
|
||||
NEO4J_HTTP("org.neo4j.jdbc.http.HttpDriver");
|
||||
|
||||
private final String driverClassName;
|
||||
|
||||
|
||||
DriverClassNameConstants(String driverClassName) {
|
||||
this.driverClassName = driverClassName;
|
||||
}
|
||||
|
||||
public String getDriverClassName() {
|
||||
return driverClassName;
|
||||
}
|
||||
|
||||
}
|
@ -20,11 +20,16 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.pig4cloud.pig.codegen.entity.GenDatasourceConf;
|
||||
import com.pig4cloud.pig.codegen.service.GenDatasourceConfService;
|
||||
import com.pig4cloud.pig.common.core.util.R;
|
||||
import com.pig4cloud.pig.common.datasource.support.DriverClassNameConstants;
|
||||
import com.pig4cloud.pig.common.log.annotation.SysLog;
|
||||
import io.swagger.annotations.Api;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* 数据源管理
|
||||
*
|
||||
@ -37,11 +42,14 @@ import org.springframework.web.bind.annotation.*;
|
||||
@Api(value = "dsconf", tags = "数据源管理模块")
|
||||
public class GenDsConfController {
|
||||
|
||||
private static List<String> driverList = null;
|
||||
|
||||
private final GenDatasourceConfService datasourceConfService;
|
||||
|
||||
/**
|
||||
* 分页查询
|
||||
* @param page 分页对象
|
||||
*
|
||||
* @param page 分页对象
|
||||
* @param datasourceConf 数据源表
|
||||
* @return
|
||||
*/
|
||||
@ -52,6 +60,7 @@ public class GenDsConfController {
|
||||
|
||||
/**
|
||||
* 查询全部数据源
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/list")
|
||||
@ -61,6 +70,7 @@ public class GenDsConfController {
|
||||
|
||||
/**
|
||||
* 通过id查询数据源表
|
||||
*
|
||||
* @param id id
|
||||
* @return R
|
||||
*/
|
||||
@ -71,6 +81,7 @@ public class GenDsConfController {
|
||||
|
||||
/**
|
||||
* 新增数据源表
|
||||
*
|
||||
* @param datasourceConf 数据源表
|
||||
* @return R
|
||||
*/
|
||||
@ -82,6 +93,7 @@ public class GenDsConfController {
|
||||
|
||||
/**
|
||||
* 修改数据源表
|
||||
*
|
||||
* @param conf 数据源表
|
||||
* @return R
|
||||
*/
|
||||
@ -93,6 +105,7 @@ public class GenDsConfController {
|
||||
|
||||
/**
|
||||
* 通过id删除数据源表
|
||||
*
|
||||
* @param id id
|
||||
* @return R
|
||||
*/
|
||||
@ -102,4 +115,21 @@ public class GenDsConfController {
|
||||
return R.ok(datasourceConfService.removeByDsId(id));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得支持的数据库驱动
|
||||
*
|
||||
* @return 数据库驱动列表
|
||||
*/
|
||||
@SysLog("获得支持的数据库驱动")
|
||||
@GetMapping("/driver")
|
||||
public R<List<String>> driverList() {
|
||||
return Optional.ofNullable(driverList).map(R::ok).orElseGet(() -> {
|
||||
driverList = new ArrayList<>();
|
||||
for (DriverClassNameConstants driverClassNameConstant : DriverClassNameConstants.values()) {
|
||||
driverList.add(driverClassNameConstant.getDriverClassName());
|
||||
}
|
||||
return R.ok(driverList);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -59,6 +59,11 @@ public class GenDatasourceConf extends BaseEntity {
|
||||
*/
|
||||
private String password;
|
||||
|
||||
/**
|
||||
* 驱动类型
|
||||
*/
|
||||
private String driverClassName;
|
||||
|
||||
/**
|
||||
* 删除标记
|
||||
*/
|
||||
|
@ -24,7 +24,6 @@ import com.pig4cloud.pig.codegen.entity.GenDatasourceConf;
|
||||
import com.pig4cloud.pig.codegen.mapper.GenDatasourceConfMapper;
|
||||
import com.pig4cloud.pig.codegen.service.GenDatasourceConfService;
|
||||
import com.pig4cloud.pig.common.core.util.SpringContextHolder;
|
||||
import com.pig4cloud.pig.common.datasource.support.DataSourceConstants;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jasypt.encryption.StringEncryptor;
|
||||
@ -52,6 +51,7 @@ public class GenDatasourceConfServiceImpl extends ServiceImpl<GenDatasourceConfM
|
||||
|
||||
/**
|
||||
* 保存数据源并且加密
|
||||
*
|
||||
* @param conf
|
||||
* @return
|
||||
*/
|
||||
@ -73,6 +73,7 @@ public class GenDatasourceConfServiceImpl extends ServiceImpl<GenDatasourceConfM
|
||||
|
||||
/**
|
||||
* 更新数据源
|
||||
*
|
||||
* @param conf 数据源信息
|
||||
* @return
|
||||
*/
|
||||
@ -98,6 +99,7 @@ public class GenDatasourceConfServiceImpl extends ServiceImpl<GenDatasourceConfM
|
||||
|
||||
/**
|
||||
* 通过数据源名称删除
|
||||
*
|
||||
* @param dsId 数据源ID
|
||||
* @return
|
||||
*/
|
||||
@ -111,6 +113,7 @@ public class GenDatasourceConfServiceImpl extends ServiceImpl<GenDatasourceConfM
|
||||
|
||||
/**
|
||||
* 添加动态数据源
|
||||
*
|
||||
* @param conf 数据源信息
|
||||
*/
|
||||
@Override
|
||||
@ -120,7 +123,7 @@ public class GenDatasourceConfServiceImpl extends ServiceImpl<GenDatasourceConfM
|
||||
dataSourceProperty.setUrl(conf.getUrl());
|
||||
dataSourceProperty.setUsername(conf.getUsername());
|
||||
dataSourceProperty.setPassword(conf.getPassword());
|
||||
dataSourceProperty.setDriverClassName(DataSourceConstants.DS_DRIVER);
|
||||
dataSourceProperty.setDriverClassName(conf.getDriverClassName());
|
||||
dataSourceProperty.setLazy(true);
|
||||
DataSource dataSource = hikariDataSourceCreator.createDataSource(dataSourceProperty);
|
||||
SpringContextHolder.getBean(DynamicRoutingDataSource.class).addDataSource(dataSourceProperty.getPoolName(),
|
||||
@ -129,6 +132,7 @@ public class GenDatasourceConfServiceImpl extends ServiceImpl<GenDatasourceConfM
|
||||
|
||||
/**
|
||||
* 校验数据源配置是否有效
|
||||
*
|
||||
* @param conf 数据源信息
|
||||
* @return 有效/无效
|
||||
*/
|
||||
@ -136,8 +140,7 @@ public class GenDatasourceConfServiceImpl extends ServiceImpl<GenDatasourceConfM
|
||||
public Boolean checkDataSource(GenDatasourceConf conf) {
|
||||
try {
|
||||
DriverManager.getConnection(conf.getUrl(), conf.getUsername(), conf.getPassword());
|
||||
}
|
||||
catch (SQLException e) {
|
||||
} catch (SQLException e) {
|
||||
log.error("数据源配置 {} , 获取链接失败", conf.getName(), e);
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user