Skip to content

Commit a887573

Browse files
authored
Merge pull request #57 from kazuki43zoo/gh-56
Add SQL provider class that help detecting a template file automatically
2 parents 85a49c1 + bd21c8b commit a887573

28 files changed

+1411
-16
lines changed

src/main/java/org/mybatis/scripting/freemarker/FreeMarkerLanguageDriver.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.apache.ibatis.scripting.LanguageDriver;
3333
import org.apache.ibatis.scripting.defaults.DefaultParameterHandler;
3434
import org.apache.ibatis.session.Configuration;
35+
import org.mybatis.scripting.freemarker.support.TemplateFilePathProvider;
3536

3637
/**
3738
* Adds FreeMarker templates support to scripting in MyBatis. If you want to change or extend template loader
@@ -64,6 +65,7 @@ public FreeMarkerLanguageDriver() {
6465
public FreeMarkerLanguageDriver(FreeMarkerLanguageDriverConfig driverConfig) {
6566
this.driverConfig = driverConfig;
6667
this.freemarkerCfg = createFreeMarkerConfiguration();
68+
TemplateFilePathProvider.setLanguageDriverConfig(driverConfig);
6769
}
6870

6971
/**
@@ -75,7 +77,7 @@ protected freemarker.template.Configuration createFreeMarkerConfiguration() {
7577
freemarker.template.Configuration.VERSION_2_3_22);
7678

7779
TemplateLoader templateLoader = new ClassTemplateLoader(this.getClass().getClassLoader(),
78-
driverConfig.getBasePackage());
80+
driverConfig.getTemplateFile().getBaseDir());
7981
cfg.setTemplateLoader(templateLoader);
8082

8183
// To avoid formatting numbers using spaces and commas in SQL

src/main/java/org/mybatis/scripting/freemarker/FreeMarkerLanguageDriverConfig.java

Lines changed: 219 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import freemarker.template.Version;
3535
import org.apache.commons.text.WordUtils;
3636
import org.apache.ibatis.io.Resources;
37+
import org.apache.ibatis.logging.Log;
38+
import org.apache.ibatis.logging.LogFactory;
3739
import org.apache.ibatis.reflection.DefaultReflectorFactory;
3840
import org.apache.ibatis.reflection.MetaObject;
3941
import org.apache.ibatis.reflection.factory.DefaultObjectFactory;
@@ -46,28 +48,30 @@
4648
* @since 1.2.0
4749
*/
4850
public class FreeMarkerLanguageDriverConfig {
49-
5051
private static final String PROPERTY_KEY_CONFIG_FILE = "mybatis-freemarker.config.file";
5152
private static final String PROPERTY_KEY_CONFIG_ENCODING = "mybatis-freemarker.config.encoding";
5253
private static final String DEFAULT_PROPERTIES_FILE = "mybatis-freemarker.properties";
53-
private static Map<Class<?>, Function<String, Object>> TYPE_CONVERTERS;
54+
private static final Map<Class<?>, Function<String, Object>> TYPE_CONVERTERS;
5455

5556
static {
5657
Map<Class<?>, Function<String, Object>> converters = new HashMap<>();
5758
converters.put(String.class, String::trim);
59+
converters.put(boolean.class, v -> Boolean.valueOf(v.trim()));
5860
converters.put(Object.class, v -> v);
5961
TYPE_CONVERTERS = Collections.unmodifiableMap(converters);
6062
}
6163

64+
private static final Log log = LogFactory.getLog(FreeMarkerLanguageDriverConfig.class);
65+
6266
/**
6367
* The configuration properties.
6468
*/
6569
private final Map<String, String> freemarkerSettings = new HashMap<>();
6670

6771
/**
68-
* The base directory for reading template resources.
72+
* Template file configuration.
6973
*/
70-
private String basePackage = "";
74+
private final TemplateFileConfig templateFile = new TemplateFileConfig();
7175

7276
/**
7377
* Get FreeMarker settings.
@@ -85,19 +89,228 @@ public Map<String, String> getFreemarkerSettings() {
8589
* </p>
8690
*
8791
* @return a base directory for reading template resources
92+
* @deprecated Recommend to use the {@link TemplateFileConfig#getBaseDir()}} because this method defined for keeping
93+
* backward compatibility (There is possibility that this method removed at a future version)
8894
*/
95+
@Deprecated
8996
public String getBasePackage() {
90-
return basePackage;
97+
return templateFile.getBaseDir();
9198
}
9299

93100
/**
94101
* Set a base directory for reading template resources.
95102
*
96103
* @param basePackage
97104
* a base directory for reading template resources
105+
* @deprecated Recommend to use the {@link TemplateFileConfig#setBaseDir(String)} because this method defined for
106+
* keeping backward compatibility (There is possibility that this method removed at a future version)
98107
*/
108+
@Deprecated
99109
public void setBasePackage(String basePackage) {
100-
this.basePackage = basePackage;
110+
log.warn("The 'basePackage' has been deprecated since 1.2.0. Please use the 'templateFile.baseDir'.");
111+
templateFile.setBaseDir(basePackage);
112+
}
113+
114+
/**
115+
* Get a template file configuration.
116+
*
117+
* @return a template file configuration
118+
*/
119+
public TemplateFileConfig getTemplateFile() {
120+
return templateFile;
121+
}
122+
123+
/**
124+
* Template file configuration.
125+
*/
126+
public static class TemplateFileConfig {
127+
128+
/**
129+
* The base directory for reading template resources.
130+
*/
131+
private String baseDir = "";
132+
133+
/**
134+
* The template file path provider configuration.
135+
*/
136+
private final PathProviderConfig pathProvider = new PathProviderConfig();
137+
138+
/**
139+
* Get the base directory for reading template resource file.
140+
* <p>
141+
* Default is {@code ""}(none).
142+
* </p>
143+
*
144+
* @return the base directory for reading template resource file
145+
*/
146+
public String getBaseDir() {
147+
return baseDir;
148+
}
149+
150+
/**
151+
* Set the base directory for reading template resource file.
152+
*
153+
* @param baseDir
154+
* the base directory for reading template resource file
155+
*/
156+
public void setBaseDir(String baseDir) {
157+
this.baseDir = baseDir;
158+
}
159+
160+
/**
161+
* Get the template file path provider configuration.
162+
*
163+
* @return the template file path provider configuration
164+
*/
165+
public PathProviderConfig getPathProvider() {
166+
return pathProvider;
167+
}
168+
169+
/**
170+
* The template file path provider configuration.
171+
*/
172+
public static class PathProviderConfig {
173+
174+
/**
175+
* The prefix for adding to template file path.
176+
*/
177+
private String prefix = "";
178+
179+
/**
180+
* Whether includes package path part.
181+
*/
182+
private boolean includesPackagePath = true;
183+
184+
/**
185+
* Whether separate directory per mapper.
186+
*/
187+
private boolean separateDirectoryPerMapper = true;
188+
189+
/**
190+
* Whether includes mapper name into file name when separate directory per mapper.
191+
*/
192+
private boolean includesMapperNameWhenSeparateDirectory = true;
193+
194+
/**
195+
* Whether cache a resolved template file path.
196+
*/
197+
private boolean cacheEnabled = true;
198+
199+
/**
200+
* Get a prefix for adding to template file path.
201+
* <p>
202+
* Default is {@code ""}.
203+
* </p>
204+
*
205+
* @return a prefix for adding to template file path
206+
*/
207+
public String getPrefix() {
208+
return prefix;
209+
}
210+
211+
/**
212+
* Set the prefix for adding to template file path.
213+
*
214+
* @param prefix
215+
* The prefix for adding to template file path
216+
*/
217+
public void setPrefix(String prefix) {
218+
this.prefix = prefix;
219+
}
220+
221+
/**
222+
* Get whether includes package path part.
223+
* <p>
224+
* Default is {@code true}.
225+
* </p>
226+
*
227+
* @return If includes package path, return {@code true}
228+
*/
229+
public boolean isIncludesPackagePath() {
230+
return includesPackagePath;
231+
}
232+
233+
/**
234+
* Set whether includes package path part.
235+
*
236+
* @param includesPackagePath
237+
* If want to includes, set {@code true}
238+
*/
239+
public void setIncludesPackagePath(boolean includesPackagePath) {
240+
this.includesPackagePath = includesPackagePath;
241+
}
242+
243+
/**
244+
* Get whether separate directory per mapper.
245+
*
246+
* @return If separate directory per mapper, return {@code true}
247+
*/
248+
public boolean isSeparateDirectoryPerMapper() {
249+
return separateDirectoryPerMapper;
250+
}
251+
252+
/**
253+
* Set whether separate directory per mapper.
254+
* <p>
255+
* Default is {@code true}.
256+
* </p>
257+
*
258+
* @param separateDirectoryPerMapper
259+
* If want to separate directory, set {@code true}
260+
*/
261+
public void setSeparateDirectoryPerMapper(boolean separateDirectoryPerMapper) {
262+
this.separateDirectoryPerMapper = separateDirectoryPerMapper;
263+
}
264+
265+
/**
266+
* Get whether includes mapper name into file name when separate directory per mapper.
267+
* <p>
268+
* Default is {@code true}.
269+
* </p>
270+
*
271+
* @return If includes mapper name, return {@code true}
272+
*/
273+
public boolean isIncludesMapperNameWhenSeparateDirectory() {
274+
return includesMapperNameWhenSeparateDirectory;
275+
}
276+
277+
/**
278+
* Set whether includes mapper name into file name when separate directory per mapper.
279+
* <p>
280+
* Default is {@code true}.
281+
* </p>
282+
*
283+
* @param includesMapperNameWhenSeparateDirectory
284+
* If want to includes, set {@code true}
285+
*/
286+
public void setIncludesMapperNameWhenSeparateDirectory(boolean includesMapperNameWhenSeparateDirectory) {
287+
this.includesMapperNameWhenSeparateDirectory = includesMapperNameWhenSeparateDirectory;
288+
}
289+
290+
/**
291+
* Get whether cache a resolved template file path.
292+
* <p>
293+
* Default is {@code true}.
294+
* </p>
295+
*
296+
* @return If cache a resolved template file path, return {@code true}
297+
*/
298+
public boolean isCacheEnabled() {
299+
return cacheEnabled;
300+
}
301+
302+
/**
303+
* Set whether cache a resolved template file path.
304+
*
305+
* @param cacheEnabled
306+
* If want to cache, set {@code true}
307+
*/
308+
public void setCacheEnabled(boolean cacheEnabled) {
309+
this.cacheEnabled = cacheEnabled;
310+
}
311+
312+
}
313+
101314
}
102315

103316
/**

0 commit comments

Comments
 (0)