1. 自定義配置文件的模板
在IDEA的使用過程中,爲了創建許多框架的配置文件,由於這些配置文件的頭各不相同,所以每次都要查找,造成很多不便。通過IDEA的File and Code Templates 可以定義和管理這些通用的配置文件模板,如下演示mybatis的配置文件模板的自定義過程:
2. 配置註釋模板
配置註釋時使用的是 Velocity Template Language(VTL)語法,使後臺和展示分離,動態展示。
自定義變量:#set ( $AUTHOR = "xxx"),常用的如下所示
- ${PACKAGE_NAME} : name of the package in which the new class is created
- ${PROJECT_NAME} : the name of the current project
- ${NAME} : name of the new class specified by you in the Create New Class dialog
- ${USER} : current user system login name
- ${DATE} : current system date
- ${TIME} : current system time
- ${YEAR} : current year
- ${MONTH} : current month
- ${MONTH_NAME_SHORT} : first 3 letters of the current month name. Example: Jan, Feb, etc.
- ${MONTH_NAME_FULL} : full name of the current month. Example: January, February, etc.
- ${DAY} : current day of the month
- ${HOUR} : current hour
- ${MINUTE} : current minute
1)類註釋
/**
* @ClassName ${NAME}
* @Description TODO
* @Author ${USER}
* @Date ${DATE} ${TIME}
* @Version 1.0
*/
爲什麼要這樣設置呢?因爲 Class、Interface、Enum等的模板中都有這樣一句話 #parse("File Header.java"),表示引入File Header.java這個文件,所以這樣設置可以使引用該文件的模板都起作用。
2)方法註釋
*
* @description TODO
* @author $user$
* @date $date$ $time$
$param$
* @return $return$
* @throws $throws$
*/
有幾點需要重點說明:
- 快捷鍵的設置,最初設置的是 /** ,結果導致的是在方法外無法獲取 param 和 return 。參照別人的說明,最終設置爲 *,這樣在方法外輸入 /** + enter 就可以出來效果了,而且模板第一個 * 的位置是調整好的,否則格式會變亂。
- $param$ 這個參數是比較特殊的,效果是每個參數都有一個@param,在設置Edit variables 的時候加入以下代碼即可實現
groovyScript("def result=''; def params=\"${_1}\".replaceAll('[\\\\[|\\\\]|\\\\s]', '').split(',').toList(); for(i = 0; i < params.size(); i++) {result+=' * @param ' + params[i] + ((i < params.size() - 1) ? '\\n' : '')}; return result", methodParameters())
-
@throws $throws$ 沒法實現,如有知道的大神可以指導下!
最終效果展示
/**
* @ClassName Color
* @Description 創建類時自動生成的
* @Author Danglt
* @Date 2018/9/7 16:16
* @Version 1.0
*/
public class Color {
/**
* @description 在方法上輸入 /** ,按enter會生成
* @author Danglt
* @date 2018/9/7 16:16
* @param a
* @param b
* @return int
* @throws 異常無法獲取
*/
public int add(int a, int b) throws NumberFormatException {
return a + b;
}
}