优化 数据源,添加,修改,删除,测试连接

dev
chao 2024-04-21 19:34:31 +08:00
parent 1378c3f5e8
commit 857423dcd9
4 changed files with 70 additions and 42 deletions

View File

@ -38,4 +38,16 @@ public class DataType extends BaseEntity {
@Excel(name = "数据源类型") @Excel(name = "数据源类型")
private String dataType; private String dataType;
/**
*
*/
@Excel(name = "注册驱动")
private String driverManager;
/**
* jdbc
*/
@Excel(name = "jdbc前缀")
private String jdbcPre;
} }

View File

@ -143,46 +143,43 @@ public class DataSourceServiceImpl extends ServiceImpl<DataSourceMapper, DataSou
new LambdaQueryWrapper<DataSource>() new LambdaQueryWrapper<DataSource>()
.eq(DataSource::getId, id) .eq(DataSource::getId, id)
); );
String driveClass = "";
String jdbcUrl = ""; String jdbcUrl = "";
String driveClass = "";
boolean flag = false; boolean flag = false;
DataType dataType = dataTypeService.getOne(
new LambdaQueryWrapper<DataType>()
.eq(DataType::getId, dataSource.getTypeId())
);
try { try {
switch (dataSource.getTypeId().intValue()) { if (dataType.getDriverManager() != null && dataType.getJdbcPre() != null){
case 1: if ("mysql".equals(dataType.getDataType())){
driveClass = "com.mysql.cj.jdbc.Driver"; driveClass = dataType.getDriverManager();
jdbcUrl = "jdbc:mysql://" + dataSource.getDataSourceIp() + ":" + dataSource.getDataSourcePort() + "/" + dataSource.getDataSourceDatabaseName() + "?" + dataSource.getAdditionalConfiguration(); jdbcUrl = dataType.getJdbcPre() + dataSource.getDataSourceIp() + ":" + dataSource.getDataSourcePort() + "/" + dataSource.getDataSourceDatabaseName() + "?" + dataSource.getAdditionalConfiguration();
break; }
case 2: if ("oracle".equals(dataType.getDataType())){
// redis driveClass = dataType.getDriverManager();
//连接指定的redis jdbcUrl = dataType.getJdbcPre() + dataSource.getDataSourceIp() + ":" + dataSource.getDataSourcePort() + ":" + dataSource.getDataSourceDatabaseName();
Jedis jedis = new Jedis(dataSource.getDataSourceIp(), Integer.valueOf(dataSource.getDataSourcePort())); }
//如果有密码则需要下面这一行 if ("sqlserver".equals(dataType.getDataType())){
if (dataSource.getDataSourcePassword() != null && !dataSource.getDataSourcePassword().equals("")){ driveClass = dataType.getDriverManager();
jedis.auth(dataSource.getDataSourcePassword()); jdbcUrl = dataType.getJdbcPre()+ dataSource.getDataSourceIp() +":"+dataSource.getDataSourcePort()+";databaseName="+dataSource.getDataSourceDatabaseName();
} }
//查看服务是否运行,运行正常的话返回一个PONG否则返回一个连接错误
try {
jedis.ping();
flag = true;
} catch (Exception e) {
flag = false;
}
break;
case 3:
// oracle数据库
driveClass = "oracle.jdbc.OracleDriver";
jdbcUrl = "jdbc:oracle:thin:@" + dataSource.getDataSourceIp() + ":" + dataSource.getDataSourcePort() + ":" + dataSource.getDataSourceDatabaseName();
break;
case 4:
driveClass = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
jdbcUrl = "jdbc:sqlserver://"+ dataSource.getDataSourceIp() +":"+dataSource.getDataSourcePort()+";databaseName="+dataSource.getDataSourceDatabaseName();
break;
default:
return false;
}
if (dataSource.getTypeId() != 2){
flag = testDatasource(driveClass, jdbcUrl, dataSource.getDataSourceUsername(), dataSource.getDataSourcePassword()); flag = testDatasource(driveClass, jdbcUrl, dataSource.getDataSourceUsername(), dataSource.getDataSourcePassword());
}else {
// redis
//连接指定的redis
Jedis jedis = new Jedis(dataSource.getDataSourceIp(), Integer.valueOf(dataSource.getDataSourcePort()));
//如果有密码则需要下面这一行
if (dataSource.getDataSourcePassword() != null && !dataSource.getDataSourcePassword().equals("")){
jedis.auth(dataSource.getDataSourcePassword());
}
//查看服务是否运行,运行正常的话返回一个PONG否则返回一个连接错误
try {
jedis.ping();
flag = true;
} catch (Exception e) {
flag = false;
}
} }
return flag; return flag;
} catch (Exception e) { } catch (Exception e) {

View File

@ -52,6 +52,7 @@ public class DataTypeServiceImpl extends ServiceImpl<DataTypeMapper, DataType> i
@Override @Override
public int insertDataType(DataType dataType) { public int insertDataType(DataType dataType) {
dataType.setCreateTime(DateUtils.getNowDate()); dataType.setCreateTime(DateUtils.getNowDate());
dataType.setDataType(lowerCase(dataType.getDataType()));
return dataTypeMapper.insertDataType(dataType); return dataTypeMapper.insertDataType(dataType);
} }
@ -64,6 +65,7 @@ public class DataTypeServiceImpl extends ServiceImpl<DataTypeMapper, DataType> i
@Override @Override
public int updateDataType(DataType dataType) { public int updateDataType(DataType dataType) {
dataType.setUpdateTime(DateUtils.getNowDate()); dataType.setUpdateTime(DateUtils.getNowDate());
dataType.setDataType(lowerCase(dataType.getDataType()));
return dataTypeMapper.updateDataType(dataType); return dataTypeMapper.updateDataType(dataType);
} }
@ -88,4 +90,13 @@ public class DataTypeServiceImpl extends ServiceImpl<DataTypeMapper, DataType> i
public int deleteDataTypeById(Long id) { public int deleteDataTypeById(Long id) {
return dataTypeMapper.deleteDataTypeById(id); return dataTypeMapper.deleteDataTypeById(id);
} }
/**
*
* @param str
* @return
*/
public static String lowerCase(String str){
return str.toLowerCase().replaceAll(" ","");
}
} }

