Skip to content

Commit 04f7ec9

Browse files
author
dongdongshi5
committed
为了提高对XmlElement实例对象创建简化代码编写,新增支持建造者模式特性支持:XmlElementBuilder。
单元测试类:XmlElementBuilderTest
1 parent 8b70974 commit 04f7ec9

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.itfsw.mybatis.generator.plugins.builder;
2+
3+
import org.mybatis.generator.api.dom.xml.Attribute;
4+
import org.mybatis.generator.api.dom.xml.Element;
5+
import org.mybatis.generator.api.dom.xml.TextElement;
6+
import org.mybatis.generator.api.dom.xml.XmlElement;
7+
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
/**
12+
* @description: XmlElement 的建造者模式
13+
* <code>
14+
* XmlElement xmlElement = new XmlElementBuilder().name("sql")
15+
* .attribute("id","queryCondition")
16+
* .element(whereElement)
17+
* .build();
18+
* </code>
19+
* @see XmlElement
20+
* @Date : 2020/7/2 上午10:43
21+
* @Author : 石冬冬-Seig Heil
22+
*/
23+
public class XmlElementBuilder {
24+
25+
/** The attributes. */
26+
private List<Attribute> attributes = new ArrayList<>();
27+
28+
/** The elements. */
29+
private List<Element> elements = new ArrayList<>();
30+
31+
/** The name. */
32+
private String name;
33+
34+
35+
public XmlElementBuilder name(String name){
36+
this.name = name;
37+
return this;
38+
}
39+
40+
public XmlElementBuilder attribute(String name, String value){
41+
attributes.add(new Attribute(name,value));
42+
return this;
43+
}
44+
45+
public XmlElementBuilder text(String text){
46+
elements.add(new TextElement(text));
47+
return this;
48+
}
49+
50+
public XmlElementBuilder element(Element element){
51+
elements.add(element);
52+
return this;
53+
}
54+
55+
public XmlElementBuilder elements(List<Element> list){
56+
elements.addAll(list);
57+
return this;
58+
}
59+
60+
/**
61+
* 提供 XmlElement 实例对象属性的封装
62+
* @return
63+
*/
64+
public XmlElement build(){
65+
XmlElement xmlElement = new XmlElement(this.name);
66+
attributes.forEach(each -> xmlElement.addAttribute(each));
67+
elements.forEach(each -> xmlElement.addElement(each));
68+
return xmlElement;
69+
}
70+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.itfsw.mybatis.generator.plugins.builder;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.junit.Test;
5+
import org.mybatis.generator.api.dom.xml.Element;
6+
import org.mybatis.generator.api.dom.xml.XmlElement;
7+
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
/**
12+
* @description: XmlElementBuilder
13+
* @Date : 2020/7/2 上午10:56
14+
* @Author : 石冬冬-Seig Heil
15+
*/
16+
@Slf4j
17+
public class XmlElementBuilderTest {
18+
19+
/**
20+
* for console
21+
*
22+
* <code>
23+
* <sql id="queryCondition">
24+
<where>
25+
<if test="year != null">
26+
and year = #{year}
27+
</if>
28+
<if test="month != null">
29+
and month = #{month}
30+
</if>
31+
</where>
32+
</sql>
33+
</code>
34+
*/
35+
@Test
36+
public void test(){
37+
38+
List<Element> ifElements = new ArrayList<>();
39+
40+
ifElements.add(new XmlElementBuilder().name("if").attribute("test","year != null").text(" and year = #{year}").build());
41+
ifElements.add(new XmlElementBuilder().name("if").attribute("test","month != null").text(" and month = #{month}").build());
42+
43+
XmlElement whereElement = new XmlElementBuilder().name("where").elements(ifElements).build();
44+
45+
XmlElement xmlElement = new XmlElementBuilder().name("sql")
46+
.attribute("id","queryCondition")
47+
.element(whereElement)
48+
.build();
49+
String sql = xmlElement.getFormattedContent(1);
50+
log.info("\n{}",sql);
51+
}
52+
}

0 commit comments

Comments
 (0)