1.Idea插件搜索easy code并安装 2.在idea设置找到easy code,其中本文中主要用到Global Config(全局配置)和 Template(模板) 3.由于设计业务,表名很多都有前缀及类名有时会有内部规定,所有先在Global Config里面的define.vm申明全局变量,easy code的语法是velotity。
define.vm
##由于表名存在下划线,先进行截取下划线后面的内容
#set($className = $tool.getClassName($tableInfo.obj.name.substring($tableInfo.obj.name.indexOf("_"))))
#set($baseUrl = "/src/main/java/com/xxxx/xxxx/xxxx/")
#set($fileSuffixName = ".java")
#set($classPrefix = "Sys")
##各部分定义的全局变量,在下面编写中和模板中会用上,如${baseUrl}或者${entityName}
##entity
#set($entityName = $!tool.append($classPrefix,$className))
#set($entitySavePath = $!tool.append($modulePath,$baseUrl,"entity"))
#set($entityFileName = $tool.append($entityName, $fileSuffixName))
##mapper
#set($mapperName = $!tool.append($entityName,"Mapper"))
#set($mapperSavePath = $!tool.append($modulePath, $baseUrl,"dao"))
#set($mapperFileName = $tool.append($mapperName, $fileSuffixName))
##service
##由于规则service接口要加上I,所以用append方法。
#set($serviceName = $!tool.append("I",$entityName,"Service"))
#set($serviceSavePath = $!tool.append($modulePath, $baseUrl,"service"))
#set($serviceFileName = $!tool.append($serviceName, $fileSuffixName))
##serviceImpl
#set($serviceImplName = $!tool.append($entityName,"ServiceImpl"))
#set($serviceImplSavePath = $!tool.append($modulePath, $baseUrl,"service/impl"))
#set($serviceImplFileName = $!tool.append($serviceImplName, $fileSuffixName))
##controller
#set($controllerName = $!tool.append(${entityName},"Controller"))
#set($controllerSavePath = $!tool.append($modulePath, $baseUrl,"web"))
#set($controllerFileName = $!tool.append($controllerName, $fileSuffixName))
##定义直接保存路径与文件名简化的宏定义,调用方式:#save("entity")
##save方法原本就有,觉得不适用,就自定义修改了,
#macro(save $type)
#if($type == "entity")
##这里的$entitySavePath就是上文中通过#set()自定义的
##保存路径
$!callback.setSavePath($entitySavePath)
##保存文件名
$!callback.setFileName($entityFileName)
#end
#if($type == "mapper")
$!callback.setSavePath($mapperSavePath)
$!callback.setFileName($mapperFileName)
#end
#if($type == "service")
$!callback.setSavePath($serviceSavePath)
$!callback.setFileName($serviceFileName)
#end
#if($type == "serviceImpl")
$!callback.setSavePath($serviceImplSavePath)
$!callback.setFileName($serviceImplFileName)
#end
#if($type == "controller")
$!callback.setSavePath($controllerSavePath)
$!callback.setFileName($controllerFileName)
#end
#end
##感觉首字符小写的文本长度有点长就写了短一点的
#macro(toLow $str)$!tool.firstLowerCase($str)#end
template
entity模板
$!{define.vm}
##保存路径及文件名,具体方法在Global Config里面编写
#save("entity")
##包路径(宏定义)
#setPackageSuffix("entity")
##自动导入包(全局变量)
$!{autoImport.vm}
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonView;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
##表注释(宏定义)
#tableComment("")
//@establish: easyCode Automatic generation
@Data
@EqualsAndHashCode(callSuper = true)
@Accessors(chain = true)
@TableName("$!{tableInfo.obj.name}")
##BaseEntity为自己创建,不需要则可改为序列化
public class ${entityName} extends BaseEntity {
#foreach($column in $tableInfo.fullColumn)
#if(!$column.name.equals("id")
&&!$column.name.equals("addUser")
&&!$column.name.equals("addTime")
&&!$column.name.equals("modUser")
&&!$column.name.equals("modTime")
&&!$column.name.equals("orderTag")
&&!$column.name.equals("delFlag")
&&!$column.name.equals("remarks")
)
#if(${column.comment})
/**
* ${column.comment}
*/
#end
@TableField("$!column.obj.name")
private $!{tool.getClsNameByFullName($column.type)} $!{column.name};
#end
#end
}
mapper
$!{define.vm}
##保存文件(宏定义)
#save("mapper")
##包路径(宏定义)
#setPackageSuffix("dao")
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import $!{tableInfo.savePackageName}.entity.${entityName};
import org.apache.ibatis.annotations.Mapper;
/**
* @Author: $author
* @Date: $time.currTime("yyyy-MM-dd HH:mm:ss")
* @establish: easyCode Automatic generation
*/
@Mapper
public interface ${mapperName} extends BaseMapper<${entityName}> {
}
service
$!{define.vm}
##保存文件(宏定义)
#save("service")
##包路径(宏定义)
#setPackageSuffix("service")
/**
* @Author: $author
* @Date: $time.currTime("yyyy-MM-dd HH:mm:ss")
* @establish: easyCode Automatic generation
*/
import com.baomidou.mybatisplus.extension.service.IService;
import $!{tableInfo.savePackageName}.entity.${entityName};
public interface ${serviceName} extends IService<${entityName}> {
}
serviceImpl
##导入宏定义
$!{define.vm}
##保存文件(宏定义)
#save("serviceImpl")
##包路径(宏定义)
#setPackageSuffix("service.impl")
/**
* @Author: $author
* @Date: $time.currTime("yyyy-MM-dd HH:mm:ss")
* @establish: easyCode Automatic generation
*/
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import $!{tableInfo.savePackageName}.dao.${mapperName};
import $!{tableInfo.savePackageName}.entity.${entityName};
import $!{tableInfo.savePackageName}.service.${serviceName};
import org.springframework.stereotype.Service;
##表注释(宏定义)
#tableComment("表服务实现类")
@Service("$tool.firstLowerCase(${serviceName})")
public class ${entityName}ServiceImpl extends ServiceImpl<${mapperName}, ${entityName}> implements ${serviceName} {
}
controller
##导入宏定义
$!{define.vm}
##保存文件(宏定义)
#save("controller")
##包路径(宏定义)
#setPackageSuffix("web")
/**
* @Author: $author
* @Date: $time.currTime("yyyy-MM-dd HH:mm:ss")
* @establish: easyCode Automatic generation
*/
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.${entityName};
import $!{tableInfo.savePackageName}.service.${serviceName};
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
##表注释(宏定义)
#tableComment("表控制层")
@RestController
@RequestMapping("api/#toLow("${entityName}")")
public class ${controllerName} {
#set($servcieTolow = $tool.firstLowerCase($tool.append($!{entityName},"Service")))
@Resource
private $!{serviceName} ${servcieTolow} ;
}
在具体模板设置页下面有很多说明方法都比较实用,最主要的是根据自己的业务名称需求在global config的define.vm设置常量,变量,编写方法,后续在模板中调用. ${var} 如果不为空则显示,如果为空则直接输出代码文本。 $!{var} 如果不为空则显示,如果为空则不输出。
idea连接数据库后,右键表名选择easyCode(也可shift多选,ctrl去除),选择好生成的路径包,勾选要生成的类,如果表有前缀,则写出来,它会自动去除。
|