-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDbUtil.java
366 lines (348 loc) · 10.1 KB
/
DbUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package cn.xanderye.util;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.sql.*;
import java.util.*;
/**
* 数据库工具类
*/
@Slf4j
public class DbUtil {
/**
* 连接地址
*/
private static String url;
/**
* 用户名
*/
private static String username;
/**
* 密码
*/
private static String password;
/**
* 驱动名称
*/
private static String driver;
/**
* 下划线
*/
private static final char UNDERLINE = '_';
static {
// 类加载时先初始化连接
try {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream is = classLoader.getResourceAsStream("db.properties");
if (is != null) {
Properties p = new Properties();
p.load(is);
url = p.getProperty("db.url");
username = p.getProperty("db.username");
password = p.getProperty("db.password");
driver = p.getProperty("db.driver");
} else {
log.warn("Could not found db.properties, please configure manually.");
}
} catch (IOException e) {
log.error("Error initializing the config.");
}
}
/**
* 手动配置数据库
* @param customUrl
* @param customUsername
* @param customPassword
* @param customDriver
* @return void
* @author XanderYe
* @date 2021/12/16
*/
public static void setConfig(String customUrl, String customUsername, String customPassword, String customDriver) {
url = customUrl;
username = customUsername;
password = customPassword;
driver = customDriver;
}
/**
* 初始化连接池
* @param
* @return java.sql.Connection
* @author XanderYe
* @date 2021/12/16
*/
public static Connection getConn() throws SQLException {
if (StringUtils.isAnyEmpty(url, username, password, driver)) {
throw new RuntimeException("Please check the database config.");
}
Connection conn = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
if (conn == null) {
throw new RuntimeException("Error connecting to the database.");
}
return conn;
}
/**
* 更新操作,包括(insert,update,delete)
*
* @param sql
* @param args
* @return boolean
* @author XanderYe
* @date 2020/9/1
*/
public static int update(String sql, Object... args) throws SQLException {
int affect;
Connection conn = null;
PreparedStatement ps = null;
try {
conn = getConn();
ps = conn.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
ps.setObject(i + 1, args[i]);
}
affect = ps.executeUpdate();
} finally {
DbUtil.close(null, ps, conn);
}
return affect;
}
/**
* 根据条件查询map
* @param sql
* @param args
* @return java.util.Map<java.lang.String,java.lang.Object>
* @author XanderYe
* @date 2020/9/1
*/
public static Map<String, Object> queryOne(String sql, Object... args) throws SQLException {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = getConn();
ps = conn.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
ps.setObject(i + 1, args[i]);
}
rs = ps.executeQuery();
if (rs.next()) {
return getMapFromResultSet(rs);
}
} catch (IllegalArgumentException | SecurityException e) {
e.printStackTrace();
} finally {
close(rs, ps, conn);
}
return null;
}
/**
* 根据条件查询对象
* @param clazz
* @param sql
* @param args
* @return T
* @author XanderYe
* @date 2020/9/1
*/
public static <T> T queryOne(Class<T> clazz, String sql, Object... args) throws SQLException {
Map<String, Object> map = queryOne(sql, args);
if (map != null) {
try {
return mapToObject(clazz, map);
} catch (IllegalAccessException | InstantiationException | NoSuchFieldException e) {
e.printStackTrace();
}
}
return null;
}
/**
* 查询列表
* @param sql
* @param objs
* @return java.util.List<T>
* @author XanderYe
* @date 2020/9/1
*/
public static List<Map<String, Object>> queryList(String sql, Object... objs) throws SQLException {
List<Map<String, Object>> list = new ArrayList<>();
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = getConn();
ps = conn.prepareStatement(sql);
for (int i = 0; i < objs.length; i++) {
ps.setObject(i + 1, objs[i]);
}
rs = ps.executeQuery();
while (rs.next()) {
Map<String, Object> map = getMapFromResultSet(rs);
if (map != null) {
list.add(map);
}
}
} catch (IllegalArgumentException | SecurityException e) {
e.printStackTrace();
} finally {
close(rs, ps, conn);
}
return list;
}
/**
* 查询列表
* @param clazz
* @param sql
* @param objs
* @return java.util.List<T>
* @author XanderYe
* @date 2020/9/1
*/
public static <T> List<T> queryList(Class<T> clazz, String sql, Object... objs) throws SQLException {
List<T> list = new ArrayList<>();
List<Map<String, Object>> mapList = queryList(sql, objs);
for (Map<String, Object> map : mapList) {
try {
T obj = mapToObject(clazz, map);
if (obj != null) {
list.add(obj);
}
} catch (NoSuchFieldException | IllegalAccessException | InstantiationException e) {
e.printStackTrace();
}
}
return list;
}
/**
* resultSet转map
*
* @param resultSet
* @return java.util.Map<java.lang.String, java.lang.Object>
* @author XanderYe
* @date 2021/2/1
*/
private static Map<String, Object> getMapFromResultSet(ResultSet resultSet) throws SQLException {
ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
int columnCount = resultSetMetaData.getColumnCount();
if (columnCount > 0) {
Map<String, Object> map = new HashMap<>(16);
for (int i = 0; i < columnCount; i++) {
String columnName = resultSetMetaData.getColumnName(i + 1);
map.put(columnName, resultSet.getObject(columnName));
}
return map;
}
return null;
}
/**
* map转对象
* @param clazz
* @param map
* @return T
* @author XanderYe
* @date 2021/2/1
*/
private static <T> T mapToObject(Class<T> clazz, Map<String, Object> map) throws IllegalAccessException, InstantiationException, NoSuchFieldException {
T object = null;
if (!map.isEmpty()) {
Set<String> columnNames = map.keySet();
object = clazz.newInstance();
for (String column : columnNames) {
Object value = map.get(column);
String camel = underlineToCamel(column);
if (camel != null) {
try {
Field f = clazz.getDeclaredField(camel);
f.setAccessible(true);
f.set(object, value);
} catch (NoSuchFieldException ignored){}
}
}
}
return object;
}
/**
* 关闭资源
*
* @param rs
* @param ps
* @param conn
* @return void
* @author XanderYe
* @date 2020/9/1
*/
public static void close(ResultSet rs, PreparedStatement ps, Connection conn) {
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 驼峰转下划线
*
* @param param
* @return java.lang.String
* @author XanderYe
* @date 2020/9/1
*/
public static String camelToUnderline(String param) {
if (param == null) {
return null;
}
StringBuilder sb = new StringBuilder();
int len = param.length();
for (int i = 0; i < len; i++) {
char c = param.charAt(i);
if (Character.isUpperCase(c)) {
sb.append(UNDERLINE);
sb.append(Character.toLowerCase(c));
} else {
sb.append(c);
}
}
return sb.toString();
}
/**
* 下划线转驼峰
*
* @param param
* @return java.lang.String
* @author XanderYe
* @date 2020/9/1
*/
public static String underlineToCamel(String param) {
if (param == null) {
return null;
}
param = param.toLowerCase();
StringBuilder sb = new StringBuilder();
int len = param.length();
for (int i = 0; i < len; i++) {
char c = param.charAt(i);
if (c == UNDERLINE) {
if (++i < len) {
sb.append(Character.toUpperCase(param.charAt(i)));
}
} else {
sb.append(c);
}
}
return sb.toString();
}
}