admin
2022-04-07 211840b64fa1132d76d6dff6c779e9ba2c0c450f
src/main/java/org/yeshi/utils/generater/mybatis/ColumnUtil.java
@@ -13,187 +13,221 @@
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.dom4j.tree.AbstractAttribute;
import org.yeshi.utils.StringUtil;
public class ColumnUtil {
   @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);
                     }
                  }
    /**
     * @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));
            }
        }
               } 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 + ","));
        if (temp.size() > 0) {
            buffer.append(("_" + StringUtil.concat(temp, "")).toLowerCase());
            temp.clear();
        }
        return buffer.toString();
    }
                     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);
    @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);
                        }
                    }
                     e.setText(value);
                  }
               }
                } 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);
                            }
                        }
            }
         }
      } 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();
         }
      }
   }
                    } 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 + ","));
   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;
   }
                            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;
    String key;
    String value;
   public KeyPair(String key, String value) {
      this.key = key;
      this.value = value;
   }
    public KeyPair(String key, String value) {
        this.key = key;
        this.value = value;
    }
   public KeyPair() {
    public KeyPair() {
   }
    }
}