|
@@ -1,45 +1,100 @@
|
|
|
package com.ruoyi.web.dataBase;
|
|
|
|
|
|
+import com.ruoyi.common.core.domain.entity.SysUser;
|
|
|
+import com.ruoyi.common.utils.SecurityUtils;
|
|
|
+import com.ruoyi.system.domain.dto.UserEnterpriseRelation;
|
|
|
+import org.springframework.jdbc.core.BeanPropertyRowMapper;
|
|
|
import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
+import javax.annotation.PostConstruct;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
|
@Component
|
|
|
public class DatabaseConnectionMap {
|
|
|
|
|
|
- private ConcurrentHashMap<String, JdbcTemplate> databaseConnectionMap = new ConcurrentHashMap<>();
|
|
|
+
|
|
|
+ private static ConcurrentHashMap<Long, List<JdbcTemplate>> databaseConnectionMap = new ConcurrentHashMap<>();
|
|
|
+
|
|
|
+ private JdbcTemplate iotDataBast;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ @PostConstruct
|
|
|
+ public void postConstructDataBase() {
|
|
|
+ System.out.println("连接数据库");
|
|
|
+ connectIotDatabases();
|
|
|
+ System.out.println("存储连接");
|
|
|
+ connectToAllDatabase(null);
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
- * 获取数据库连接
|
|
|
- * @param dataBaseName 数据库名
|
|
|
+ * 连接iot数据库
|
|
|
*/
|
|
|
- public JdbcTemplate getJdbcTemplate(String dataBaseName) {
|
|
|
- JdbcTemplate jdbcTemplate = databaseConnectionMap.get(dataBaseName);
|
|
|
+ public void connectIotDatabases() {
|
|
|
+ DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
|
|
+ dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
|
|
|
+ dataSource.setUrl("jdbc:mysql://47.94.195.4:3525/iot?useUnicode=true&useSSL=false&characterEncoding=utf8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai");
|
|
|
+ dataSource.setUsername("wdy_iot");
|
|
|
+ dataSource.setPassword("DF5!Oe^NJj7$f2vY");
|
|
|
+ iotDataBast = new JdbcTemplate(dataSource);
|
|
|
+ }
|
|
|
|
|
|
- // 连接不存在则连接数据库
|
|
|
- if (jdbcTemplate == null) {
|
|
|
- DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
|
|
- dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
|
|
|
- dataSource.setUrl("jdbc:mysql://47.94.195.4:3525/" + dataBaseName + "?useUnicode=true&useSSL=false&characterEncoding=utf8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai");
|
|
|
- dataSource.setUsername("db_cdyq6tkqn1ioja179i8b");
|
|
|
- dataSource.setPassword("By@#5c9i75sgtx3b620g6m6e");
|
|
|
- JdbcTemplate template = new JdbcTemplate(dataSource);
|
|
|
- jdbcTemplate = template;
|
|
|
+ /**
|
|
|
+ * 数据库
|
|
|
+ */
|
|
|
+ public void connectToAllDatabase(Long standingId) {
|
|
|
+ String sql = "select ue.enterprise_database_driver, ue.enterprise_database_url, ue.enterprise_database_username, ue.enterprise_database_password , uer.standing_id from t_sys_enterprise ue inner join user_enterprise_relation uer on ue.enterprise_openid = uer.enterprise_open_id where ue.factory_status = 1";
|
|
|
+ if (standingId != null) {
|
|
|
+ sql += " and standing_id = " + standingId;
|
|
|
+ }
|
|
|
+ List<UserEnterpriseRelation> query = iotDataBast.query(sql, new BeanPropertyRowMapper<>(UserEnterpriseRelation.class));
|
|
|
+ for (UserEnterpriseRelation userEnterpriseRelation : query) {
|
|
|
+ List<JdbcTemplate> jdbcTemplates = new ArrayList<>();
|
|
|
+ for (UserEnterpriseRelation enterpriseRelation : query) {
|
|
|
+ if (userEnterpriseRelation.getStandingId() == enterpriseRelation.getStandingId()) {
|
|
|
+ jdbcTemplates.add(connectDatabases(enterpriseRelation));
|
|
|
+ };
|
|
|
+ }
|
|
|
// map中存值
|
|
|
- databaseConnectionMap.put(dataBaseName, template);
|
|
|
+ databaseConnectionMap.put(userEnterpriseRelation.getStandingId(), jdbcTemplates);
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- // 测试连接
|
|
|
- try {
|
|
|
- int count = jdbcTemplate.queryForObject("SELECT 1 FROM DUAL" , Integer.class);
|
|
|
- } catch (Exception e) {
|
|
|
- // 移除连接失败的数据源
|
|
|
- databaseConnectionMap.remove(dataBaseName);
|
|
|
- throw new RuntimeException(e.getMessage());
|
|
|
+ /**
|
|
|
+ * 连接数据库
|
|
|
+ *
|
|
|
+ * @param userEnterpriseRelation 连接信息
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private JdbcTemplate connectDatabases(UserEnterpriseRelation userEnterpriseRelation) {
|
|
|
+ DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
|
|
+ dataSource.setDriverClassName(userEnterpriseRelation.getEnterpriseDatabaseDriver());
|
|
|
+ dataSource.setUrl(userEnterpriseRelation.getEnterpriseDatabaseUrl());
|
|
|
+ dataSource.setUsername(userEnterpriseRelation.getEnterpriseDatabaseUsername());
|
|
|
+ dataSource.setPassword(userEnterpriseRelation.getEnterpriseDatabasePassword());
|
|
|
+ JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
|
|
|
+ int count = jdbcTemplate.queryForObject("SELECT 1 FROM DUAL", Integer.class);
|
|
|
+ return jdbcTemplate;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取数据库连接
|
|
|
+ */
|
|
|
+ public List<JdbcTemplate> getJdbcTemplate() {
|
|
|
+ SysUser user = SecurityUtils.getLoginUser().getUser();
|
|
|
+ List<JdbcTemplate> jdbcTemplateList = databaseConnectionMap.get(user.getDeptId());
|
|
|
+
|
|
|
+ // 连接不存在则连接数据库
|
|
|
+ if (jdbcTemplateList == null) {
|
|
|
+ connectToAllDatabase(user.getDeptId());
|
|
|
+ jdbcTemplateList = databaseConnectionMap.get(user.getDeptId());
|
|
|
}
|
|
|
|
|
|
- return jdbcTemplate;
|
|
|
+ return jdbcTemplateList;
|
|
|
}
|
|
|
}
|