Idea插件EasyCode使用

easyCode时idea的一款插件,可以通过数据库表快速生成代码!

模板语法

0、说明文档

属性
    $author 设置中的作者 java.lang.String
    $modulePath 选中的module路径 java.lang.String
    $projectPath 项目绝对路径 java.lang.String
对象
$tableInfo 表对象
    obj 表原始对象 com.intellij.database.model.DasTable
    name 表名(转换后的首字母大写)java.lang.String
    comment 表注释 java.lang.String
    fullColumn 所有列 java.util.List<ColumnInfo>
    pkColumn 主键列 java.util.List<ColumnInfo>
    otherColumn 其他列 java.util.List<ColumnInfo>,除主键以外的列
    savePackageName 保存的包名 java.lang.String
    savePath 保存路径 java.lang.String
    saveModelName 保存的model名称 java.lang.String
columnInfo 列对象
    obj 列原始对象 com.intellij.database.model.DasColumn
    name 列名(首字母小写) java.lang.String
    comment 列注释 java.lang.String
    type 列类型(类型全名) java.lang.String
    shortType 列类型(短类型) java.lang.String
    custom 是否附加列 java.lang.Boolean
    ext 附加字段(Map类型) java.lang.Map<java.lang.String, java.lang.Object>
$tableInfoList java.util.List<TableInfo>所有选中的表
$importList 所有需要导入的包集合 java.util.Set<java.lang.String>

回调
&callback 回调对象
    setFileName(String) 设置文件储存名字
    setSavePath(String) 设置文件储存路径,默认使用选中路径
    setReformat(Boolean) 设置是否重新格式化生成后的代码,默认为界面选定值
    setWriteFile(Boolean) 设置是否生成文件,默认为true
工具
$tool
    firstUpperCase(String name) 首字母大写方法
    firstLowerCase(String name) 首字母小写方法
    getClsNameByFullName(String fullName) 通过包全名获取类名
    getJavaName(String name) 将下划线分割字符串转驼峰命名(属性名)
    getClassName(String name) 将下划线分割字符串转驼峰命名(类名)
    hump2Underline(String str) 将驼峰字符串转下划线字符串
    append(Object... objs) 多个数据进行拼接
    newHashSet(Object... objs) 创建一个HashSet对象
    newArrayList(Object... objs) 创建一个ArrayList对象
    newLinkedHashMap() 创建一个LinkedHashMap()对象
    newHashMap() 创建一个HashMap()对象
    getField(Object obj, String fieldName) 获取对象的属性值,可以访问任意修饰符修饰的属性.配合debug方法使用.
    call(Object... objs) 空白执行方法,用于调用某些方法时消除返回值
    debug(Object obj) 调式方法,用于查询对象结构.可查看对象所有属性与public方法
    serial() 随机获取序列化的UID
    service(String serviceName, Object... param)远程服务调用
    parseJson(String) 将字符串转Map对象
    toJson(Object, Boolean) 将对象转json对象,Boolean:是否格式化json,不填时为不格式化。
    toUnicode(String, Boolean) 将String转换为unicode形式,Boolean:是否转换所有符号,不填时只转换中文及中文符号。
$time
    currTime(String format) 获取当前时间,指定时间格式(默认:yyyy-MM-dd HH:mm:ss)
$generateService
    run(String, Map<String,Object>) 代码生成服务,参数1:模板名称,参数2:附加参数。
$dasUtil Database提供的工具类,具体可方法请查看源码,适用于高端玩家
$dbUtil  Database提供的工具类,具体可方法请查看源码,适用于高端玩

1、定义变量

1
#set($serviceName = $!tool.firstLowerCase($serviceClass))

2、if

1
2
3
4
##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
#set($pk = $tableInfo.pkColumn.get(0))
#end

自定义模板

这是我自己配置的一些模板

使用xia17common时的Jpa模板

这是我自己平常使用的编码风格

1、entity.java.vm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
##引入宏定义
$!{define.vm}

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

##使用宏定义设置包后缀
#setPackageSuffix("entity")

##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
#set($pk = $tableInfo.pkColumn.get(0))
#end

