From a8f40fbc2911591798d05182c6d634231b96ffab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=8B=E5=A4=9C=E6=97=A0=E9=9C=9C?= Date: Thu, 31 Dec 2020 15:39:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=BA=E4=BA=86=E6=8F=90=E9=AB=98=E5=AF=B9Xm?= =?UTF-8?q?lElement=E5=AE=9E=E4=BE=8B=E5=AF=B9=E8=B1=A1=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E7=AE=80=E5=8C=96=E4=BB=A3=E7=A0=81=E7=BC=96=E5=86=99=EF=BC=8C?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=94=AF=E6=8C=81=E5=BB=BA=E9=80=A0=E8=80=85?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F=E7=89=B9=E6=80=A7=E6=94=AF=E6=8C=81=EF=BC=9A?= =?UTF-8?q?XmlElementBuilder=E3=80=82=20=E5=8D=95=E5=85=83=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E7=B1=BB=EF=BC=9AXmlElementBuilderTest?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugins/builder/XmlElementBuilder.java | 70 +++++++++++++++++++ .../builder/XmlElementBuilderTest.java | 52 ++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 src/main/java/com/itfsw/mybatis/generator/plugins/builder/XmlElementBuilder.java create mode 100644 src/test/java/com/itfsw/mybatis/generator/plugins/builder/XmlElementBuilderTest.java 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); + } +}