EasyCode 插件
EasyCode 插件 是一款根據(jù)表結(jié)構(gòu)生成代碼的很方便的Idea插件, 強(qiáng)烈推薦. 并且可以自定義模板來控制生成的類
我在使用的過程中發(fā)現(xiàn)一些問題,現(xiàn)在把解決辦法記錄下來, 我主要使用的是插件自帶的mybatisplus模板
1. 生成的代碼中有大量的get set方法
lombok 插件是個好東西, 我刪除了模板中的get和set方法, 添加了lombok 的注解, '
2. 如果數(shù)據(jù)庫中的表都有前綴"t_" 導(dǎo)致生成的類名中都有一個前綴 “T”
這個問題困擾我很久,改了各種模板 , 最后發(fā)現(xiàn)把init文件的第一行代碼復(fù)制到define文件的第一行就可以, init文件根本就沒有用.
3, 生成的類中沒有DTO對象
直接把entity模板文件復(fù)制一份改改就有了
下面分享下我修改后的模板
Template Setting 配置項 Group Name : MybatisPlus
如果沒有MybatisPlus 的group name, 可以新增一個
dto文件
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
|
##導(dǎo)入宏定義 $!define ##保存文件(宏定義) #save( "/dto" , "DTO.java" ) ##包路徑(宏定義) #setPackageSuffix( "dto" ) ##自動導(dǎo)入包(全局變量) $!autoImport ## import com.baomidou.mybatisplus.extension.activerecord.Model; import java.io.Serializable; import lombok.Data; ## import com.baomidou.mybatisplus.annotation.IdType; ## import com.baomidou.mybatisplus.annotation.TableId; ##表注釋(宏定義) #tableComment( "表實體類" ) @Data @SuppressWarnings ( "serial" ) public class $!{tableInfo.name}DTO implements Serializable { #foreach($column in $tableInfo.fullColumn) # if (${column.comment}) /**${column.comment}*/ #end private $!{tool.getClsNameByFullName($column.type)} $!{column.name}; #end ###foreach($column in $tableInfo.fullColumn) ## #getSetMethod($column) ###end ###foreach($column in $tableInfo.pkColumn) ## /** ## * 獲取主鍵值 ## * ## * @return 主鍵值 ## */ ## @Override ## protected Serializable pkVal() { ## return this .$!column.name; ## } ## # break ###end } |
controller 文件
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
|
##導(dǎo)入宏定義 $!define ##設(shè)置表后綴(宏定義) #setTableSuffix( "Controller" ) ##保存文件(宏定義) #save( "/controller" , "Controller.java" ) ##包路徑(宏定義) #setPackageSuffix( "controller" ) ##定義服務(wù)名 #set($serviceName = $!tool.append($!tool.firstLowerCase($!tableInfo.name), "Service" )) ##定義實體對象名 #set($entityName = $!tool.firstLowerCase($!tableInfo.name)) import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.api.ApiController; import com.baomidou.mybatisplus.extension.api.R; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import $!{tableInfo.savePackageName}.entity.$!tableInfo.name; import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import java.io.Serializable; import java.util.List; ##表注釋(宏定義) #tableComment( "表控制層" ) @RestController @RequestMapping ( "$!tool.firstLowerCase($!tableInfo.name)" ) public class $!{tableName} extends ApiController { /** * 服務(wù)對象 */ @Resource private $!{tableInfo.name}Service $!{serviceName}; /** * 分頁查詢所有數(shù)據(jù) * * @param page 分頁對象 * @param $!entityName 查詢實體 * @return 所有數(shù)據(jù) */ @GetMapping public R selectAll(Page<$!tableInfo.name> page, $!tableInfo.name $!entityName) { return success( this .$!{serviceName}.page(page, new QueryWrapper<>($!entityName))); } /** * 通過主鍵查詢單條數(shù)據(jù) * * @param id 主鍵 * @return 單條數(shù)據(jù) */ @GetMapping ( "{id}" ) public R selectOne( @PathVariable Serializable id) { return success( this .$!{serviceName}.getById(id)); } /** * 新增數(shù)據(jù) * * @param $!entityName 實體對象 * @return 新增結(jié)果 */ @PostMapping public R insert( @RequestBody $!tableInfo.name $!entityName) { return success( this .$!{serviceName}.save($!entityName)); } /** * 修改數(shù)據(jù) * * @param $!entityName 實體對象 * @return 修改結(jié)果 */ @PutMapping public R update( @RequestBody $!tableInfo.name $!entityName) { return success( this .$!{serviceName}.updateById($!entityName)); } /** * 刪除數(shù)據(jù) * * @param idList 主鍵結(jié)合 * @return 刪除結(jié)果 */ @DeleteMapping public R delete( @RequestParam ( "idList" ) List<Long> idList) { return success( this .$!{serviceName}.removeByIds(idList)); } } |
serviceImpl 文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
##導(dǎo)入宏定義 $!define ##設(shè)置表后綴(宏定義) #setTableSuffix( "ServiceImpl" ) ##保存文件(宏定義) #save( "/service/impl" , "ServiceImpl.java" ) ##包路徑(宏定義) #setPackageSuffix( "service.impl" ) import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import $!{tableInfo.savePackageName}.dao.$!{tableInfo.name}Dao; import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name}; import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service; import org.springframework.stereotype.Service; ##表注釋(宏定義) #tableComment( "表服務(wù)實現(xiàn)類" ) @Service ( "$!tool.firstLowerCase($tableInfo.name)Service" ) public class $!{tableName} extends ServiceImpl<$!{tableInfo.name}Dao, $!{tableInfo.name}> implements $!{tableInfo.name}Service { } |
service文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
##導(dǎo)入宏定義 $!define ##設(shè)置表后綴(宏定義) #setTableSuffix( "Service" ) ##保存文件(宏定義) #save( "/service" , "Service.java" ) ##包路徑(宏定義) #setPackageSuffix( "service" ) import com.baomidou.mybatisplus.extension.service.IService; import $!{tableInfo.savePackageName}.entity.$!tableInfo.name; ##表注釋(宏定義) #tableComment( "表服務(wù)接口" ) public interface $!{tableName} extends IService<$!tableInfo.name> { } |
dao文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
##導(dǎo)入宏定義 $!define ##設(shè)置表后綴(宏定義) #setTableSuffix( "Dao" ) ##保存文件(宏定義) #save( "/dao" , "Dao.java" ) ##包路徑(宏定義) #setPackageSuffix( "dao" ) import com.baomidou.mybatisplus.core.mapper.BaseMapper; import $!{tableInfo.savePackageName}.entity.$!tableInfo.name; ##表注釋(宏定義) #tableComment( "表數(shù)據(jù)庫訪問層" ) public interface $!{tableName} extends BaseMapper<$!tableInfo.name> { } |
entity 文件
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
|
##導(dǎo)入宏定義 $!define ##保存文件(宏定義) #save( "/entity" , ".java" ) ##包路徑(宏定義) #setPackageSuffix( "entity" ) ##自動導(dǎo)入包(全局變量) $!autoImport import com.baomidou.mybatisplus.extension.activerecord.Model; import java.io.Serializable; import lombok.Data; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; ##表注釋(宏定義) #tableComment( "表實體類" ) @Data @SuppressWarnings ( "serial" ) public class $!{tableInfo.name} extends Model<$!{tableInfo.name}> { @TableId (type = IdType.AUTO) #foreach($column in $tableInfo.fullColumn) # if (${column.comment}) /**${column.comment}*/ #end private $!{tool.getClsNameByFullName($column.type)} $!{column.name}; #end ###foreach($column in $tableInfo.fullColumn) ## #getSetMethod($column) ###end #foreach($column in $tableInfo.pkColumn) /** * 獲取主鍵值 * * @return 主鍵值 */ @Override protected Serializable pkVal() { return this .$!column.name; } # break #end } |
Global Config 配置項 Group Name : Default
下面的文件都是Group Name 為 Default 的
mybatisSupport 文件
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
|
##針對Mybatis 進(jìn)行支持,主要用于生成xml文件 #foreach($column in $tableInfo.fullColumn) ##儲存列類型 $tool.call($column.ext.put( "sqlType" , $tool.getField($column.obj.dataType, "typeName" ))) # if ($tool.newHashSet( "java.lang.String" ).contains($column.type)) #set($jdbcType= "VARCHAR" ) #elseif($tool.newHashSet( "java.lang.Boolean" , "boolean" ).contains($column.type)) #set($jdbcType= "BOOLEAN" ) #elseif($tool.newHashSet( "java.lang.Byte" , "byte" ).contains($column.type)) #set($jdbcType= "BYTE" ) #elseif($tool.newHashSet( "java.lang.Integer" , "int" , "java.lang.Short" , "short" ).contains($column.type)) #set($jdbcType= "INTEGER" ) #elseif($tool.newHashSet( "java.lang.Long" , "long" ).contains($column.type)) #set($jdbcType= "INTEGER" ) #elseif($tool.newHashSet( "java.lang.Float" , "float" , "java.lang.Double" , "double" ).contains($column.type)) #set($jdbcType= "NUMERIC" ) #elseif($tool.newHashSet( "java.util.Date" , "java.sql.Timestamp" , "java.time.Instant" , "java.time.LocalDateTime" , "java.time.OffsetDateTime" , " java.time.ZonedDateTime" ).contains($column.type)) #set($jdbcType= "TIMESTAMP" ) #elseif($tool.newHashSet( "java.sql.Date" , "java.time.LocalDate" ).contains($column.type)) #set($jdbcType= "TIMESTAMP" ) # else ##其他類型 #set($jdbcType= "OTHER" ) #end $tool.call($column.ext.put( "jdbcType" , $jdbcType)) #end ##定義宏,查詢所有列 #macro(allSqlColumn)#foreach($column in $tableInfo.fullColumn)$column.obj.name# if ($velocityHasNext), #end#end#end autoImport 文件 ##自動導(dǎo)入包(僅導(dǎo)入實體屬性需要的包,通常用于實體類) #foreach($ import in $importList) import $! import ; #end |
define 文件
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
|
##(Velocity宏定義) ## 去掉表的t_前綴 $!tableInfo.setName($tool.getClassName($tableInfo.obj.name.replaceFirst( "t_" , "" ))) ##定義設(shè)置表名后綴的宏定義,調(diào)用方式:#setTableSuffix( "Test" ) #macro(setTableSuffix $suffix) #set($tableName = $!tool.append($tableInfo.name, $suffix)) #end ##定義設(shè)置包名后綴的宏定義,調(diào)用方式:#setPackageSuffix( "Test" ) #macro(setPackageSuffix $suffix) # if ($suffix!= "" ) package #end# if ($tableInfo.savePackageName!= "" )$!{tableInfo.savePackageName}.#{end}$!suffix; #end ##定義直接保存路徑與文件名簡化的宏定義,調(diào)用方式:#save( "/entity" , ".java" ) #macro(save $path $fileName) $!callback.setSavePath($tool.append($tableInfo.savePath, $path)) $!callback.setFileName($tool.append($tableInfo.name, $fileName)) #end ##定義表注釋的宏定義,調(diào)用方式:#tableComment( "注釋信息" ) #macro(tableComment $desc) /** * $!{tableInfo.comment}($!{tableInfo.name})$desc * * @author $!author * @since $!time.currTime() */ #end ##定義GET,SET方法的宏定義,調(diào)用方式:#getSetMethod($column) #macro(getSetMethod $column) public $!{tool.getClsNameByFullName($column.type)} get$!{tool.firstUpperCase($column.name)}() { return $!{column.name}; } public void set$!{tool.firstUpperCase($column.name)}($!{tool.getClsNameByFullName($column.type)} $!{column.name}) { this .$!{column.name} = $!{column.name}; } #end |
init 文件
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
|
##初始化區(qū)域 ##去掉表的t_前綴 $!tableInfo.setName($tool.getClassName($tableInfo.obj.name.replaceFirst( "t_" , "" ))) ##參考阿里巴巴開發(fā)手冊,POJO 類中布爾類型的變量,都不要加 is 前綴,否則部分框架解析會引起序列化錯誤 #foreach($column in $tableInfo.fullColumn) # if ($column.name.startsWith( "is" ) && $column.type.equals( "java.lang.Boolean" )) $!column.setName($tool.firstLowerCase($column.name.substring( 2 ))) #end #end ##實現(xiàn)動態(tài)排除列 #set($temp = $tool.newHashSet( "testCreateTime" , "otherColumn" )) #foreach($item in $temp) #set($newList = $tool.newArrayList()) #foreach($column in $tableInfo.fullColumn) # if ($column.name!=$item) ##帶有反回值的方法調(diào)用時使用$tool.call來消除返回值 $tool.call($newList.add($column)) #end #end ##重新保存 $tableInfo.setFullColumn($newList) #end ##對importList進(jìn)行篡改 #set($temp = $tool.newHashSet()) #foreach($column in $tableInfo.fullColumn) # if (!$column.type.startsWith( "java.lang." )) ##帶有反回值的方法調(diào)用時使用$tool.call來消除返回值 $tool.call($temp.add($column.type)) #end #end ##覆蓋 #set($importList = $temp) |
到此這篇關(guān)于idea的easyCode的 MybatisPlus模板的配置詳解的文章就介紹到這了,更多相關(guān)idea easyCode MybatisPlus配置內(nèi)容請搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/leisurelen/article/details/103930429