mirror of
https://gitee.com/dromara/sms4j.git
synced 2025-12-06 17:08:40 +08:00
尝试添加 DataSource获取数据库链接方式
This commit is contained in:
parent
40b4891875
commit
12bac18c14
@ -35,6 +35,7 @@ public class SmsSqlConfig {
|
||||
/**
|
||||
* 数据库名
|
||||
*/
|
||||
@Deprecated
|
||||
private String databaseName;
|
||||
|
||||
/**
|
||||
|
||||
@ -5,6 +5,7 @@ import org.dromara.sms4j.comm.config.SmsSqlConfig;
|
||||
import org.dromara.sms4j.comm.exception.SmsSqlException;
|
||||
import org.dromara.sms4j.comm.factory.BeanFactory;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
@ -12,6 +13,7 @@ import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* JDBCTool
|
||||
@ -25,15 +27,28 @@ public class JDBCTool {
|
||||
public static final String SELECT = "select {},{} from {} where {} = {}";
|
||||
private static final String ERR_CONFIG = "One configuration was expected, but {} was found. Please check your database configuration";
|
||||
private final SmsSqlConfig config;
|
||||
/**
|
||||
* 数据库链接
|
||||
* */
|
||||
private Connection connection;
|
||||
|
||||
|
||||
public JDBCTool(SmsSqlConfig config,Connection connection) {
|
||||
// if (config == null) {
|
||||
// throw new SmsSqlException("The configuration file failed to be loaded");
|
||||
// }
|
||||
this.config = config;
|
||||
this.connection = connection;
|
||||
}
|
||||
|
||||
public JDBCTool(SmsSqlConfig config) {
|
||||
if (config == null) {
|
||||
throw new SmsSqlException("The configuration file failed to be loaded");
|
||||
}
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public void setConnection(Connection connection){
|
||||
this.connection = connection;
|
||||
}
|
||||
|
||||
public static Map<String, String> selectConfig() {
|
||||
Map<String, String> select = BeanFactory.getJDBCTool().select();
|
||||
if (select.size() == 0) {
|
||||
@ -48,6 +63,9 @@ public class JDBCTool {
|
||||
public Connection getConn() {
|
||||
Connection connection;
|
||||
String url;
|
||||
if (SmsUtil.isEmpty(config.getUrl())) {
|
||||
throw new SmsSqlException("The configuration file failed to be loaded");
|
||||
}
|
||||
try {
|
||||
if (config.getDatabaseName().isEmpty()){
|
||||
url = config.getUrl();
|
||||
@ -69,13 +87,16 @@ public class JDBCTool {
|
||||
* @author :Wind
|
||||
*/
|
||||
public Map<String, String> select(String sql) {
|
||||
Connection conn = null;
|
||||
// Connection conn = null;
|
||||
if (Objects.isNull(connection)){
|
||||
connection = getConn();
|
||||
}
|
||||
PreparedStatement preparedStatement = null;
|
||||
ResultSet resultSet = null;
|
||||
Map<String, String> map = new Hashtable<>();
|
||||
try {
|
||||
conn = getConn();
|
||||
preparedStatement = conn.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
|
||||
// conn = getConn();
|
||||
preparedStatement = connection.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
|
||||
preparedStatement.setFetchSize(1000);
|
||||
resultSet = preparedStatement.executeQuery();
|
||||
|
||||
@ -87,7 +108,7 @@ public class JDBCTool {
|
||||
} catch (SQLException e) {
|
||||
throw new SmsSqlException(e.getMessage());
|
||||
} finally {
|
||||
close(conn, preparedStatement, resultSet);
|
||||
close(connection, preparedStatement, resultSet);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
@ -8,6 +8,7 @@ import org.dromara.sms4j.comm.config.SmsSqlConfig;
|
||||
import org.dromara.sms4j.comm.constant.Constant;
|
||||
import org.dromara.sms4j.comm.delayedTime.DelayedTime;
|
||||
import org.dromara.sms4j.comm.factory.BeanFactory;
|
||||
import org.dromara.sms4j.comm.utils.JDBCTool;
|
||||
import org.dromara.sms4j.core.SupplierSqlConfig;
|
||||
import org.dromara.sms4j.starter.aop.RestrictedProcessImpl;
|
||||
import org.dromara.sms4j.api.universal.RedisUtil;
|
||||
@ -20,6 +21,9 @@ import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
|
||||
@ -70,7 +74,11 @@ public class SmsAutowiredConfig {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnProperty(prefix = "sms", name = "config-type", havingValue = "sql_config")
|
||||
protected SupplierSqlConfig supplierSqlConfig(SmsSqlConfig smsSqlConfig){
|
||||
protected SupplierSqlConfig supplierSqlConfig(SmsSqlConfig smsSqlConfig) throws SQLException {
|
||||
DataSource bean = SpringUtil.getBean(DataSource.class);
|
||||
if (!Objects.isNull(bean)){
|
||||
BeanFactory.getJDBCTool().setConnection(bean.getConnection());
|
||||
}
|
||||
return new SupplierSqlConfig();
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user