admin
2022-04-07 211840b64fa1132d76d6dff6c779e9ba2c0c450f
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
package org.yeshi.utils.generater.mybatis;
 
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
 
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.dom4j.tree.AbstractAttribute;
import org.yeshi.utils.StringUtil;
 
public class ColumnUtil {
 
 
    /**
     * @author hxh
     * @description 根据属性获取sql中的驼峰字段
     * @date 15:44 2022/3/31
     * @param: property
     * @return java.lang.String
     **/
    public static String getColumnFromProperty(String property) {
        StringBuffer buffer = new StringBuffer();
        List<String> temp = new ArrayList<>();
        for (int i = 0; i < property.length(); i++) {
            //是否为大写
            if (property.charAt(i) >= 65 && property.charAt(i) <= 90) {
                temp.add(property.charAt(i) + "");
            } else {
                //消费之前的
                if (temp.size() > 0) {
                    buffer.append(("_" + StringUtil.concat(temp, "")).toLowerCase());
                    temp.clear();
                }
                buffer.append(property.charAt(i));
            }
        }
 
        if (temp.size() > 0) {
            buffer.append(("_" + StringUtil.concat(temp, "")).toLowerCase());
            temp.clear();
        }
        return buffer.toString();
    }
 
 
    @SuppressWarnings("rawtypes")
    public static void addColumnToMapper(String path, String columnName, String property, String type) {
        File xmlPath = new File(path);
        SAXReader reader = new SAXReader();
        Document document = null;
        try {
            document = reader.read(xmlPath);
            Element root = document.getRootElement();
            // 得到根元素的所有子节点
            List<Element> elementList = root.elements();
            // 遍历所有子节点
            for (Element e : elementList) {
                if (e.getName().equals("resultMap")) {
                    AbstractAttribute attr = (AbstractAttribute) e.attribute("id");
                    if ("BaseResultMap".equalsIgnoreCase(attr.getValue())) {
                        List list = e.elements("result");
                        boolean isE = false;
                        for (int i = 0; i < list.size(); i++) {
                            Element item = (Element) list.get(i);
                            attr = (AbstractAttribute) item.attribute("property");
                            if (attr.getValue().trim().equalsIgnoreCase(property)) {
                                isE = true;
                                break;
                            }
                        }
                        if (!isE) {
                            List<KeyPair> li = new ArrayList<>();
                            li.add(new KeyPair("column", columnName));
                            li.add(new KeyPair("property", property));
                            li.add(new KeyPair("jdbcType", type));
                            e.add(createElement("result", li, null));
                        }
                    }
                } else if (e.getName().equals("sql")) {
                    AbstractAttribute attr = (AbstractAttribute) e.attribute("id");
                    if ("Base_Column_List".equalsIgnoreCase(attr.getValue())) {
                        String columns = e.getText();
                        String[] cs = columns.split(",");
                        boolean isE = false;
                        for (String c : cs) {
                            if (c.trim().equalsIgnoreCase(columnName)) {
                                isE = true;
                                break;
                            }
                        }
                        if (!isE) {
                            columns += ("," + columnName);
                            e.setText(columns);
                        }
                    }
 
                } else if (e.getName().equals("insert")) {
                    AbstractAttribute attr = (AbstractAttribute) e.attribute("id");
                    if ("insert".equalsIgnoreCase(attr.getValue())) {
                        String insert = e.getText();
                        String[] cs = insert.split("values ");
                        if (cs.length > 1) {
                            int start = cs[0].indexOf("(");
                            int end = cs[0].indexOf(")");
                            String columns = cs[0].substring(start, end);
                            String[] cols = columns.split(",");
                            boolean isE = false;
                            for (String col : cols) {
                                if (col.trim().equalsIgnoreCase(columnName)) {
                                    isE = true;
                                    break;
                                }
                            }
                            if (!isE) {
                                cs[0] = cs[0].replace(columns, columns + ("," + columnName));
                                start = cs[1].indexOf("(");
                                end = cs[1].indexOf(")");
                                String columns2 = cs[1].substring(start, end);
                                cs[1] = cs[1].replace(columns2,
                                        columns2 + String.format(",#{%s,jdbcType=%s}", property, type));
                                insert = cs[0] + " values " + cs[1];
                                e.setText(insert);
                            }
                        }
 
                    } else if ("insertSelective".equalsIgnoreCase(attr.getValue())) {
                        List trims = e.elements("trim");
                        Element els = (Element) trims.get(0);
                        boolean isE = false;
                        for (int i = 0; i < els.elements("if").size(); i++) {
                            String cn = ((Element) els.elements("if").get(i)).getText().replace(",", "").trim();
                            if (columnName.equalsIgnoreCase(cn)) {
                                isE = true;
                                break;
                            }
                        }
                        if (!isE) {
                            List<KeyPair> list = new ArrayList<>();
                            list.add(new KeyPair("test", String.format("%s != null", property)));
                            els.add(createElement("if", list, columnName + ","));
 
                            els = (Element) trims.get(1);
                            list = new ArrayList<>();
                            list.add(new KeyPair("test", String.format("%s != null", property)));
                            els.add(createElement("if", list, String.format("#{%s,jdbcType=%s}", property, type)));
                        }
                    }
 
                } else if (e.getName().equals("update")) {
                    AbstractAttribute attr = (AbstractAttribute) e.attribute("id");
                    if ("updateByPrimaryKeySelective".equalsIgnoreCase(attr.getValue())) {
                        List list = ((Element) e.elements("set").get(0)).elements("if");
                        boolean isE = false;
                        for (int i = 0; i < list.size(); i++) {
                            if (((Element) list.get(i)).getText().indexOf(columnName) > -1) {
                                isE = true;
                                break;
                            }
                        }
                        if (!isE) {
                            List<KeyPair> klist = new ArrayList<>();
                            klist.add(new KeyPair("test", String.format("%s !=null", property)));
                            ((Element) e.elements("set").get(0)).add(createElement("if", klist,
                                    String.format("%s =#{%s,jdbcType=%s},", columnName, property, type)));
                        }
                    } else if ("updateByPrimaryKey".equalsIgnoreCase(attr.getValue())) {
                        String value = e.getText();
                        // 不存在该列
                        if (value.indexOf(columnName) <= -1) {
                            int wpos = value.indexOf("where ");
                            if (value.substring(0, wpos).trim().endsWith(","))
                                value = value.substring(0, wpos)
                                        + String.format("%s =#{%s,jdbcType=%s},", columnName, property, type) + " "
                                        + value.substring(wpos);
                            else
                                value = value.substring(0, wpos) + ","
                                        + String.format("%s =#{%s,jdbcType=%s},", columnName, property, type) + " "
                                        + value.substring(wpos);
 
                            e.setText(value);
                        }
                    }
 
                }
            }
        } catch (DocumentException e1) {
            e1.printStackTrace();
        }
        if (document != null) {
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding("UTF-8");
            try {
                XMLWriter writer = new XMLWriter(new FileOutputStream(path), format);
                writer.write(document);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
 
    private static Element createElement(String qName, List<KeyPair> keyPairList, String value) {
        Element element = DocumentHelper.createElement(qName);
        for (KeyPair kp : keyPairList) {
            element.addAttribute(kp.key, kp.value);
        }
        if (value != null)
            element.setText(value);
        return element;
    }
 
}
 
class KeyPair {
    String key;
    String value;
 
    public KeyPair(String key, String value) {
        this.key = key;
        this.value = value;
    }
 
    public KeyPair() {
 
    }
 
}