##使用全局变量实现默认包导入
$!{autoImport.vm}
import java.io.Serializable;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import javax.persistence.*;
import com.ss.plugins.common.database.jpa.extension.BaseUserTimeEntity;
import com.ss.plugins.common.database.jpa.extension.JpaEntity;

##使用宏定义实现类注释信息
#tableComment("实体类")
@Entity
@Table(name = "$!{tableInfo.obj.name}")
@Accessors(chain = true)
@Getter
@Setter
@NoArgsConstructor
public class $!{tableInfo.name} implements JpaEntity<$!pk.shortType> , Serializable {
private static final long SERIAL_VERSION_UID = $!tool.serial();

## 主键列
/** ${pk.comment} */
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private $!{tool.getClsNameByFullName($pk.type)} $!{pk.name};

## 其他列
#foreach($column in $tableInfo.otherColumn)
#if(${column.comment})/** ${column.comment} */#end

private $!{tool.getClsNameByFullName($column.type)} $!{column.name};

#end


}

2、repository.java.vm 数据访问层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
##引入宏定义
$!{define.vm}

##使用回调(保存位置与文件后缀)
$!callback.setFileName($tool.append($tableInfo.name, "Repository.java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/repository"))

##使用宏定义设置包后缀
#setPackageSuffix("repository")

##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
#set($pk = $tableInfo.pkColumn.get(0))
#end

##使用全局变量实现默认包导入
$!{autoImport.vm}
import com.ss.plugins.common.database.jpa.extension.Xia17JpaRepository;
import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};

##使用宏定义实现类注释信息
#tableComment("查询类")
public interface $!{tableInfo.name}Repository extends Xia17JpaRepository<$!{tableInfo.name},$!{tool.getClsNameByFullName($pk.type)}> {

}

3、service.java.vm service接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
##引入宏定义
$!{define.vm}

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

##使用宏定义设置包后缀
#setPackageSuffix("service")

##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
#set($pk = $tableInfo.pkColumn.get(0))
#end

##使用全局变量实现默认包导入
$!{autoImport.vm}
import com.ss.plugins.common.database.jpa.extension.JpaService;
import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};

##使用宏定义实现类注释信息
#tableComment("service")
public interface $!{tableInfo.name}Service extends JpaService<$!{tableInfo.name}, $!{tool.getClsNameByFullName($pk.type)}> {

}

4、serviceImpl.java.vm service接口的实现类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
##引入宏定义
$!{define.vm}

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

##使用宏定义设置包后缀
#setPackageSuffix("service.impl")

##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
#set($pk = $tableInfo.pkColumn.get(0))
#end

##使用全局变量实现默认包导入
$!{autoImport.vm}
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ss.plugins.common.database.jpa.extension.JpaTxServiceImpl;
import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import $!{tableInfo.savePackageName}.repository.$!{tableInfo.name}Repository;
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;

##使用宏定义实现类注释信息
#tableComment("serviceImpl")
@Service
@RequiredArgsConstructor
public class $!{tableInfo.name}ServiceImpl extends JpaTxServiceImpl<$!{tableInfo.name}, $!{tool.getClsNameByFullName($pk.type)}, $!{tableInfo.name}Repository> implements $!{tableInfo.name}Service {

}

5、aloneservice.java.vm , 这是不使用接口的service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
##引入宏定义
$!{define.vm}

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

##使用宏定义设置包后缀
#setPackageSuffix("service")

##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
#set($pk = $tableInfo.pkColumn.get(0))
#end

##使用全局变量实现默认包导入
$!{autoImport.vm}
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ss.plugins.common.database.jpa.extension.JpaTxServiceImpl;
import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import $!{tableInfo.savePackageName}.repository.$!{tableInfo.name}Repository;

##使用宏定义实现类注释信息
#tableComment("service")
@Service
@RequiredArgsConstructor
public class $!{tableInfo.name}Service extends JpaTxServiceImpl<$!{tableInfo.name}, $!{tool.getClsNameByFullName($pk.type)}, $!{tableInfo.name}Repository> {

}

6、query.java.vm 查询类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
##引入宏定义
$!{define.vm}

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

##使用宏定义设置包后缀
#setPackageSuffix("query")

##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
#set($pk = $tableInfo.pkColumn.get(0))
#end

