IDEA技巧之模板

版权声明:此文章为作者原创,转载需要经过作者同意! https://blog.csdn.net/JavaDad/article/details/82495198

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;
    }

}

猜你喜欢

转载自blog.csdn.net/JavaDad/article/details/82495198
今日推荐