idea的easyCode的 MybatisPlus模板的配置詳解
EasyCode 插件
EasyCode 插件 是一款根據(jù)表結(jié)構(gòu)生成代碼的很方便的Idea插件, 強(qiáng)烈推薦. 并且可以自定義模板來控制生成的類
我在使用的過程中發(fā)現(xiàn)一些問題,現(xiàn)在把解決辦法記錄下來, 我主要使用的是插件自帶的mybatisplus模板
1. 生成的代碼中有大量的get set方法
lombok 插件是個(gè)好東西, 我刪除了模板中的get和set方法, 添加了lombok 的注解, '
2. 如果數(shù)據(jù)庫中的表都有前綴"t_" 導(dǎo)致生成的類名中都有一個(gè)前綴 “T”
這個(gè)問題困擾我很久,改了各種模板 , 最后發(fā)現(xiàn)把init文件的第一行代碼復(fù)制到define文件的第一行就可以, init文件根本就沒有用.
3, 生成的類中沒有DTO對(duì)象
直接把entity模板文件復(fù)制一份改改就有了
下面分享下我修改后的模板
Template Setting 配置項(xiàng) Group Name : MybatisPlus
如果沒有MybatisPlus 的group name, 可以新增一個(gè)
dto文件
##導(dǎo)入宏定義 $!define ##保存文件(宏定義) #save("/dto", "DTO.java") ##包路徑(宏定義) #setPackageSuffix("dto") ##自動(dòng)導(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("表實(shí)體類") @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 文件
##導(dǎo)入宏定義 $!define ##設(shè)置表后綴(宏定義) #setTableSuffix("Controller") ##保存文件(宏定義) #save("/controller", "Controller.java") ##包路徑(宏定義) #setPackageSuffix("controller") ##定義服務(wù)名 #set($serviceName = $!tool.append($!tool.firstLowerCase($!tableInfo.name), "Service")) ##定義實(shí)體對(duì)象名 #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ù)對(duì)象 */ @Resource private $!{tableInfo.name}Service $!{serviceName}; /** * 分頁查詢所有數(shù)據(jù) * * @param page 分頁對(duì)象 * @param $!entityName 查詢實(shí)體 * @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 實(shí)體對(duì)象 * @return 新增結(jié)果 */ @PostMapping public R insert(@RequestBody $!tableInfo.name $!entityName) { return success(this.$!{serviceName}.save($!entityName)); } /** * 修改數(shù)據(jù) * * @param $!entityName 實(shí)體對(duì)象 * @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 文件
##導(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ù)實(shí)現(xiàn)類") @Service("$!tool.firstLowerCase($tableInfo.name)Service") public class $!{tableName} extends ServiceImpl<$!{tableInfo.name}Dao, $!{tableInfo.name}> implements $!{tableInfo.name}Service { }
service文件
##導(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文件
##導(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 文件
##導(dǎo)入宏定義 $!define ##保存文件(宏定義) #save("/entity", ".java") ##包路徑(宏定義) #setPackageSuffix("entity") ##自動(dòng)導(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("表實(shí)體類") @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 配置項(xiàng) Group Name : Default
下面的文件都是Group Name 為 Default 的
mybatisSupport 文件
##針對(duì)Mybatis 進(jìn)行支持,主要用于生成xml文件 #foreach($column in $tableInfo.fullColumn) ##儲(chǔ)存列類型 $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òng)導(dǎo)入包(僅導(dǎo)入實(shí)體屬性需要的包,通常用于實(shí)體類) #foreach($import in $importList) import $!import; #end
define 文件
##(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 ##定義直接保存路徑與文件名簡(jiǎn)化的宏定義,調(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 文件
##初始化區(qū)域 ##去掉表的t_前綴 $!tableInfo.setName($tool.getClassName($tableInfo.obj.name.replaceFirst("t_",""))) ##參考阿里巴巴開發(fā)手冊(cè),POJO 類中布爾類型的變量,都不要加 is 前綴,否則部分框架解析會(huì)引起序列化錯(cuò)誤 #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 ##實(shí)現(xiàn)動(dòng)態(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)用時(shí)使用$tool.call來消除返回值 $tool.call($newList.add($column)) #end #end ##重新保存 $tableInfo.setFullColumn($newList) #end ##對(duì)importList進(jìn)行篡改 #set($temp = $tool.newHashSet()) #foreach($column in $tableInfo.fullColumn) #if(!$column.type.startsWith("java.lang.")) ##帶有反回值的方法調(diào)用時(shí)使用$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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于Feign傳輸對(duì)象無法接收參數(shù)的問題
這篇文章主要介紹了基于Feign傳輸對(duì)象無法接收參數(shù)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03java 高并發(fā)中volatile的實(shí)現(xiàn)原理
這篇文章主要介紹了java 高并發(fā)中volatile的實(shí)現(xiàn)原理的相關(guān)資料,在多線程并發(fā)編程中synchronized和Volatile都扮演著重要的角色,Volatile是輕量級(jí)的synchronized,它在多處理器開發(fā)中保證了共享變量的“可見性”,需要的朋友可以參考下2017-03-03Java實(shí)戰(zhàn)之校園外賣點(diǎn)餐系統(tǒng)的實(shí)現(xiàn)
這篇文章主要介紹了如何利用Java實(shí)現(xiàn)簡(jiǎn)易的校園外賣點(diǎn)餐系統(tǒng),文中采用的技術(shù)有:JSP、Spring、SpringMVC、MyBatis 等,感興趣的可以了解一下2022-03-03java面向?qū)ο笤O(shè)計(jì)原則之開閉原則示例解析
這篇文章主要介紹了java面向?qū)ο笤O(shè)計(jì)原則之開閉原則的示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2021-10-10QQ好友列表樹形列表java代碼實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了QQ好友列表樹形列表簡(jiǎn)單實(shí)現(xiàn)方式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08spring 使用RabbitMQ進(jìn)行消息傳遞的示例代碼
這篇文章主要介紹了spring 使用RabbitMQ進(jìn)行消息傳遞的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12