package org.spiderflow.core.utils;@b@@b@import java.util.HashMap;@b@import java.util.Map;@b@@b@import javax.sql.DataSource;@b@@b@import org.spiderflow.core.service.DataSourceService;@b@import org.springframework.beans.factory.annotation.Autowired;@b@import org.springframework.stereotype.Component;@b@@b@import com.alibaba.druid.pool.DruidDataSource;@b@@b@/**@b@ * 数据库连接工具类@b@ * @author jmxd@b@ *@b@ */@b@@Component@b@public class DataSourceUtils {@b@ @b@ private static final Map<String,DataSource> datasources = new HashMap<>();@b@ @b@ private static DataSourceService dataSourceService;@b@ @b@ public static DataSource createDataSource(String className,String url,String username,String password){@b@ DruidDataSource datasource = new DruidDataSource();@b@ datasource.setDriverClassName(className);@b@ datasource.setUrl(url);@b@ datasource.setUsername(username);@b@ datasource.setPassword(password);@b@ datasource.setDefaultAutoCommit(true);@b@ datasource.setMinIdle(1);@b@ datasource.setInitialSize(2);@b@ return datasource;@b@ }@b@ @b@ public static void remove(String dataSourceId){@b@ DataSource dataSource = datasources.get(dataSourceId);@b@ if(dataSource != null){@b@ DruidDataSource ds = (DruidDataSource) dataSource;@b@ ds.close();@b@ datasources.remove(dataSourceId);@b@ }@b@ }@b@ @b@ public synchronized static DataSource getDataSource(String dataSourceId){@b@ DataSource dataSource = datasources.get(dataSourceId);@b@ if(dataSource == null){@b@ org.spiderflow.core.model.DataSource ds = dataSourceService.getById(dataSourceId);@b@ if(ds != null){@b@ dataSource = createDataSource(ds.getDriverClassName(), ds.getJdbcUrl(), ds.getUsername(), ds.getPassword());@b@ datasources.put(dataSourceId, dataSource);@b@ }@b@ }@b@ return dataSource;@b@ }@b@@b@ @Autowired@b@ public void setDataSourceService(DataSourceService dataSourceService) {@b@ DataSourceUtils.dataSourceService = dataSourceService;@b@ }@b@@b@}