创建数据源的连接池

master
冷调 2024-08-22 16:09:27 +08:00
parent b2c3c2a063
commit 64dec29ad1
8 changed files with 199 additions and 0 deletions

View File

@ -22,6 +22,12 @@
<groupId>com.muyu</groupId> <groupId>com.muyu</groupId>
<artifactId>muyu-source-common</artifactId> <artifactId>muyu-source-common</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.23</version>
<scope>compile</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -0,0 +1,13 @@
package com.muyu.JdbcUtils;
/**
* @author Lenovo
* @ ToolIntelliJ IDEA
* @ AuthorCHX
* @ Date2024-08-22-15:02
* @ Version1.0
* @ DescriptionJDBC
*/
public class JDBCAbstractClass {
}

View File

@ -0,0 +1,13 @@
package com.muyu.JdbcUtils;
/**
* @author Lenovo
* @ ToolIntelliJ IDEA
* @ AuthorCHX
* @ Date2024-08-22-15:04
* @ Version1.0
* @ DescriptionJDBC
*/
public class JDBCConcreteClass {
}

View File

@ -0,0 +1,13 @@
package com.muyu.config;
/**
* @author Lenovo
* @ ToolIntelliJ IDEA
* @ AuthorCHX
* @ Date2024-08-22-14:33
* @ Version1.0
* @ Description
*/
public class SourceClientConfig {
}

View File

@ -0,0 +1,12 @@
package com.muyu.config;
/**
* @author Lenovo
* @ ToolIntelliJ IDEA
* @ AuthorCHX
* @ Date2024-08-22-14:34
* @ Version1.0
* @ Description
*/
public class SourceClientRunner {
}

View File

@ -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
* @ ToolIntelliJ IDEA
* @ AuthorCHX
* @ Date2024-08-22-14:36
* @ Version1.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);
}
}
}

View File

@ -29,5 +29,27 @@ public class AssetAccredit {
@TableId(value = "id", type = IdType.AUTO) @TableId(value = "id", type = IdType.AUTO)
@Excel(name = "主键") @Excel(name = "主键")
private Long id; 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;
} }

View File

@ -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
* @ ToolIntelliJ IDEA
* @ AuthorCHX
* @ Date2024-08-22-15:44
* @ Version1.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;
}