IDEA 模板代码(live template)总结

2020-04-16  本文已影响0人  醉疯觞

模板代码live template教程

官网文档链接

中文第三方博客:这个有点像是官网的翻译版本,很实用,主要是对函数的翻译。

自己使用的一些感受

  1. 模板代码无法实现像 .for 这样的效果,只能 sout 然后 Tab,或者选中代码块使用 Ctrl + Alt + J,将选中代码块当成参数传入模板中(模板中定义的 SELECTION
  2. 要想实现 .for 这样的效果,要使用另外一个方式:Postfix code completion
  3. 目前 IDEA 只内置了两个函数 SELECTIONEND

分享一些自己写的

IFA

描述

mybatis <if> 标签

效果
<if test="judgeFullName != null ">
    AND judge_full_name = #{judgeFullName}
</if>
使用

输入代码 ,使用 Ctrl + Alt + J选中,选择 IFA

模板
<if test="$SELECTION$ != null ">
   AND $VAR1$ $END$ = #{$SELECTION$}
</if>
<template name="IF" value="&lt;if test=&quot;$SELECTION$ != null &quot;&gt;&#10;   AND $VAR1$ $END$ = #{$SELECTION$}&#10;&lt;/if&gt;" description="if" toReformat="true" toShortenFQNames="true">
  <variable name="VAR1" expression="snakeCase(String)" defaultValue="$SELECTION$" alwaysStopAt="true" />
  <context>
    <option name="SQL" value="true" />
  </context>
</template>

FI

描述

mybatis <foreach> 标签

效果
<foreach item="item" collection="List" separator="," open="(" close=")" index="">
    #{item}
</foreach>
使用

输入代码,使用 Ctrl + Alt + J选中,选择 FI

模板
<foreach item="item" collection="$SELECTION$" separator="," open="(" close=")" index="">
    #{item}
</foreach>
<template name="FI" value="&lt;foreach item=&quot;item&quot; collection=&quot;$SELECTION$&quot; separator=&quot;,&quot; open=&quot;(&quot; close=&quot;)&quot; index=&quot;&quot;&gt;&#10;    #{item}&#10;&lt;/foreach&gt;" description="for earch in" toReformat="true" toShortenFQNames="true">
  <context>
    <option name="SQL" value="true" />
  </context>
</template>

LikeC

描述

MYSQL LIKE CONCAT() 语句补全

效果
LIKE CONCAT('%',#{judge_full_name} ,'%' )
使用

输入代码,使用 Ctrl + Alt + J选中,选择 LikeC

模板
LIKE CONCAT('%',#{$SELECTION$} ,'%' )
<template name="LikeC" value="LIKE CONCAT('%',#{$SELECTION$} ,'%' )" description="Like Concat" toReformat="true" toShortenFQNames="true">
  <context>
    <option name="SQL" value="true" />
  </context>
</template>

最后

最后放一个 github 地址,后面慢慢去维护模板

上一篇 下一篇

猜你喜欢

热点阅读