Skip to content

Refactoring configuration for TemplateFilePathProvider #20

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

Merged
merged 1 commit into from
Apr 14, 2019
Merged
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
47 changes: 36 additions & 11 deletions src/main/asciidoc/user-guide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1030,13 +1030,8 @@ If does not match all, it throw an exception that indicate not found a template
* `com/example/mapper/BaseMapper/BaseMapper-{methodName}.sql` +
(fallback using declaring class of mapper method and default database)

If you want to customize the template file path format, please call static setter methods of the `TemplateFilePathProvider`.

[NOTE]
====
If you applied an user defined `ThymeleafLanguageDriverConfig` for `ThymeleafLanguageDriver`,
please apply same instance to the `TemplateFilePathProvider` using the `setLanguageDriverConfig` method.
====
If you want to customize the template file path format,
you can customize using the <<Configuration properties, configuration properties>> that start with `template-file.path-provider`.


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

4+|*Template file path provider configuration(TemplateFilePathProvider)*

|`template-file.path-provider.prefix`
|The prefix for adding to template file path
|`String`
|`""`

|`template-file.path-provider.includes-package-path`
|Whether includes package path part
|`Boolean`
|`true` (includes package path)

|`template-file.path-provider.separate-directory-per-mapper`
|Whether separate directory per mapper
|`Boolean`
|`true` (separate directory per mapper)

|`template-file.path-provider.includes-mapper-name-when-separate-directory`
|Whether includes mapper name into file name when separate directory per mapper
|`Boolean`
|`true` (includes mapper name)

4+|*Dialect configuration*