View File

@ -1,12 +1,14 @@
<?xml version="1.0" encoding="UTF-8" ?> <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper <!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.etl.data.type.mapper.DataTypeMapper"> <mapper namespace="com.etl.data.type.mapper.DataTypeMapper">
<resultMap type="com.etl.data.type.domain.DataType" id="DataTypeResult"> <resultMap type="com.etl.data.type.domain.DataType" id="DataTypeResult">
<result property="id" column="id" /> <result property="id" column="id" />
<result property="dataType" column="data_type" /> <result property="dataType" column="data_type" />
<result property="driverManager" column="driver_manager" />
<result property="jdbcPre" column="jdbc_pre" />
<result property="remark" column="remark" /> <result property="remark" column="remark" />
<result property="createBy" column="create_by" /> <result property="createBy" column="create_by" />
<result property="createTime" column="create_time" /> <result property="createTime" column="create_time" />
@ -15,13 +17,13 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
</resultMap> </resultMap>
<sql id="selectDataTypeVo"> <sql id="selectDataTypeVo">
select id, data_type, remark, create_by, create_time, update_by, update_time from data_type select id, data_type, driver_manager, jdbc_pre, remark, create_by, create_time, update_by, update_time from data_type
</sql> </sql>
<select id="selectDataTypeList" parameterType="com.etl.data.type.domain.DataType" resultMap="DataTypeResult"> <select id="selectDataTypeList" parameterType="com.etl.data.type.domain.DataType" resultMap="DataTypeResult">
<include refid="selectDataTypeVo"/> <include refid="selectDataTypeVo"/>
<where> <where>
<if test="dataType != null and dataType != ''"> and data_type = #{dataType}</if> <if test="dataType != null and dataType != ''"> and data_type like concat('%', #{dataType}, '%')</if>
</where> </where>
</select> </select>
@ -34,26 +36,32 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
insert into data_type insert into data_type
<trim prefix="(" suffix=")" suffixOverrides=","> <trim prefix="(" suffix=")" suffixOverrides=",">
<if test="dataType != null">data_type,</if> <if test="dataType != null">data_type,</if>
<if test="driverManager != null">driver_manager,</if>
<if test="jdbcPre != null">jdbc_pre,</if>
<if test="remark != null">remark,</if> <if test="remark != null">remark,</if>
<if test="createBy != null">create_by,</if> <if test="createBy != null">create_by,</if>
<if test="createTime != null">create_time,</if> <if test="createTime != null">create_time,</if>
<if test="updateBy != null">update_by,</if> <if test="updateBy != null">update_by,</if>
<if test="updateTime != null">update_time,</if> <if test="updateTime != null">update_time,</if>
</trim> </trim>
<trim prefix="values (" suffix=")" suffixOverrides=","> <trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="dataType != null">#{dataType},</if> <if test="dataType != null">#{dataType},</if>
<if test="driverManager != null">#{driverManager},</if>
<if test="jdbcPre != null">#{jdbcPre},</if>
<if test="remark != null">#{remark},</if> <if test="remark != null">#{remark},</if>
<if test="createBy != null">#{createBy},</if> <if test="createBy != null">#{createBy},</if>
<if test="createTime != null">#{createTime},</if> <if test="createTime != null">#{createTime},</if>
<if test="updateBy != null">#{updateBy},</if> <if test="updateBy != null">#{updateBy},</if>
<if test="updateTime != null">#{updateTime},</if> <if test="updateTime != null">#{updateTime},</if>
</trim> </trim>
</insert> </insert>
<update id="updateDataType" parameterType="com.etl.data.type.domain.DataType"> <update id="updateDataType" parameterType="com.etl.data.type.domain.DataType">
update data_type update data_type
<trim prefix="SET" suffixOverrides=","> <trim prefix="SET" suffixOverrides=",">
<if test="dataType != null">data_type = #{dataType},</if> <if test="dataType != null">data_type = #{dataType},</if>
<if test="driverManager != null">driver_manager = #{driverManager},</if>
<if test="jdbcPre != null">jdbc_pre = #{jdbcPre},</if>
<if test="remark != null">remark = #{remark},</if> <if test="remark != null">remark = #{remark},</if>
<if test="createBy != null">create_by = #{createBy},</if> <if test="createBy != null">create_by = #{createBy},</if>
<if test="createTime != null">create_time = #{createTime},</if> <if test="createTime != null">create_time = #{createTime},</if>