Idea 生成代码的插件 Easy Code 介绍

本贴最后更新于 1597 天前,其中的信息可能已经渤澥桑田

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

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

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

安装

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

image.png

配置

Easy Code 配置

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

image.png

Type Mapper 配置

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

image.png

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://b3logfile.com/file/2020/04/image-13227096.png)


}

使用

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

image.png

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

image.png

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

image.png

  • IDEA

    IDEA 全称 IntelliJ IDEA,是一款 Java 语言开发的集成环境,在业界被公认为最好的 Java 开发工具之一。IDEA 是 JetBrains 公司的产品,这家公司总部位于捷克共和国的首都布拉格,开发人员以严谨著称的东欧程序员为主。

    180 引用 • 400 回帖

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...