创建数据源的连接池
parent
b2c3c2a063
commit
64dec29ad1
|
@ -22,6 +22,12 @@
|
|||
<groupId>com.muyu</groupId>
|
||||
<artifactId>muyu-source-common</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>druid</artifactId>
|
||||
<version>1.2.23</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
package com.muyu.JdbcUtils;
|
||||
|
||||
/**
|
||||
* @author Lenovo
|
||||
* @ Tool:IntelliJ IDEA
|
||||
* @ Author:CHX
|
||||
* @ Date:2024-08-22-15:02
|
||||
* @ Version:1.0
|
||||
* @ Description:JDBC抽象类
|
||||
*/
|
||||
public class JDBCAbstractClass {
|
||||
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.muyu.JdbcUtils;
|
||||
|
||||
/**
|
||||
* @author Lenovo
|
||||
* @ Tool:IntelliJ IDEA
|
||||
* @ Author:CHX
|
||||
* @ Date:2024-08-22-15:04
|
||||
* @ Version:1.0
|
||||
* @ Description:JDBC实体类
|
||||
*/
|
||||
public class JDBCConcreteClass {
|
||||
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.muyu.config;
|
||||
|
||||
/**
|
||||
* @author Lenovo
|
||||
* @ Tool:IntelliJ IDEA
|
||||
* @ Author:CHX
|
||||
* @ Date:2024-08-22-14:33
|
||||
* @ Version:1.0
|
||||
* @ Description:数据接入客户端配置类
|
||||
*/
|
||||
public class SourceClientConfig {
|
||||
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package com.muyu.config;
|
||||
|
||||
/**
|
||||
* @author Lenovo
|
||||
* @ Tool:IntelliJ IDEA
|
||||
* @ Author:CHX
|
||||
* @ Date:2024-08-22-14:34
|
||||
* @ Version:1.0
|
||||
* @ Description:初始化加载
|
||||
*/
|
||||
public class SourceClientRunner {
|
||||
}
|
|
@ -0,0 +1,73 @@
|
|||
package com.muyu.connection;
|
||||
|
||||
import com.alibaba.druid.pool.DruidPooledConnection;
|
||||
import com.muyu.source.domain.DataSource;
|
||||
import com.muyu.source.domain.DataType;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
* @author Lenovo
|
||||
* @ Tool:IntelliJ IDEA
|
||||
* @ Author:CHX
|
||||
* @ Date:2024-08-22-14:36
|
||||
* @ Version:1.0
|
||||
* @ Description:数据源连接配置类
|
||||
*/
|
||||
@Log4j2
|
||||
@Component
|
||||
public class DataSourceConfig {
|
||||
//数据源连接池
|
||||
private static HashMap<Long, DruidDataSource> dataSourceMap = new HashMap<>();
|
||||
|
||||
//数据源连接池
|
||||
public static void init(DataSource source, DataType dataType) {
|
||||
//创建连接池
|
||||
DruidDataSource druidDataSource = new DruidDataSource();
|
||||
//用户名
|
||||
druidDataSource.setUsername(source.getUserName());
|
||||
//密码
|
||||
druidDataSource.setPassword(source.getPassword());
|
||||
//数据库连接
|
||||
druidDataSource.setUrl(dataType.getPrefix() + source.getIp() + ":" + source.getPort() + "/" + source.getDatabaseName() + "?" + source.getConnectionParam());
|
||||
//驱动
|
||||
druidDataSource.setDriverClassName(dataType.getDriverManager());
|
||||
//最小连接数
|
||||
druidDataSource.setMinIdle(Math.toIntExact(source.getMaxWaitSize()));
|
||||
//最大连接数
|
||||
druidDataSource.setMaxActive(Math.toIntExact(source.getMaxNum()));
|
||||
//初始连接数
|
||||
druidDataSource.setInitialSize(Math.toIntExact(source.getInitNum()));
|
||||
try {
|
||||
druidDataSource.init();
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
dataSourceMap.put(source.getId(), druidDataSource);
|
||||
}
|
||||
|
||||
//获取连接
|
||||
public static Connection getDataSource(Long id) {
|
||||
DruidDataSource druidDataSource = dataSourceMap.get(id);
|
||||
try {
|
||||
DruidPooledConnection connection = druidDataSource.getConnection();
|
||||
return connection;
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
//归还连接
|
||||
public static void returnConnection(Connection connection) {
|
||||
try {
|
||||
connection.close();
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -29,5 +29,27 @@ public class AssetAccredit {
|
|||
@TableId(value = "id", type = IdType.AUTO)
|
||||
@Excel(name = "主键")
|
||||
private Long id;
|
||||
/**
|
||||
* 授权编号
|
||||
*/
|
||||
@Excel(name = "授权编号")
|
||||
private String accreditNo;
|
||||
/**
|
||||
* 数据源id
|
||||
*/
|
||||
private Long dataId;
|
||||
/**
|
||||
* 授权类型 用户/部门
|
||||
*/
|
||||
private String accreditType;
|
||||
|
||||
/**
|
||||
* 授权数据类型 1:数据源 2:表
|
||||
*/
|
||||
private String accreditDataType;
|
||||
/**
|
||||
* 授权状态 1:授权 2:取消授权
|
||||
*/
|
||||
private String status;
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
package com.muyu.source.domain;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.muyu.common.core.annotation.Excel;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
/**
|
||||
* @author Lenovo
|
||||
* @ Tool:IntelliJ IDEA
|
||||
* @ Author:CHX
|
||||
* @ Date:2024-08-22-15:44
|
||||
* @ Version:1.0
|
||||
* @ Description:数据库类型
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@SuperBuilder
|
||||
@TableName("data_type")
|
||||
public class DataType {
|
||||
/**
|
||||
* 主键
|
||||
*/
|
||||
@TableId(value = "id", type = IdType.AUTO)
|
||||
@Excel(name = "主键")
|
||||
private Long id;
|
||||
/**
|
||||
* 数据库类型
|
||||
*/
|
||||
@Excel(name = "数据库类型")
|
||||
private String type;
|
||||
/**
|
||||
* 前缀
|
||||
*/
|
||||
@Excel(name = "前缀")
|
||||
private String prefix;
|
||||
/**
|
||||
* 驱动管理器
|
||||
*/
|
||||
@Excel(name = "驱动管理器")
|
||||
private String driverManager;
|
||||
}
|
Loading…
Reference in New Issue