11-SqlBuildUtil

SQL语句构建

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
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
* 构造SQL语句
*
* @author Jerry(姜源)
* @date 20/9/4 16:13
*/
@SuppressWarnings("all")
public class SqlBuildUtil {

protected static Logger logger = Logger.getLogger(SqlBuildUtil.class);

/**
* 构造批量插入 SQL 语句
*
* @param
* @return java.util.Map
* @throws
* @author Jerry(姜源)
* @date 2020-09-04 16:15
*/
public static Map batchInsert(List sourceList, Class clazz) throws Exception {
List list = new ArrayList<>();
//代理类对象转普通类对象
sourceList.stream().forEach(o -> {
try {
Object obj = Class.forName(clazz.getName()).newInstance();
BeanUtils.setBean(obj, BeanUtils.bean2Map(o));
list.add(obj);
} catch (Exception e) {
e.printStackTrace();
}
});
//表名
String tableName = clazz.getSimpleName();
//类的属性名列表
List<Field> fieldList = Arrays.asList(clazz.getDeclaredFields());
//SQL
StringBuilder sql = new StringBuilder();
StringBuilder keySb = new StringBuilder("INSERT INTO");
keySb.append(" `").append(tableName).append("` ");
keySb.append(" (");
int fieldLength = fieldList.size();
//拼接属性名
for (int i = 0; i < fieldLength; i++) {
Field field = fieldList.get(i);
keySb.append("`").append(field.getName()).append("`");
if (i != fieldLength - 1) {
keySb.append(",");
}
}
keySb.append(") ").append("VALUES");
//拼接属性值
int listSize = list.size();
for (int i = 0; i < listSize; i++) {
StringBuilder valSb = new StringBuilder();
valSb.append(" (");
for (int j = 0; j < fieldLength; j++) {
Field field = fieldList.get(j);
field.setAccessible(true);
Object val = field.get(list.get(i));
if (String.class.equals(field.getType())) {
valSb.append("'").append(ObjectUtils.isEmpty(val) ? "" : val).append("'");
} else {
valSb.append(ObjectUtils.isEmpty(val) ? 0 : val);
}
if (j != fieldLength - 1) {
valSb.append(",");
}
}
valSb.append(");\n");
sql.append(keySb).append(valSb);
logger.info("构造表名为 " + tableName + " 的恢复数据量为 " + listSize + " 条,SQL:\n" + sql.toString());
}
return MapUtils.toMap(new Object[][]{
{"sql", sql},
{"count", listSize},
});
}

}

11-SqlBuildUtil
https://janycode.github.io/2020/09/04/21_代码片段/01_工具类/11-SqlBuildUtil/
作者
Jerry(姜源)
发布于
2020年9月4日
许可协议