Java Code Examples for org.mybatis.generator.api.dom.xml.XmlElement#getElements()

The following examples show how to use org.mybatis.generator.api.dom.xml.XmlElement#getElements() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IncrementsPlugin.java    From mybatis-generator-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * 有Selective代码生成
 * @param element
 */
private void generatedWithSelective(XmlElement element, IntrospectedTable introspectedTable, boolean hasPrefix) {
    if (this.support()) {
        // 查找 set->if->text
        List<XmlElement> sets = XmlElementTools.findXmlElements(element, "set");
        if (sets.size() > 0) {
            List<XmlElement> ifs = XmlElementTools.findXmlElements(sets.get(0), "if");
            if (ifs.size() > 0) {
                for (XmlElement xmlElement : ifs) {
                    // 下面为if的text节点
                    List<Element> textEles = xmlElement.getElements();
                    TextElement textEle = (TextElement) textEles.get(0);
                    String[] strs = textEle.getContent().split("=");
                    String columnName = strs[0].trim();
                    IntrospectedColumn introspectedColumn = IntrospectedTableTools.safeGetColumn(introspectedTable, columnName);
                    // 查找是否需要进行增量操作
                    List<Element> incrementEles = PluginTools.getHook(IIncrementsPluginHook.class).incrementSetElementGenerated(introspectedColumn, hasPrefix ? "record." : null, true);
                    if (!incrementEles.isEmpty()) {
                        xmlElement.getElements().clear();
                        xmlElement.getElements().addAll(incrementEles);
                    }
                }
            }
        }
    }
}
 
Example 2
Source File: IncrementPlugin.java    From mybatis-generator-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * 有Selective代码生成
 * @param element
 */
private void generatedWithSelective(XmlElement element, IntrospectedTable introspectedTable, boolean hasPrefix) {
    if (this.support()) {
        // 查找 set->if->text
        List<XmlElement> sets = XmlElementTools.findXmlElements(element, "set");
        if (sets.size() > 0) {
            List<XmlElement> ifs = XmlElementTools.findXmlElements(sets.get(0), "if");
            if (ifs.size() > 0) {
                for (XmlElement xmlElement : ifs) {
                    // 下面为if的text节点
                    List<Element> textEles = xmlElement.getElements();
                    TextElement textEle = (TextElement) textEles.get(0);
                    String[] strs = textEle.getContent().split("=");
                    String columnName = strs[0].trim();
                    IntrospectedColumn introspectedColumn = IntrospectedTableTools.safeGetColumn(introspectedTable, columnName);
                    if (this.supportIncrement(introspectedColumn)) {
                        XmlElementTools.replaceXmlElement(xmlElement, PluginTools.getHook(IIncrementPluginHook.class).generateIncrementSetSelective(introspectedColumn, hasPrefix ? "record." : null));
                    }
                }
            }
        }
    }
}
 
Example 3
Source File: OracleSupport.java    From mybatis-generator-plus with Apache License 2.0 6 votes vote down vote up
/**
 * 在单条插入动态sql中增加查询序列,以实现oracle主键自增
 *
 * @author 吴帅
 * @parameter @param element
 * @parameter @param introspectedTable
 * @createDate 2015年9月29日 下午12:00:37
 */
@Override
public void adaptInsertSelective(XmlElement element, IntrospectedTable introspectedTable) {
    TableConfiguration tableConfiguration = introspectedTable.getTableConfiguration();
    Properties properties = tableConfiguration.getProperties();
    String incrementFieldName = properties.getProperty("incrementField");
    if (incrementFieldName != null) {// 有自增字段的配置
        List<Element> elements = element.getElements();
        XmlElement selectKey = new XmlElement("selectKey");
        selectKey.addAttribute(new Attribute("keyProperty", incrementFieldName));
        selectKey.addAttribute(new Attribute("resultType", "java.lang.Long"));
        selectKey.addAttribute(new Attribute("order", "BEFORE"));
        selectKey.addElement(new TextElement("select "));
        XmlElement includeSeq = new XmlElement("include");
        includeSeq.addAttribute(new Attribute("refid", "TABLE_SEQUENCE"));
        selectKey.addElement(includeSeq);
        selectKey.addElement(new TextElement(" from dual"));
        elements.add(0, selectKey);
    }
}
 
Example 4
Source File: AliasPlugin.java    From maven-archetype with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * getElementById: 在指定的xmlElement的一级子元素中查找id值为指定value的Element. <br/>
 *
 * @author Hongbin Yuan
 * @param xmlElement	指定的xmlElement元素  <br/>
 * @param value			查找id值为指定value的Element  <br/>
 * @return
 * @since JDK 1.6
 */
