Skip to content

Code completion

Kazuki Shimizu edited this page Mar 15, 2019 · 3 revisions

In this page, we explain about code completion. The mybatis-thymeleaf does not provide a plugin for support writing a 2-way SQL because it can be replaced by a code completion feature provided by IDE.

We introduce settings example for following three IDEs.

And, we introduce following 10 templates.

Table 1. template example list
Template name Description

Thymeleaf standard dialect

th_if

Render 'th:if' attribute tag for mybatis-thymeleaf 2-way SQL

Example result
/*[# th:if=""]*/

/*[/]*/

th_unless

Render 'th:unless' attribute tag for mybatis-thymeleaf 2-way SQL

Example result
/*[# th:unless=""]*/

/*[/]*/

th_switch

Render 'th:switch'/'th:case' attribute tag for mybatis-thymeleaf 2-way SQL

Example result
/*[# th:switch=""]*/
  /*[# th:case=""]*/

  /*[/]*/
  /*[# th:case="*"]*/

  /*[/]*/
/*[/]*/

th_each

Render 'th:each' attribute tag for mybatis-thymeleaf 2-way SQL

Example result
/*[# th:each=" : ${}"]*/

/*[/]*/

th_each_comma_if_not_last

Render ',(comma)' when iteration object is not last element for mybatis-thymeleaf 2-way SQL

Example result
/*[(${Stat.last} ? '' : ',')]*/

th_insert

Render 'th:insert' attribute tag for mybatis-thymeleaf 2-way SQL

Example result
/*[# th:insert="~{}" /]*/

MyBatis custom dialect

mb_p

Render 'mb:p' attribute tag for mybatis-thymeleaf 2-way SQL

Example result
/*[# mb:p=""]*/  /*[/]*/

mb_bind

Render 'mb:bind' attribute tag for mybatis-thymeleaf 2-way SQL

Example result
/*[# mb:bind="=" /]*/

mb_like_esc_clause

Render '#likes.escapeClause()' for mybatis-thymeleaf 2-way SQL

Example result
/*[(${#likes.escapeClause()})]*/

mb_like_esc_wildcard

Render '#likes.escapeWildcard()' for mybatis-thymeleaf 2-way SQL

Example result
#likes.escapeWildcard()

IntelliJ IDEA

You can use the "Live template" feature.

