Skip to content

为了提高对XmlElement实例对象创建简化代码编写,新增支持建造者模式特性支持:XmlElementBuilder。 #123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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 的建造者模式
* <code>
* XmlElement xmlElement = new XmlElementBuilder().name("sql")
* .attribute("id","queryCondition")
* .element(whereElement)
* .build();
* </code>
* @see XmlElement
* @Date : 2020/7/2 上午10:43
* @Author : 石冬冬-Seig Heil
*/
public class XmlElementBuilder {

/** The attributes. */
private List<Attribute> attributes = new ArrayList<>();

/** The elements. */
private List<Element> 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<Element> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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
*
* <code>
* <sql id="queryCondition">
<where>
<if test="year != null">
and year = #{year}
</if>
<if test="month != null">
and month = #{month}
</if>
</where>
</sql>
</code>
*/
@Test
public void test(){

List<Element> 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);
}
}