public XmlElement getElementById(XmlElement xmlElement,String value){
	if(xmlElement == null){
		return null;
	}
	if(value == null || "".equals(value.trim())){
		return xmlElement;
	}
	List<Element> elementList = xmlElement.getElements();
	if(elementList != null && elementList.size() > 0){
		for(Element e : elementList){
			if(e instanceof XmlElement){
				XmlElement eXML = (XmlElement)e;
				List<Attribute> eXMLAttrList = eXML.getAttributes();
				for(Attribute a : eXMLAttrList){
					if(a!=null && "id".equals(a.getName()) && value.equals(a.getValue())){
						return eXML;
					}
				}
			}
		}
	}
	return null;
}
 
Example 5
Source File: DeleteAtPlugin.java    From S-mall-ssm with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean sqlMapExampleWhereClauseElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {

    for (Element child : element.getElements()) {
        if (child instanceof XmlElement && ((XmlElement) child).getName().equals("where")) {
            TextElement element1 = new TextElement("and deleteAt is NULL");
            ((XmlElement) child).getElements().add(element1);
            break;
        }
    }
    return true;
}
 
Example 6
Source File: IncrementsPlugin.java    From mybatis-generator-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * 无Selective代码生成
 * @param xmlElement
 * @param introspectedTable
 * @param hasPrefix
 */
private void generatedWithoutSelective(XmlElement xmlElement, IntrospectedTable introspectedTable, boolean hasPrefix) {
    if (this.support()) {
        List<Element> newEles = new ArrayList<>();
        for (Element ele : xmlElement.getElements()) {
            // 找到text节点且格式为 set xx = xx 或者 xx = xx
            if (ele instanceof TextElement) {
                String text = ((TextElement) ele).getContent().trim();
                if (text.matches("(^set\\s)?\\S+\\s?=.*")) {
                    // 清理 set 操作
                    text = text.replaceFirst("^set\\s", "").trim();
                    String columnName = text.split("=")[0].trim();
                    IntrospectedColumn introspectedColumn = IntrospectedTableTools.safeGetColumn(introspectedTable, columnName);
                    // 查找判断是否需要进行节点替换
                    List<Element> incrementEles = PluginTools.getHook(IIncrementsPluginHook.class).incrementSetElementGenerated(introspectedColumn, hasPrefix ? "record." : null, text.endsWith(","));
                    if (!incrementEles.isEmpty()) {
                        newEles.addAll(incrementEles);

                        continue;
                    }
                }
            }
            newEles.add(ele);
        }

        // 替换节点
        xmlElement.getElements().clear();
        xmlElement.getElements().addAll(newEles);
    }
}
 
Example 7
Source File: XmlElementTools.java    From mybatis-generator-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * 查找指定xml节点下指定节点名称的元素
 * @param xmlElement
 * @param name
 * @return
 */
public static List<XmlElement> findXmlElements(XmlElement xmlElement, String name) {
    List<XmlElement> list = new ArrayList<>();
    List<Element> elements = xmlElement.getElements();
    for (Element ele : elements) {
        if (ele instanceof XmlElement) {
            XmlElement xmlElement1 = (XmlElement) ele;
            if (name.equalsIgnoreCase(xmlElement1.getName())) {
                list.add(xmlElement1);
            }
        }
    }
    return list;
}
 
Example 8
Source File: XmlElementTools.java    From mybatis-generator-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * 查询指定xml下所有text xml 节点
 * @param xmlElement
 * @return
 */
public static List<TextElement> findAllTextElements(XmlElement xmlElement){
    List<TextElement> textElements = new ArrayList<>();
    for (Element element : xmlElement.getElements()){
        if (element instanceof XmlElement){
            textElements.addAll(findAllTextElements((XmlElement) element));
        } else if (element instanceof TextElement){
            textElements.add((TextElement) element);
        }
    }
    return textElements;
}
 
Example 9
Source File: XmlElementTools.java    From mybatis-generator-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * 拷贝
 * @param element
 * @return
 */
public static XmlElement clone(XmlElement element) {
    XmlElement destEle = new XmlElement(element.getName());
    for (Attribute attribute : element.getAttributes()) {
        destEle.addAttribute(XmlElementTools.clone(attribute));
    }
    for (Element ele : element.getElements()) {
        if (ele instanceof XmlElement) {
            destEle.addElement(XmlElementTools.clone((XmlElement) ele));
        } else if (ele instanceof TextElement) {
            destEle.addElement(XmlElementTools.clone((TextElement) ele));
        }
    }
    return destEle;
}
 
Example 10
Source File: SQLServerPaginationPlugin.java    From dolphin with Apache License 2.0 5 votes vote down vote up
@Override
public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {

  XmlElement orderBy = (XmlElement) findElement(element.getElements(), "if", "test", "orderByClause != null");
  XmlElement baseColumnList = (XmlElement) findElement(element.getElements(), "include", "refid", introspectedTable.getBaseColumnListId());

  XmlElement prefix = buildPrefix(baseColumnList);
  XmlElement inner = buildInner(orderBy, introspectedTable);
  XmlElement suffix = buildSuffix(orderBy);


  List<Element> elements = element.getElements();
  List<Element> newElements = new ArrayList<>();

  newElements.add(prefix);
  for (Element e : elements) {
    if(e != orderBy) {
      newElements.add(e);
      if( e == baseColumnList){
        newElements.add(inner);
      }
    }
  }
  newElements.add(suffix);

  elements.clear();
  elements.addAll(newElements);
  return true;
}
 
