what

EasyCode是基于IntelliJ IDEA Ultimate版开发的一个代码生成插件,主要通过自定义模板(基于velocity)来生成各种你想要的代码。

why

项目中添加新的数据表时,可以直接生成基于自定义模板的基础类,节省基础编码开发时间,提高效率。

how

1、安装插件

Settings -> Plugins -> easy code(install -> restart Idea)

2、导入

Settings -> Other Settings -> Easy Code
导入模板 -> token:8cf91ffdd5fee2abc01198cc44b60d7b(6小时有效)

3、生成

选择要生成的表,右键

image

选择包、路径(支持模块)及要生成的代码模板

image

生成的代码模板样例(表名:apply_asset)

image

image

4、代码

## -------- model start --------
##引入宏定义
$!define

##使用宏定义设置回调(保存位置与文件后缀)
#save("/model", ".java")

##使用宏定义设置包后缀
#setPackageSuffix("model")
#set($tableName = $tableInfo.name)
$!callback.setFileName($tool.append($tableName, ".java"))

##使用全局变量实现默认包导入
$!autoImport
import javax.persistence.*;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.io.Serializable;
import lombok.Data;
import org.hibernate.annotations.*;

##使用宏定义实现类注释信息
/**
 * @author $!author
 * @description $!{tableInfo.comment}($!{tableInfo.name})实体类
 * @date $!time.currTime()
 * @since 1.0
 */
@Entity
@Table(name = $tool.append('"', $tableInfo.obj.name, '"'))
@Data
@DynamicInsert
@DynamicUpdate
public class $!{tableName} implements Serializable {
// TODO private static final long serialVersionUID = -1L;
#foreach($column in $tableInfo.fullColumn)

#if(${column.comment})
    /**
    * ${column.comment}
    */
#end
#if($!{column.obj.name} == 'id')
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
#elseif($!{column.obj.name} == 'created_time')
    @CreationTimestamp
#elseif($!{column.obj.name} == 'modified_time')
    @UpdateTimestamp
#end
    @Column(name = $tool.append('"', $column.obj.name ,'"'))
    private $!{tool.getClsNameByFullName($column.type)} $!{column.name};
#end
}
## -------- model end --------

## -------- dao start --------
##设置回调
$!callback.setFileName($tool.append($tableInfo.name, "Dao.java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/dao"))

#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}#{end}.dao;

import com.linkmed.core.dao.BaseDao;

/**
 * @author $!author
 * @description $!{tableInfo.comment}($tableInfo.obj.name)}数据操作接口层
 * @date $!time.currTime()
 * @since 1.0
 */
public interface ${tableInfo.name}Dao extends BaseDao {

}
## -------- dao end --------

## -------- dao impl start --------
##设置回调
$!callback.setFileName($tool.append($tableInfo.name, "DaoImpl.java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/dao/impl"))

package $!{tableInfo.savePackageName}.dao.impl;

import com.linkmed.core.dao.hibernate.BaseDaoHibernate;
import $!{tableInfo.savePackageName}.dao.${tableInfo.name}Dao;
import org.springframework.stereotype.Component;

/**
 * @author $!author
 * @description $!{tableInfo.comment}($tableInfo.obj.name)数据操作层
 * @date $!time.currTime()
 * @since 1.0
 */
@Component
public class ${tableInfo.name}DaoImpl extends BaseDaoHibernate implements ${tableInfo.name}Dao {

}
## -------- dao impl end --------

## -------- service start --------
##设置回调
$!callback.setFileName($tool.append($tableInfo.name, "Service.java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/service"))

package $!{tableInfo.savePackageName}.service;

/**
 * @author $!author
 * @description $!{tableInfo.comment}($tableInfo.obj.name)业务操作接口层
 * @date $!time.currTime()
 * @since 1.0
 */
public interface ${tableInfo.name}Service {

}
## -------- service end --------

## -------- service impl start --------
##设置回调
$!callback.setFileName($tool.append($tableInfo.name, "ServiceImpl.java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/service/impl"))

package $!{tableInfo.savePackageName}.service.impl;

import $!{tableInfo.savePackageName}.dao.${tableInfo.name}Dao;
import $!{tableInfo.savePackageName}.service.${tableInfo.name}Service;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author $!author
 * @description $!{tableInfo.comment}($tableInfo.obj.name)业务操作接口层
 * @date $!time.currTime()
 * @since 1.0
 */
@Service
public class ${tableInfo.name}ServiceImpl implements ${tableInfo.name}Service {
    
    @Autowired
    private ${tableInfo.name}Dao $tool.firstLowerCase(${tableInfo.name})Dao;
    
}
## -------- service impl end --------

other

学习文档:https://gitee.com/makejava/EasyCode/wikis/pages

在线安装教学:https://raw.githubusercontent.com/makejavas/EasyCode/master/%E6%95%99%E7%A8%8B%E5%9B%BE%E7%89%87/1-1/%E5%9C%A8%E7%BA%BF%E5%AE%89%E8%A3%85.gif

使用debug查看一些对象方法:
Settings -> EasyCode -> Template Setting
调试

end