diff --git a/src/main/java/com/itfsw/mybatis/generator/plugins/builder/XmlElementBuilder.java b/src/main/java/com/itfsw/mybatis/generator/plugins/builder/XmlElementBuilder.java new file mode 100644 index 00000000..8fc0580d --- /dev/null +++ b/src/main/java/com/itfsw/mybatis/generator/plugins/builder/XmlElementBuilder.java @@ -0,0 +1,70 @@ +package com.itfsw.mybatis.generator.plugins.builder; + +import org.mybatis.generator.api.dom.xml.Attribute; +import org.mybatis.generator.api.dom.xml.Element; +import org.mybatis.generator.api.dom.xml.TextElement; +import org.mybatis.generator.api.dom.xml.XmlElement; + +import java.util.ArrayList; +import java.util.List; + +/** + * @description: XmlElement 的建造者模式 + * + * XmlElement xmlElement = new XmlElementBuilder().name("sql") + * .attribute("id","queryCondition") + * .element(whereElement) + * .build(); + * + * @see XmlElement + * @Date : 2020/7/2 上午10:43 + * @Author : 石冬冬-Seig Heil + */ +public class XmlElementBuilder { + + /** The attributes. */ + private List attributes = new ArrayList<>(); + + /** The elements. */ + private List elements = new ArrayList<>(); + + /** The name. */ + private String name; + + + public XmlElementBuilder name(String name){ + this.name = name; + return this; + } + + public XmlElementBuilder attribute(String name, String value){ + attributes.add(new Attribute(name,value)); + return this; + } + + public XmlElementBuilder text(String text){ + elements.add(new TextElement(text)); + return this; + } + + public XmlElementBuilder element(Element element){ + elements.add(element); + return this; + } + + public XmlElementBuilder elements(List list){ + elements.addAll(list); + return this; + } + + /** + * 提供 XmlElement 实例对象属性的封装 + * @return + */ + public XmlElement build(){ + XmlElement xmlElement = new XmlElement(this.name); + attributes.forEach(each -> xmlElement.addAttribute(each)); + elements.forEach(each -> xmlElement.addElement(each)); + return xmlElement; + } +} diff --git a/src/test/java/com/itfsw/mybatis/generator/plugins/builder/XmlElementBuilderTest.java b/src/test/java/com/itfsw/mybatis/generator/plugins/builder/XmlElementBuilderTest.java new file mode 100644 index 00000000..93f3dd6c --- /dev/null +++ b/src/test/java/com/itfsw/mybatis/generator/plugins/builder/XmlElementBuilderTest.java @@ -0,0 +1,52 @@ +package com.itfsw.mybatis.generator.plugins.builder; + +import lombok.extern.slf4j.Slf4j; +import org.junit.Test; +import org.mybatis.generator.api.dom.xml.Element; +import org.mybatis.generator.api.dom.xml.XmlElement; + +import java.util.ArrayList; +import java.util.List; + +/** + * @description: XmlElementBuilder + * @Date : 2020/7/2 上午10:56 + * @Author : 石冬冬-Seig Heil + */ +@Slf4j +public class XmlElementBuilderTest { + + /** + * for console + * + * + * + + + and year = #{year} + + + and month = #{month} + + + + + */ + @Test + public void test(){ + + List ifElements = new ArrayList<>(); + + ifElements.add(new com.itfsw.mybatis.generator.plugins.builder.XmlElementBuilder().name("if").attribute("test","year != null").text(" and year = #{year}").build()); + ifElements.add(new com.itfsw.mybatis.generator.plugins.builder.XmlElementBuilder().name("if").attribute("test","month != null").text(" and month = #{month}").build()); + + XmlElement whereElement = new com.itfsw.mybatis.generator.plugins.builder.XmlElementBuilder().name("where").elements(ifElements).build(); + + XmlElement xmlElement = new com.itfsw.mybatis.generator.plugins.builder.XmlElementBuilder().name("sql") + .attribute("id","queryCondition") + .element(whereElement) + .build(); + String sql = xmlElement.getFormattedContent(1); + log.info("\n{}",sql); + } +}