|`dialect.prefix`
Expand Down Expand Up @@ -1157,13 +1174,17 @@ The mybatis-thymeleaf provides following properties for customizing configuratio
[source,properties]
.src/main/resources/mybatis-thymeleaf.properties
----
use-2way = true
use-2way = false
customizer = com.example.MyTemplateEngineCustomizer
template-file.cache-enabled = true
template-file.cache-ttl = 3600000
template-file.encoding = UTF-8
template-file.base-dir = templates/sqls/
template-file.base-dir = templates/
template-file.patterns = *sql, *.sql.template
template-file.path-provider.prefix = sqls/
template-file.path-provider.includes-package-path = false
template-file.path-provider.separate-directory-per-mapper = false
template-file.path-provider.includes-mapper-name-when-separate-directory = false
dialect.prefix = mybatis
dialect.like-escape-char = ~
dialect.like-escape-clause-format = escape '%s'
Expand All @@ -1178,13 +1199,17 @@ These properties can be specified via factory method of `ThymeleafLanguageDriver
----
configuration.getLanguageRegistry().register(
new ThymeleafLanguageDriver(ThymeleafLanguageDriverConfig.newInstance(c -> {
c.setUse2way(true);
c.setUse2way(false);
c.setCustomizer(CustomTemplateEngineCustomizer.class);
c.getTemplateFile().setCacheEnabled(false);
c.getTemplateFile().setCacheTtl(3600000L);
c.getTemplateFile().setEncoding(StandardCharsets.UTF_8);
c.getTemplateFile().setBaseDir("templates/sqls/");
c.getTemplateFile().setBaseDir("templates/");
c.getTemplateFile().setPatterns("*.sql", "*.sql.template");
c.getTemplateFile().getPathProvider().setPrefix("sqls/");
c.getTemplateFile().getPathProvider().setIncludesPackagePath(false);
c.getTemplateFile().getPathProvider().setSeparateDirectoryPerMapper(false);
c.getTemplateFile().getPathProvider().setIncludesMapperNameWhenSeparateDirectory(false);
c.getDialect().setPrefix("mybatis");
c.getDialect().setLikeEscapeChar('~');
c.getDialect().setLikeEscapeClauseFormat("escape '%s'");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.ibatis.scripting.defaults.DefaultParameterHandler;
import org.apache.ibatis.session.Configuration;
import org.mybatis.scripting.thymeleaf.expression.Likes;
import org.mybatis.scripting.thymeleaf.support.TemplateFilePathProvider;
import org.thymeleaf.ITemplateEngine;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.templatemode.TemplateMode;
Expand Down Expand Up @@ -60,6 +61,7 @@ public ThymeleafLanguageDriver() {
*/
public ThymeleafLanguageDriver(ThymeleafLanguageDriverConfig config) {
this.templateEngine = createDefaultTemplateEngine(config);
TemplateFilePathProvider.setLanguageDriverConfig(config);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ public static class TemplateFileConfig {
*/
private Long cacheTtl;

/**
* The template file path provider configuration.
*/
private final PathProviderConfig pathProvider = new PathProviderConfig();

/**
* Get the character encoding for reading template resource file.
* <p>
Expand Down Expand Up @@ -289,6 +294,136 @@ public void setCacheTtl(Long cacheTtl) {
this.cacheTtl = cacheTtl;
}

/**
* Get the template file path provider configuration.
*
* @return the template file path provider configuration
* @since 1.0.1
*/
public PathProviderConfig getPathProvider() {
return pathProvider;
}

/**
* The template file path provider configuration.
*
* @since 1.0.1
*/
public static class PathProviderConfig {

/**
* The prefix for adding to template file path.
*/
private String prefix = "";

/**
* Whether includes package path part.
*/
private boolean includesPackagePath = true;

/**
* Whether separate directory per mapper.
*/
private boolean separateDirectoryPerMapper = true;

/**
* Whether includes mapper name into file name when separate directory per mapper.
*/
private boolean includesMapperNameWhenSeparateDirectory = true;

/**
* Get a prefix for adding to template file path.
* <p>
* Default is {@code ""}.
* </p>
*
* @return a prefix for adding to template file path
*/
public String getPrefix() {
return prefix;
}

/**
* Set the prefix for adding to template file path.
*
* @param prefix
* The prefix for adding to template file path
*/
public void setPrefix(String prefix) {
this.prefix = prefix;
}

/**
* Get whether includes package path part.
* <p>
* Default is {@code true}.
* </p>
*
* @return If includes package path, return {@code true}
*/
public boolean isIncludesPackagePath() {
return includesPackagePath;
}

/**
* Set whether includes package path part.
*
* @param includesPackagePath
* If want to includes, set {@code true}
*/
public void setIncludesPackagePath(boolean includesPackagePath) {
this.includesPackagePath = includesPackagePath;
}

/**
* Get whether separate directory per mapper.
*
* @return If separate directory per mapper, return {@code true}
*/
public boolean isSeparateDirectoryPerMapper() {
return separateDirectoryPerMapper;
}

/**
* Set whether separate directory per mapper.
* <p>
* Default is {@code true}.
* </p>
*
* @param separateDirectoryPerMapper
* If want to separate directory, set {@code true}
*/
public void setSeparateDirectoryPerMapper(boolean separateDirectoryPerMapper) {
this.separateDirectoryPerMapper = separateDirectoryPerMapper;
}

/**
* Get whether includes mapper name into file name when separate directory per mapper.
* <p>
* Default is {@code true}.
* </p>
*
* @return If includes mapper name, set {@code true}
*/
public boolean isIncludesMapperNameWhenSeparateDirectory() {
return includesMapperNameWhenSeparateDirectory;
}

/**
* Set whether includes mapper name into file name when separate directory per mapper.
* <p>
* Default is {@code true}.
* </p>
*
* @param includesMapperNameWhenSeparateDirectory
* If want to includes, set {@code true}
*/
public void setIncludesMapperNameWhenSeparateDirectory(boolean includesMapperNameWhenSeparateDirectory) {
this.includesMapperNameWhenSeparateDirectory = includesMapperNameWhenSeparateDirectory;
}

}

}

/**
Expand Down Expand Up @@ -465,6 +600,33 @@ public void setLikeAdditionalEscapeTargetChars(Character... likeAdditionalEscape
* <td>{@code "*.sql"}</td>
* </tr>
* <tr>
* <tr>
* <th colspan="3">Template file path provider configuration(TemplateFilePathProvider)</th>
* </tr>
* <tr>
* <td>template-file.path-provider.prefix</td>
* <td>The prefix for adding to template file path</td>
* <td>{@code ""}</td>
* </tr>
* <tr>
* <tr>
* <td>template-file.path-provider.includes-package-path</td>
* <td>Whether includes package path part</td>
* <td>{@code true}</td>
* </tr>
* <tr>
* <tr>
* <td>template-file.patterns</td>
* <td>Whether separate directory per mapper</td>
* <td>{@code true}</td>
* </tr>
* <tr>
* <tr>
* <td>template-file.patterns</td>
* <td>Whether includes mapper name into file name when separate directory per mapper</td>
* <td>{@code true}</td>
* </tr>
* <tr>
* <th colspan="3">Dialect configuration</th>
* </tr>
* <tr>
Expand Down
Loading