Skip to content

Commit 2237645

Browse files
committed
Refactoring configuration for TemplateFilePathProvider
See gh-10
1 parent 020fa19 commit 2237645

File tree

8 files changed

+301
-126
lines changed

8 files changed

+301
-126
lines changed

src/main/asciidoc/user-guide.adoc

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,13 +1030,8 @@ If does not match all, it throw an exception that indicate not found a template
10301030
* `com/example/mapper/BaseMapper/BaseMapper-{methodName}.sql` +
10311031
(fallback using declaring class of mapper method and default database)
10321032

1033-
If you want to customize the template file path format, please call static setter methods of the `TemplateFilePathProvider`.
1034-
1035-
[NOTE]
1036-
====
1037-
If you applied an user defined `ThymeleafLanguageDriverConfig` for `ThymeleafLanguageDriver`,
1038-
please apply same instance to the `TemplateFilePathProvider` using the `setLanguageDriverConfig` method.
1039-
====
1033+
If you want to customize the template file path format,
1034+
you can customize using the <<Configuration properties, configuration properties>> that start with `template-file.path-provider`.
10401035

10411036

10421037
== Cautions for usage
@@ -1129,6 +1124,28 @@ The mybatis-thymeleaf provides following properties for customizing configuratio
11291124
|`String[]`
11301125
|`"*.sql"`
11311126

1127+
4+|*Template file path provider configuration(TemplateFilePathProvider)*
1128+
1129+
|`template-file.path-provider.prefix`
1130+
|The prefix for adding to template file path
1131+
|`String`
1132+
|`""`
1133+
1134+
|`template-file.path-provider.includes-package-path`
1135+
|Whether includes package path part
1136+
|`Boolean`
1137+
|`true` (includes package path)
1138+
1139+
|`template-file.path-provider.separate-directory-per-mapper`
1140+
|Whether separate directory per mapper
1141+
|`Boolean`
1142+
|`true` (separate directory per mapper)
1143+
1144+
|`template-file.path-provider.includes-mapper-name-when-separate-directory`
1145+
|Whether includes mapper name into file name when separate directory per mapper
1146+
|`Boolean`
1147+
|`true` (includes mapper name)
1148+
11321149
4+|*Dialect configuration*
11331150

