1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.generator.plugins;
17
18 import java.util.List;
19
20 import org.mybatis.generator.api.IntrospectedTable;
21 import org.mybatis.generator.api.PluginAdapter;
22 import org.mybatis.generator.api.dom.xml.Attribute;
23 import org.mybatis.generator.api.dom.xml.Document;
24 import org.mybatis.generator.api.dom.xml.XmlElement;
25 import org.mybatis.generator.internal.util.StringUtility;
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public class CachePlugin extends PluginAdapter {
48 public enum CacheProperty {
49 EVICTION("cache_eviction", "eviction"),
50 FLUSH_INTERVAL("cache_flushInterval", "flushInterval"),
51 READ_ONLY("cache_readOnly", "readOnly"),
52 SIZE("cache_size", "size"),
53 TYPE("cache_type", "type");
54
55 private String propertyName;
56 private String attributeName;
57
58 CacheProperty(String propertyName, String attributeName) {
59 this.propertyName = propertyName;
60 this.attributeName = attributeName;
61 }
62
63 public String getPropertyName() {
64 return propertyName;
65 }
66
67 public String getAttributeName() {
68 return attributeName;
69 }
70 }
71
72 public CachePlugin() {
73 super();
74 }
75
76 public boolean validate(List<String> warnings) {
77 return true;
78 }
79
80 @Override
81 public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {
82
83 XmlElement element = new XmlElement("cache");
84 context.getCommentGenerator().addComment(element);
85
86 for (CacheProperty cacheProperty : CacheProperty.values()) {
87 addAttributeIfExists(element, introspectedTable, cacheProperty);
88 }
89
90 document.getRootElement().addElement(element);
91
92 return true;
93 }
94
95 private void addAttributeIfExists(XmlElement element, IntrospectedTable introspectedTable,
96 CacheProperty cacheProperty) {
97 String property = introspectedTable.getTableConfigurationProperty(cacheProperty.getPropertyName());
98 if (property == null) {
99 property = properties.getProperty(cacheProperty.getPropertyName());
100 }
101
102 if (StringUtility.stringHasValue(property)) {
103 element.addAttribute(new Attribute(cacheProperty.getAttributeName(), property));
104 }
105 }
106 }