Example 11
Source File: SQLServerPaginationPlugin.java    From dolphin with Apache License 2.0 5 votes vote down vote up
private XmlElement buildInner(XmlElement orderBy, IntrospectedTable introspectedTable) {
  // generate order by clause, first check if user provided order by clause, if provided just use it
  // otherwise use the default primary key for order by clause
  XmlElement newOrderBy = new XmlElement("choose");
  XmlElement when = new XmlElement("when");
  when.addAttribute(new Attribute("test", "orderByClause != null"));
  for (Element e : orderBy.getElements()) {
    when.addElement(e);
  }
  newOrderBy.addElement(when);

  XmlElement otherwise = new XmlElement("otherwise");
  StringBuilder sb = new StringBuilder();
  sb.append(" order by ");
  List<IntrospectedColumn> columns = introspectedTable.getPrimaryKeyColumns();
  for (IntrospectedColumn column : columns) {
    sb.append(MyBatis3FormattingUtilities.getAliasedEscapedColumnName(column)).append(", ");
  }
  sb.setLength(sb.length() - 2);
  otherwise.addElement(new TextElement(sb.toString()));
  newOrderBy.addElement(otherwise);

  XmlElement inner = new XmlElement("if");
  inner.addAttribute(new Attribute("test", "limit != null and limit>=0 and offset != null"));
  inner.addElement(new TextElement(" , ROW_NUMBER() over ( "));
  inner.addElement(newOrderBy);
  inner.addElement(new TextElement(" ) as row_num "));
  return inner;
}
 
Example 12
Source File: SqlServerSupport.java    From mybatis-generator-plus with Apache License 2.0 4 votes vote down vote up
/**
 * 在xml文件的查询配置中加入分页支持
 *
 * @author 吴帅
 * @parameter @param element
 * @parameter @param preFixId
 * @parameter @param sufFixId
 * @createDate 2015年9月29日 上午11:59:06
 */
@Override
public XmlElement adaptSelectByExample(XmlElement element, IntrospectedTable introspectedTable) {
    //1.先取出原元素
    List<Element> elements = element.getElements();
    Element element0 = elements.get(0);//select
    XmlElement element1 = (XmlElement) elements.get(1);//<if test="distinct">distinct</if>
    XmlElement element2 = (XmlElement) elements.get(2);//<include refid="Base_Column_List" />
    Element element3 = elements.get(3);//from bus_log
    XmlElement element4 = (XmlElement) elements.get(4);//<if test="_parameter != null"><include refid="Example_Where_Clause" /></if>
    XmlElement element5 = (XmlElement) elements.get(5);//<if test="orderByClause != null">order by ${orderByClause}</if>

    element.removeElement(0);
    element.removeElement(0);
    element.removeElement(0);
    element.removeElement(0);
    element.removeElement(0);
    element.removeElement(0);


    //part1 <include refid="SqlServerDialectPrefix" />
    XmlElement prefix = new XmlElement("include");
    prefix.addAttribute(new Attribute("refid", "SqlServerDialectPrefix"));
    element.addElement(prefix);
    //part2 select
    element.addElement(element0);
    //part3 <if test="distinct"> distinct	</if>
    element.addElement(element1);
    //part4 row_number() over(
    element.addElement(new TextElement("row_number() over("));
    //part5 <if test="orderByClause != null"> order by ${orderByClause}	</if>
    element.addElement(element5);
    //part6 <if test="orderByClause == null"> order by incrementField </if>
    String incrementField = introspectedTable.getTableConfiguration().getProperties().getProperty("incrementField");
    XmlElement incrementFieldIf = new XmlElement("if");
    incrementFieldIf.addAttribute(new Attribute("test", "orderByClause == null"));
    incrementFieldIf.addElement(new TextElement("order by " + incrementField));
    element.addElement(incrementFieldIf);
    //part7 ) as _pagination_rownumber,
    element.addElement(new TextElement(") as _pagination_rownumber,"));
    //part8 <include refid="Base_Column_List" />
    element.addElement(element2);
    //part9 from table_name
    element.addElement(element3);
    //part10 <if test="_parameter != null"><include refid="Example_Where_Clause" /></if>
    element.addElement(element4);
    //part11 <include refid="SqlServerDialectSuffix" />
    XmlElement suffix = new XmlElement("include");
    suffix.addAttribute(new Attribute("refid", "SqlServerDialectSuffix"));
    element.addElement(suffix);

    return element;
}