<templateSet group="SQL-mybatis-thymeleaf">
  <template name="th_if" value="/*[# th:if=&quot;$condition$&quot;]*/&#10;  $conditionalStatement$&#10;/*[/]*/" description="Render 'th:if' attribute tag for mybatis-thymeleaf 2-way SQL" toReformat="false" toShortenFQNames="true">
    <variable name="condition" expression="" defaultValue="" alwaysStopAt="true" />
    <variable name="conditionalStatement" expression="" defaultValue="" alwaysStopAt="true" />
    <context>
      <option name="SQL" value="true" />
    </context>
  </template>
  <template name="th_unless" value="/*[# th:unless=&quot;$condition$&quot;]*/&#10;  $conditionalStatement$&#10;/*[/]*/" description="Render 'th:unless' attribute tag for mybatis-thymeleaf 2-way SQL" toReformat="false" toShortenFQNames="true">
    <variable name="condition" expression="" defaultValue="" alwaysStopAt="true" />
    <variable name="conditionalStatement" expression="" defaultValue="" alwaysStopAt="true" />
    <context>
      <option name="SQL" value="true" />
    </context>
  </template>
  <template name="th_switch" value="/*[# th:switch=&quot;$target$&quot;]*/&#10;  /*[# th:case=&quot;$expectedValue$&quot;]*/&#10;    $conditionalStatement1$&#10;  /*[/]*/&#10;  /*[# th:case=&quot;*&quot;]*/&#10;    $conditionalStatement2$&#10;  /*[/]*/&#10;/*[/]*/" description="Render 'th:switch'/'th:case' attribute tag for mybatis-thymeleaf 2-way SQL" toReformat="false" toShortenFQNames="true">
    <variable name="target" expression="" defaultValue="" alwaysStopAt="true" />
    <variable name="expectedValue" expression="" defaultValue="" alwaysStopAt="true" />
    <variable name="conditionalStatement1" expression="" defaultValue="" alwaysStopAt="true" />
    <variable name="conditionalStatement2" expression="" defaultValue="" alwaysStopAt="true" />
    <context>
      <option name="SQL" value="true" />
    </context>
  </template>
  <template name="th_each" value="/*[# th:each=&quot;$varName$ : ${$collection$}&quot;]*/&#10;  $repeatableStatement$&#10;/*[/]*/" description="Render 'th:each' attribute tag for mybatis-thymeleaf 2-way SQL" toReformat="false" toShortenFQNames="true">
    <variable name="varName" expression="" defaultValue="" alwaysStopAt="true" />
    <variable name="collection" expression="" defaultValue="" alwaysStopAt="true" />
    <variable name="repeatableStatement" expression="" defaultValue="" alwaysStopAt="true" />
    <context>
      <option name="SQL" value="true" />
    </context>
  </template>
  <template name="th_each_comma_if_not_last" value="/*[(${$varName$Stat.last} ? '' : ',')]*/" description="Render ',(comma)' when iteration object is not last element for mybatis-thymeleaf 2-way SQL" toReformat="true" toShortenFQNames="true">
    <variable name="varName" expression="" defaultValue="" alwaysStopAt="true" />
    <context>
      <option name="SQL" value="true" />
    </context>
  </template>
  <template name="th_insert" value="/*[# th:insert=&quot;~{$resourcePath$}&quot; /]*/" description="Render 'th:insert' attribute tag for mybatis-thymeleaf 2-way SQL" toReformat="true" toShortenFQNames="true">
    <variable name="resourcePath" expression="" defaultValue="" alwaysStopAt="true" />
    <context>
      <option name="SQL" value="true" />
    </context>
  </template>
  <template name="mb_p" value="/*[# mb:p=&quot;$name$&quot;]*/ $mockValue$ /*[/]*/" description="Render 'mb:p' attribute tag for mybatis-thymeleaf 2-way SQL" toReformat="false" toShortenFQNames="true">
    <variable name="name" expression="" defaultValue="" alwaysStopAt="true" />
    <variable name="mockValue" expression="" defaultValue="" alwaysStopAt="true" />
    <context>
      <option name="SQL" value="true" />
    </context>
  </template>
  <template name="mb_bind" value="/*[# mb:bind=&quot;$name$=$value$&quot; /]*/" description="Render 'mb:bind' attribute tag for mybatis-thymeleaf 2-way SQL" toReformat="false" toShortenFQNames="true">
    <variable name="name" expression="" defaultValue="" alwaysStopAt="true" />
    <variable name="value" expression="" defaultValue="" alwaysStopAt="true" />
    <context>
      <option name="SQL" value="true" />
    </context>
  </template>
  <template name="mb_like_esc_clause" value="/*[(${#likes.escapeClause()})]*/" description="Render '#likes.escapeClause()' for mybatis-thymeleaf 2-way SQL" toReformat="false" toShortenFQNames="true">
    <context>
      <option name="SQL" value="true" />
    </context>
  </template>
  <template name="mb_like_esc_wildcard" value="#likes.escapeWildcard($target$)" description="Render '#likes.escapeWildcard()' for mybatis-thymeleaf 2-way SQL" toReformat="false" toShortenFQNames="true">
    <variable name="target" expression="" defaultValue="" alwaysStopAt="true" />
    <context>
      <option name="SQL" value="true" />
    </context>
  </template>
</templateSet>

Eclipse

You can use the "Templates" feature.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<templates>
  <template autoinsert="true" context="org.eclipse.datatools.sqltools.editor.template.sql.generic" deleted="false" description="Render 'th:if' attribute tag for mybatis-thymeleaf 2-way SQL" enabled="true" name="th_if">/*[# th:if="${condition}"]*/
  ${conditionalStatement}
/*[/]*/</template>
  <template autoinsert="true" context="org.eclipse.datatools.sqltools.editor.template.sql.generic" deleted="false" description="Render 'th:unless' attribute tag for mybatis-thymeleaf 2-way SQL" enabled="true" name="th_unless">/*[# th:unless="${condition}"]*/
  ${conditionalStatement}
/*[/]*/</template>
  <template autoinsert="true" context="org.eclipse.datatools.sqltools.editor.template.sql.generic" deleted="false" description="Render 'th:switch'/'th:case' attribute tag for mybatis-thymeleaf 2-way SQL" enabled="true" name="th_switch">/*[# th:switch="${target}"]*/
  /*[# th:case="${expectedValue}"]*/
    ${conditionalStatement}
  /*[/]*/
  /*[# th:case="*"]*/
    ${conditionalStatement}
  /*[/]*/
/*[/]*/</template>
  <template autoinsert="true" context="org.eclipse.datatools.sqltools.editor.template.sql.generic" deleted="false" description="Render 'th:each' attribute tag for mybatis-thymeleaf 2-way SQL" enabled="true" name="th_each">/*[# th:each="${varName} : $${${collection}}"]*/
  ${repeatableStatement}