##使用全局变量实现默认包导入
$!{autoImport.vm}
import java.io.Serializable;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import com.ss.plugins.common.database.jpa.query.BaseQuery;
import com.ss.plugins.common.database.jpa.query.Query;
import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};


##使用宏定义实现类注释信息
#tableComment("查询类")
@Accessors(chain = true)
@Getter
@Setter
@NoArgsConstructor
public class $!{tableInfo.name}Query implements BaseQuery<$!{tableInfo.name}> , Serializable {

private static final long SERIAL_VERSION_UID = $!tool.serial();

## 所有列
#foreach($column in $tableInfo.fullColumn)
#if(${column.comment})/** ${column.comment} */#end

private $!{tool.getClsNameByFullName($column.type)} $!{column.name};

#end


}

7、controller.java.vm 控制器类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
##引入宏定义
$!{define.vm}

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

##使用宏定义设置包后缀
#setPackageSuffix("controller")

##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
#set($pk = $tableInfo.pkColumn.get(0))
#end

##定义serviceName
#set($serviceClass = $tool.append($tableInfo.name, "Service"))
#set($serviceName = $!tool.firstLowerCase($serviceClass))

#set($tableComment = $tableInfo.name)

## 定义表的注释名
#if(!$tableInfo.comment.isBlank())
#set($tableComment = $tableInfo.comment)
#end


##使用全局变量实现默认包导入
$!{autoImport.vm}
import com.ss.plugins.common.data.Back;
import com.ss.plugins.common.data.PageData;
import com.ss.plugins.common.data.PageParam;
import com.ss.plugins.common.database.jpa.JpaUtil;
import com.ss.plugins.common.utils.VerifyUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import $!{tableInfo.savePackageName}.query.$!{tableInfo.name}Query;
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

##类注释信息
/**
* $!{tableComment}接口
*
* @author xia17
* @date $!{time.currTime('yyyy-MM-dd HH:mm:ss')}
*/
@RestController
@RequestMapping("$!tool.firstLowerCase($tableInfo.name)")
@RequiredArgsConstructor
public class $!{tableInfo.name}Controller {

private final $!{serviceClass} $!{serviceName};

/**
* 新增$!{tableComment}
*
* @param resource 请求参数
* @return 返回新增的数据
*/
@PostMapping("create")
public Back<$!{tableInfo.name}> create(@RequestBody @Valid $!{tableInfo.name} resource) {
$!{tableInfo.name} entity = $!{serviceName}.create(resource);
return Back.ok(entity);
}


/**
* 修改$!{tableComment}
*
* @param resource 请求参数
* @return 返回修改后的数据
*/
@PostMapping("update")
public Back<$!{tableInfo.name}> update(@RequestBody @Valid $!{tableInfo.name} resource) {
$!{tableInfo.name} entity = $!{serviceName}.update(resource);
return Back.ok(entity);
}

/**
* 删除单个$!{tableComment}
*
* @param id $!{tableComment}的id
* @return /
*/
@PostMapping
public Back<String> delete(@NotNull $!{tool.getClsNameByFullName($pk.type)} id) {
VerifyUtil.notNull(id, "参数异常");
$!{serviceName}.deleteById(id);
return Back.ok();
}

/**
* 删除多个$!{tableComment}
*
* @param ids $!{tableComment}的id
* @return /
*/
@PostMapping("delete_by_ids")
public Back<String> deleteByIds(@NotNull @RequestBody List<$!{tool.getClsNameByFullName($pk.type)}> ids) {
$!{serviceName}.deleteById(ids);
return Back.ok();
}


/**
* $!{tableComment}分页列表
*
* @param query 查询条件
* @param pageParam 分页参数
* @return /
*/
@GetMapping
public Back<PageData<$!{tableInfo.name}>> page($!{tableInfo.name}Query query, @Valid PageParam pageParam) {
Page<$!{tableInfo.name}> page = $!{serviceName}.findAll(query, pageParam.toPageable());
return Back.ok(JpaUtil.page(page));
}


/**
* $!{tableComment}列表
*
* @param query 查询条件
* @return /
*/
@GetMapping("list")
public Back<List<$!{tableInfo.name}>> list($!{tableInfo.name}Query query) {
List<$!{tableInfo.name}> list = $!{serviceName}.findAll(query);
return Back.ok(list);
}

}