提交 | 用户 | 时间
|
1ac2bc
|
1 |
package cn.stylefeng.guns.base.db.dao; |
懒 |
2 |
|
|
3 |
import cn.hutool.core.date.DateUtil; |
|
4 |
import cn.stylefeng.guns.base.db.dao.sqls.AddDatabaseInfoSql; |
|
5 |
import cn.stylefeng.guns.base.db.dao.sqls.DatabaseListSql; |
|
6 |
import cn.stylefeng.guns.base.db.dao.sqls.DeleteDatabaseInfoSql; |
|
7 |
import cn.stylefeng.guns.base.db.exception.DataSourceInitException; |
|
8 |
import cn.stylefeng.roses.core.config.properties.DruidProperties; |
|
9 |
import com.baomidou.mybatisplus.core.toolkit.IdWorker; |
|
10 |
import lombok.extern.slf4j.Slf4j; |
|
11 |
|
|
12 |
import java.sql.*; |
|
13 |
import java.util.Date; |
|
14 |
import java.util.HashMap; |
|
15 |
import java.util.Map; |
|
16 |
|
|
17 |
import static cn.stylefeng.guns.base.db.context.DataSourceContext.MASTER_DATASOURCE_NAME; |
|
18 |
|
|
19 |
/** |
|
20 |
* 操作数据源信息的dao |
|
21 |
* |
|
22 |
* @author fengshuonan |
|
23 |
* @date 2019-06-12-14:02 |
|
24 |
*/ |
|
25 |
@Slf4j |
|
26 |
public class DataBaseInfoDao { |
|
27 |
|
|
28 |
private DruidProperties druidProperties; |
|
29 |
|
|
30 |
public DataBaseInfoDao(DruidProperties druidProperties) { |
|
31 |
this.druidProperties = druidProperties; |
|
32 |
} |
|
33 |
|
|
34 |
/** |
|
35 |
* 查询所有数据源列表 |
|
36 |
* |
|
37 |
* @author fengshuonan |
|
38 |
* @Date 2019-05-04 20:30 |
|
39 |
*/ |
|
40 |
public Map<String, DruidProperties> getAllDataBaseInfo() { |
|
41 |
|
|
42 |
Map<String, DruidProperties> dataSourceList = new HashMap<>(); |
|
43 |
|
|
44 |
try { |
|
45 |
Class.forName(druidProperties.getDriverClassName()); |
|
46 |
Connection conn = DriverManager.getConnection( |
|
47 |
druidProperties.getUrl(), druidProperties.getUsername(), druidProperties.getPassword()); |
|
48 |
|
|
49 |
PreparedStatement preparedStatement = conn.prepareStatement(new DatabaseListSql().getSql(druidProperties.getUrl())); |
|
50 |
ResultSet resultSet = preparedStatement.executeQuery(); |
|
51 |
|
|
52 |
while (resultSet.next()) { |
|
53 |
DruidProperties druidProperties = createDruidProperties(resultSet); |
|
54 |
String dbName = resultSet.getString("db_name"); |
|
55 |
dataSourceList.put(dbName, druidProperties); |
|
56 |
} |
|
57 |
|
|
58 |
return dataSourceList; |
|
59 |
|
|
60 |
} catch (Exception ex) { |
|
61 |
throw new DataSourceInitException(DataSourceInitException.ExEnum.QUERY_DATASOURCE_INFO_ERROR); |
|
62 |
} |
|
63 |
} |
|
64 |
|
|
65 |
/** |
|
66 |
* 初始化master的数据源,要和properties配置的数据源一致 |
|
67 |
* |
|
68 |
* @author fengshuonan |
|
69 |
* @Date 2019-06-15 10:20 |
|
70 |
*/ |
|
71 |
public void createMasterDatabaseInfo() { |
|
72 |
try { |
|
73 |
Class.forName(druidProperties.getDriverClassName()); |
|
74 |
Connection conn = DriverManager.getConnection( |
|
75 |
druidProperties.getUrl(), druidProperties.getUsername(), druidProperties.getPassword()); |
|
76 |
|
|
77 |
PreparedStatement preparedStatement = conn.prepareStatement(new AddDatabaseInfoSql().getSql(druidProperties.getUrl())); |
|
78 |
|
|
79 |
preparedStatement.setLong(1, IdWorker.getId()); |
|
80 |
preparedStatement.setString(2, MASTER_DATASOURCE_NAME); |
|
81 |
preparedStatement.setString(3, druidProperties.getDriverClassName()); |
|
82 |
preparedStatement.setString(4, druidProperties.getUsername()); |
|
83 |
preparedStatement.setString(5, druidProperties.getPassword()); |
|
84 |
preparedStatement.setString(6, druidProperties.getUrl()); |
|
85 |
preparedStatement.setString(7, "主数据源,项目启动数据源!"); |
|
86 |
preparedStatement.setString(8, DateUtil.formatDateTime(new Date())); |
|
87 |
|
|
88 |
int i = preparedStatement.executeUpdate(); |
|
89 |
log.info("初始化master的databaseInfo信息!初始化" + i + "条!"); |
|
90 |
} catch (Exception ex) { |
|
91 |
log.error("初始化master的databaseInfo信息错误!", ex); |
|
92 |
throw new DataSourceInitException(DataSourceInitException.ExEnum.QUERY_DATASOURCE_INFO_ERROR); |
|
93 |
} |
|
94 |
} |
|
95 |
|
|
96 |
/** |
|
97 |
* 删除master的数据源信息 |
|
98 |
* |
|
99 |
* @author fengshuonan |
|
100 |
* @Date 2019-06-15 10:20 |
|
101 |
*/ |
|
102 |
public void deleteMasterDatabaseInfo() { |
|
103 |
try { |
|
104 |
Class.forName(druidProperties.getDriverClassName()); |
|
105 |
Connection conn = DriverManager.getConnection( |
|
106 |
druidProperties.getUrl(), druidProperties.getUsername(), druidProperties.getPassword()); |
|
107 |
|
|
108 |
PreparedStatement preparedStatement = conn.prepareStatement(new DeleteDatabaseInfoSql().getSql(druidProperties.getUrl())); |
|
109 |
preparedStatement.setString(1, MASTER_DATASOURCE_NAME); |
|
110 |
int i = preparedStatement.executeUpdate(); |
|
111 |
log.info("删除master的databaseInfo信息!删除" + i + "条!"); |
|
112 |
} catch (Exception ex) { |
|
113 |
log.info("删除master的databaseInfo信息失败!", ex); |
|
114 |
throw new DataSourceInitException(DataSourceInitException.ExEnum.QUERY_DATASOURCE_INFO_ERROR); |
|
115 |
} |
|
116 |
} |
|
117 |
|
|
118 |
/** |
|
119 |
* 通过查询结果组装druidProperties |
|
120 |
* |
|
121 |
* @author fengshuonan |
|
122 |
* @Date 2019-06-12 14:22 |
|
123 |
*/ |
|
124 |
private DruidProperties createDruidProperties(ResultSet resultSet) { |
|
125 |
|
|
126 |
DruidProperties druidProperties = new DruidProperties(); |
|
127 |
|
|
128 |
druidProperties.setTestOnBorrow(true); |
|
129 |
druidProperties.setTestOnReturn(true); |
|
130 |
|
|
131 |
try { |
|
132 |
druidProperties.setDriverClassName(resultSet.getString("jdbc_driver")); |
|
133 |
druidProperties.setUrl(resultSet.getString("jdbc_url")); |
|
134 |
druidProperties.setUsername(resultSet.getString("user_name")); |
|
135 |
druidProperties.setPassword(resultSet.getString("password")); |
|
136 |
} catch (SQLException e) { |
|
137 |
throw new DataSourceInitException(DataSourceInitException.ExEnum.QUERY_DATASOURCE_INFO_ERROR); |
|
138 |
} |
|
139 |
|
|
140 |
return druidProperties; |
|
141 |
} |
|
142 |
|
|
143 |
} |