/*[/]*/</template>
  <template autoinsert="true" context="org.eclipse.datatools.sqltools.editor.template.sql.generic" deleted="false" description="Render ',(comma)' when iteration object is not last element for mybatis-thymeleaf 2-way SQL" enabled="true" name="th_each_comma_if_not_last">/*[($${${varName}Stat.last} ? '' : ',')]*/</template>
  <template autoinsert="true" context="org.eclipse.datatools.sqltools.editor.template.sql.generic" deleted="false" description="Render 'th:insert' attribute tag for mybatis-thymeleaf 2-way SQL" enabled="true" name="th_insert">/*[# th:insert="~{${resourcePath}}" /]*/</template>
  <template autoinsert="true" context="org.eclipse.datatools.sqltools.editor.template.sql.generic" deleted="false" description="Render 'mb:p' attribute tag for mybatis-thymeleaf 2-way SQL" enabled="true" name="mb_p">/*[# mb:p="${name}"]*/ ${mockValue} /*[/]*/</template>
  <template autoinsert="true" context="org.eclipse.datatools.sqltools.editor.template.sql.generic" deleted="false" description="Render 'mb:bind' attribute tag for mybatis-thymeleaf 2-way SQL" enabled="false" name="mb_bind">/*[# mb:bind="${name}=${value}" /]*/</template>
  <template autoinsert="true" context="org.eclipse.datatools.sqltools.editor.template.sql.generic" deleted="false" description="Render '#likes.escapeClause()' for mybatis-thymeleaf 2-way SQL" enabled="true" name="mb_like_esc_clause">/*[($${#likes.escapeClause()})]*/</template>
  <template autoinsert="true" context="org.eclipse.datatools.sqltools.editor.template.sql.generic" deleted="false" description="Render '#likes.escapeWildcard()' for mybatis-thymeleaf 2-way SQL" enabled="true" name="mb_like_esc_wildcard">#likes.escapeWildcard(${target})</template>
</templates>

Visual Studio Code

You can use the "User defined snippets" feature.

{
	"th_if": {
		"prefix": "th_if",
		"body": [
			"/*[# th:if=\"${1:condition}\"]*/",
			"  ${2:conditionalStatement}",
			"/*[/]*/"
		],
		"description": "Render 'th:if' attribute tag for mybatis-thymeleaf 2-way SQL"
	},
	"th_unless": {
		"prefix": "th_unless",
		"body": [
			"/*[# th:unless=\"${1:condition}\"]*/",
			"  ${2:conditionalStatement}",
			"/*[/]*/"
		],
		"description": "Render 'th:unless' attribute tag for mybatis-thymeleaf 2-way SQL"
	},
	"th_switch": {
		"prefix": "th_switch",
		"body": [
			"/*[# th:switch=\"${1:target}\"]*/",
			"  /*[# th:case=\"${2:expectedValue}\"]*/",
			"    ${3:conditionalStatement}",
			"  /*[/]*/",
			"  /*[# th:case=\"*\"]*/",
			"    ${4:conditionalStatement}",
			"  /*[/]*/",
			"/*[/]*/"
		],
		"description": "Render 'th:switch'/'yh:case' attribute tag for mybatis-thymeleaf 2-way SQL"
	},
	"th_each": {
		"prefix": "th_each",
		"body": [
			"/*[# th:each=\"varName : ${${1:collection}}\"]*/",
			"  ${2:conditionalStatement}",
			"/*[/]*/"
		],
		"description": "Render 'th:each' attribute tag for mybatis-thymeleaf 2-way SQL"
	},
	"th_each_comma_if_not_last": {
		"prefix": "th_each_comma_if_not_last",
		"body": [
			"/*[(${${1:varName}Stat.last} ? '' : ',')]*/"
		],
		"description": "Render ',(comma)' when iteration object is not last element for mybatis-thymeleaf 2-way SQL"
	},
	"th_insert": {
		"prefix": "th_insert",
		"body": [
			"/*[# th:insert=\"~{${1:resourcePath}}\" /]*/"
		],
		"description": "Render 'th:insert' attribute tag for mybatis-thymeleaf 2-way SQL"
	},
	"mb_p": {
		"prefix": "mb_p",
		"body": [
			"/*[# mb:p=\"${1:name}\"]*/ ${2:mockValue} /*[/]*/"
		],
		"description": "Render 'mb:p' attribute tag for mybatis-thymeleaf 2-way SQL"
	},
	"mb_bind": {
		"prefix": "mb_bind",
		"body": [
			"/*[# mb:bind=\"${1:name}=${2:value}\" /]*/"
		],
		"description": "Render 'mb:bind' attribute tag for mybatis-thymeleaf 2-way SQL"
	},
	"mb_like_esc_clause": {
		"prefix": "mb_like_esc_clause",
		"body": [
			"/*[(${#likes.escapeClause()})]*/"
		],
		"description": "Render '#likes.escapeClause()' for mybatis-thymeleaf 2-way SQL"
	},
	"mb_like_esc_wildcard": {
		"prefix": "mb_like_esc_wildcard",
		"body": [
			"#likes.escapeWildcard(${target})"
		],
		"description": "Render '#likes.escapeWildcard()' for mybatis-thymeleaf 2-way SQL"
	}
}
Clone this wiki locally