Idea生成代码的插件Easy Code

摘要:简单介绍Easy Code的使用

官方项目地址:https://gitee.com/makejava/EasyCode

官方文档地址:https://gitee.com/makejava/EasyCode/wikis/pages

安装

在Idea的Plugins中心搜索 Easy Code,点击安装,并重启Idea;
注意不要下载错了,第二个是同一作者开发的老版本,已不再维护

在这里插入图片描述

配置

Easy Code配置

Other Settings中点击 Easy Code,可以在右侧修改作者名称,这个名称等下在模版中可以引用到;导入导出模版作用不用过多介绍

Type Mapper配置

Type Mapper设置的是数据库类型与Java类型的映射,默认配置中没有的类型映射可手动添加

在这里插入图片描述

Template Setting配置

Template Setting设置生成代码的模版信息,下面提供一下我的配置模版,可以根据作者文档进行适当修改;我模版的是集成mybatis-plus的配置

  1. entity的模版

我的实体类继承了一个统一的BasePo类,BasePo类包含了id、createDate、lastUpdateDate三个字段,所以生成代码的时候对这个三个字段进行了过滤。$!author获取的作者信息即为前面Easy Code配置的作者名称,时间可用 $!time.currTime(String format)获取,默认为
yyyy-MM-dd HH:mm:ss,其他一些具体的配置,可参照作者的说明文档

##引入宏定义
$!define

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

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

##使用全局变量实现默认包导入
$!autoImport
import lombok.*;


/**
 * Title: $!{tableInfo.name}<br>
 * Description: $!{tableInfo.comment}($!{tableInfo.name})实体类<br>
 * Copyright (c) 公司信息 $!time.currTime("yyyy") <br>
 * Create DateTime: $!time.currTime() <br>
 *
 * @author $!author
 */

@Data
@ToString
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class $!{tableInfo.name} extends BasePo{
  
#foreach($column in $tableInfo.fullColumn)
#if(!${column.name.equals("id")} && !${column.name.equals("createDate")}&& !${column.name.equals("lastUpdateDate")})
#if(${column.comment})
    /**
     * ${column.comment}
     */
#end
    private $!{tool.getClsNameByFullName($column.type)} $!{column.name};
#end
#end

}
  1. dao的模版
##定义初始变量
#set($tableName = $tool.append($tableInfo.name, "Mapper"))
##设置回调
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/dao"))

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

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

import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.springframework.stereotype.Component;

/**
 * Title: $!{tableName}<br>
 * Description: $!{tableInfo.comment}($!{tableInfo.name})表数据库访问层<br>
 * Copyright (c) 公司信息  $!time.currTime("yyyy") <br>
 * Create DateTime: $!time.currTime() <br>
 *
 * @author $!author
 */
@Component
public interface $!{tableName} extends BaseMapper<$tableInfo.name> {

}
  1. service的模版
##定义初始变量
#set($tableName = $tool.append($tableInfo.name, "Service"))
##设置回调
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/service"))

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

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

import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import com.baomidou.mybatisplus.extension.service.IService;

/**
 * Title: $!{tableName}<br>
 * Description: $!{tableInfo.comment}($!{tableInfo.name})表服务接口<br>
 * Copyright (c) 公司信息 $!time.currTime("yyyy") <br>
 * Create DateTime: $!time.currTime() <br>
 *
 * @author $!author
 */
public interface $!{tableName} extends IService<$tableInfo.name>{


}
  1. serviceImpl的模版
##定义初始变量
#set($tableName = $tool.append($tableInfo.name, "ServiceImpl"))
##设置回调
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/service/impl"))

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

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

import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import $!{tableInfo.savePackageName}.dao.$!{tableInfo.name}Mapper;
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * Title: $!{tableName}<br>
 * Description: $!{tableInfo.comment}($!{tableInfo.name})表服务实现类<br>
 * Copyright (c)  公司信息  $!time.currTime("yyyy") <br>
 * Create DateTime: $!time.currTime() <br>
 *
 * @author $!author
 */
@Service
public class $!{tableName} extends ServiceImpl<$!{tableInfo.name}Mapper, $!{tableInfo.name}> implements $!{tableInfo.name}Service {
   ![image.png](https://img.hacpai.com/file/2020/04/image-13227096.png)


}

使用

需要使用Idea的数据库工具Database连接数据库,右键要生成代码的表,选择Generate Code

在这里插入图片描述

选择指定的路径,勾选模版生成即可

在这里插入图片描述

下面是生成的效果图,可根据自己使用的模版进行配置

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值