11341151
|`dialect.prefix`
@@ -1157,13 +1174,17 @@ The mybatis-thymeleaf provides following properties for customizing configuratio
11571174
[source,properties]
11581175
.src/main/resources/mybatis-thymeleaf.properties
11591176
----
1160-
use-2way = true
1177+
use-2way = false
11611178
customizer = com.example.MyTemplateEngineCustomizer
11621179
template-file.cache-enabled = true
11631180
template-file.cache-ttl = 3600000
11641181
template-file.encoding = UTF-8
1165-
template-file.base-dir = templates/sqls/
1182+
template-file.base-dir = templates/
11661183
template-file.patterns = *sql, *.sql.template
1184+
template-file.path-provider.prefix = sqls/
1185+
template-file.path-provider.includes-package-path = false
1186+
template-file.path-provider.separate-directory-per-mapper = false
1187+
template-file.path-provider.includes-mapper-name-when-separate-directory = false
11671188
dialect.prefix = mybatis
11681189
dialect.like-escape-char = ~
11691190
dialect.like-escape-clause-format = escape '%s'
@@ -1178,13 +1199,17 @@ These properties can be specified via factory method of `ThymeleafLanguageDriver
11781199
----
11791200
configuration.getLanguageRegistry().register(
11801201
new ThymeleafLanguageDriver(ThymeleafLanguageDriverConfig.newInstance(c -> {
1181-
c.setUse2way(true);
1202+
c.setUse2way(false);
11821203
c.setCustomizer(CustomTemplateEngineCustomizer.class);
11831204
c.getTemplateFile().setCacheEnabled(false);
11841205
c.getTemplateFile().setCacheTtl(3600000L);
11851206
c.getTemplateFile().setEncoding(StandardCharsets.UTF_8);
1186-
c.getTemplateFile().setBaseDir("templates/sqls/");
1207+
c.getTemplateFile().setBaseDir("templates/");
11871208
c.getTemplateFile().setPatterns("*.sql", "*.sql.template");
1209+
c.getTemplateFile().getPathProvider().setPrefix("sqls/");
1210+
c.getTemplateFile().getPathProvider().setIncludesPackagePath(false);
1211+
c.getTemplateFile().getPathProvider().setSeparateDirectoryPerMapper(false);
1212+
c.getTemplateFile().getPathProvider().setIncludesMapperNameWhenSeparateDirectory(false);
11881213
c.getDialect().setPrefix("mybatis");
11891214
c.getDialect().setLikeEscapeChar('~');
11901215
c.getDialect().setLikeEscapeClauseFormat("escape '%s'");

src/main/java/org/mybatis/scripting/thymeleaf/ThymeleafLanguageDriver.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.apache.ibatis.scripting.defaults.DefaultParameterHandler;
3030
import org.apache.ibatis.session.Configuration;
3131
import org.mybatis.scripting.thymeleaf.expression.Likes;
32+
import org.mybatis.scripting.thymeleaf.support.TemplateFilePathProvider;
3233
import org.thymeleaf.ITemplateEngine;
3334
import org.thymeleaf.TemplateEngine;
3435
import org.thymeleaf.templatemode.TemplateMode;
@@ -60,6 +61,7 @@ public ThymeleafLanguageDriver() {
6061
*/
6162
public ThymeleafLanguageDriver(ThymeleafLanguageDriverConfig config) {
6263
this.templateEngine = createDefaultTemplateEngine(config);
64+
TemplateFilePathProvider.setLanguageDriverConfig(config);
6365
}
6466

6567
/**

src/main/java/org/mybatis/scripting/thymeleaf/ThymeleafLanguageDriverConfig.java

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,11 @@ public static class TemplateFileConfig {
179179
*/
180180
private Long cacheTtl;
181181

182+
/**
183+
* The template file path provider configuration.
184+
*/
185+
private final PathProviderConfig pathProvider = new PathProviderConfig();
186+
182187
/**
183188
* Get the character encoding for reading template resource file.
184189
* <p>
@@ -289,6 +294,136 @@ public void setCacheTtl(Long cacheTtl) {
289294
this.cacheTtl = cacheTtl;
290295
}
291296

297+
/**
298+
* Get the template file path provider configuration.
299+
*
300+
* @return the template file path provider configuration
301+
* @since 1.0.1
302+
*/
303+
public PathProviderConfig getPathProvider() {
304+
return pathProvider;
305+
}
306+
307+
/**
308+
* The template file path provider configuration.
309+
*
310+
* @since 1.0.1
311+
*/
312+
public static class PathProviderConfig {
313+
314+
/**
315+
* The prefix for adding to template file path.
316+
*/
317+
private String prefix = "";
318+
319+
/**
320+
* Whether includes package path part.
321+
*/
322+
private boolean includesPackagePath = true;
323+
324+
/**
325+
* Whether separate directory per mapper.
326+
*/
327+
private boolean separateDirectoryPerMapper = true;
328+
329+
/**
330+
* Whether includes mapper name into file name when separate directory per mapper.
331+
*/
332+
private boolean includesMapperNameWhenSeparateDirectory = true;
333+
334+
/**
335+
* Get a prefix for adding to template file path.
336+
* <p>
337+
* Default is {@code ""}.
338+
* </p>
339+
*
340+
* @return a prefix for adding to template file path
341+
*/
342+
public String getPrefix() {
343+
return prefix;
344+
}
345+
346+
/**
347+
* Set the prefix for adding to template file path.
348+
*
349+
* @param prefix
350+
* The prefix for adding to template file path
351+
*/
352+
public void setPrefix(String prefix) {
353+
this.prefix = prefix;
354+
}
355+
356+
/**
357+
* Get whether includes package path part.
358+
* <p>
359+
* Default is {@code true}.
360+
* </p>
361+
*
362+
* @return If includes package path, return {@code true}
363+
*/
364+
public boolean isIncludesPackagePath() {
365+
return includesPackagePath;
366+
}
367+
368+
/**
369+
* Set whether includes package path part.
370+
*
371+
* @param includesPackagePath
372+
* If want to includes, set {@code true}
373+
*/
374+
public void setIncludesPackagePath(boolean includesPackagePath) {
375+
this.includesPackagePath = includesPackagePath;
376+
}
377+
378+
/**
379+
* Get whether separate directory per mapper.
380+
*
381+
* @return If separate directory per mapper, return {@code true}
382+
*/
383+
public boolean isSeparateDirectoryPerMapper() {
384+
return separateDirectoryPerMapper;
385+
}
386+
387+
/**
388+
* Set whether separate directory per mapper.
389+
* <p>
390+
* Default is {@code true}.
391+
* </p>
392+
*
393+
* @param separateDirectoryPerMapper
394+
* If want to separate directory, set {@code true}
395+
*/
396+
public void setSeparateDirectoryPerMapper(boolean separateDirectoryPerMapper) {
397+
this.separateDirectoryPerMapper = separateDirectoryPerMapper;
398+
}
399+
400+
/**
401+
* Get whether includes mapper name into file name when separate directory per mapper.
402+
* <p>
403+
* Default is {@code true}.
404+
* </p>
405+
*
406+
* @return If includes mapper name, set {@code true}
407+
*/
408+
public boolean isIncludesMapperNameWhenSeparateDirectory() {
409+
return includesMapperNameWhenSeparateDirectory;
410+
}
411+
412+
/**
413+
* Set whether includes mapper name into file name when separate directory per mapper.
414+
* <p>
415+
* Default is {@code true}.
416+
* </p>
417+
*
418+
* @param includesMapperNameWhenSeparateDirectory
419+
* If want to includes, set {@code true}
420+
*/
421+
public void setIncludesMapperNameWhenSeparateDirectory(boolean includesMapperNameWhenSeparateDirectory) {
422+
this.includesMapperNameWhenSeparateDirectory = includesMapperNameWhenSeparateDirectory;
423+
}
424+
425+
}
426+
292427
}
293428

294429
/**
@@ -465,6 +600,33 @@ public void setLikeAdditionalEscapeTargetChars(Character... likeAdditionalEscape
465600
* <td>{@code "*.sql"}</td>
466601
* </tr>
467602
* <tr>
603+
* <tr>
604+
* <th colspan="3">Template file path provider configuration(TemplateFilePathProvider)</th>
605+
* </tr>
606+
* <tr>
607+
* <td>template-file.path-provider.prefix</td>
608+
* <td>The prefix for adding to template file path</td>
609+
* <td>{@code ""}</td>
610+
* </tr>
611+
* <tr>
612+
* <tr>
613+
* <td>template-file.path-provider.includes-package-path</td>
614+
* <td>Whether includes package path part</td>
615+
* <td>{@code true}</td>
616+
* </tr>
617+
* <tr>
618+
* <tr>
619+
* <td>template-file.patterns</td>
620+
* <td>Whether separate directory per mapper</td>
621+
* <td>{@code true}</td>
622+
* </tr>
623+
* <tr>
624+
* <tr>
625+
* <td>template-file.patterns</td>
626+
* <td>Whether includes mapper name into file name when separate directory per mapper</td>
627+
* <td>{@code true}</td>
628+
* </tr>
629+
* <tr>
468630
* <th colspan="3">Dialect configuration</th>
469631
* </tr>
470632
* <tr>

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy