idea 插件 Easy Code 自定义 MybatisPlus 模板一键快速生成所需代码

本文介绍了如何在IntelliJ IDEA中安装并配置EasyCode插件,以自定义MybatisPlus模板,从而高效地生成entity、mapper、service、controller等代码,提升对MySQL数据库的增删改查开发效率。通过设置模板,可以避免生成不必要的文件,简化开发流程。
摘要由CSDN通过智能技术生成

之前无意中了解到了 idea 中的 Easy Code 插件,说是能快速生成 entity 、mapper、service、controller 等文件,避免很多简单重复性的创建工作,大大提高 MySQL 增删改查的开发效率。 正好今天要做对 MySQL 的增删改查,想着试试这个插件,没想到,特别好用,但也需要自己定制,所以就有了这篇文章,分享如何使用 idea Easy Code 插件配置 Mybatis Plus 模板来提高对 MySQL 的开发效率的。

一、idea 安装 Easy Code 插件

安装完成后,需要重启 idea 生效。

二、使用 idea 连接 MySQL 数据库

配置连接数据库步骤:

View --> Tool Windows --> Database

然后,新建 MySQL 连接,最后如下图所示:

连接成功后,这时候我们可以选择其中一个表,右键:EasyCode --> Generate Code,来快速生成 entity 、mapper、service、controller 等文件。如下图所示:

但是,这样会生成挺多文件,挺多内容的,乱七八糟。有的内容我并不想要,所以我们需要配置 Easy Code 自定义宏操作模板。

三、配置 Easy Code 生成模板

点击 File --> Settings --> Other Settings --> Easy Code --> Template Setting,如下图所示:

我们可以新建 Group,创建宏操作来自动生成 entity 、mapper、service、controller、mapper.xml 等文件。

3.1、entity

##导入宏定义
$!define

##保存文件(宏定义)
#save("/entity", ".java")

##包路径(宏定义)
#setPackageSuffix("entity")

##自动导入包(全局变量)
$!autoImport
import com.baomidou.mybatisplus.extension.activerecord.Model;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serializable;

##表注释(宏定义)
##tableComment("表实体类")
/**
 * $!{tableInfo.comment}($!{tableInfo.name})表实体类
 * 
 * @author liuyzh
 * @since $!time.currTime()
 */
@EqualsAndHashCode(callSuper = true)
@Data
@ApiModel(description = "")
@SuppressWarnings("serial")
public class $!{tableInfo.name} extends Model<$!{tableInfo.name}> implements Serializable {
    private static final long serialVersionUID = $!tool.serial();
#foreach($column in $tableInfo.fullColumn)
    ##if(${column.comment})/**
    ##* ${column.comment}
    ##*/#end

    @ApiModelProperty("$column.comment")
    private $!{tool.getClsNameByFullName($column.type)} $!{column.name};
#end
}

3.2、mapper

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

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

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

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

/**
 * $!{tableInfo.comment}($!{tableInfo.name})表数据库访问层
 *
 * @author liuyzh
 * @since $!time.currTime()
 */
public interface $!{tableName} extends BaseMapper<$!{tableInfo.name}>{

}

3.3、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 com.baomidou.mybatisplus.extension.service.IService;
import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};

/**
 * $!{tableInfo.comment}($!{tableInfo.name})表服务接口层
 *
 * @author liuyzh
 * @since $!time.currTime()
 */
public interface $!{tableInfo.name}Service extends IService<$!{tableInfo.name}>{

}

3.4、serviceImpl

##导入宏定义
$!define

##设置表后缀(宏定义)
#setTableSuffix("ServiceImpl")

##保存文件(宏定义)
#save("/service/impl", "ServiceImpl.java")

##包路径(宏定义)
#setPackageSuffix("service.impl")

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import $!{tableInfo.savePackageName}.mapper.$!{tableInfo.name}Mapper;
import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import org.springframework.stereotype.Service;

##表注释(宏定义)
##tableComment("表服务实现类")
/**
 * $!{tableInfo.comment}($!{tableInfo.name})表服务实现类
 *
 * @author liuyzh
 * @since $!time.currTime()
 */
@Service
public class $!{tableInfo.name}ServiceImpl extends ServiceImpl<$!{tableInfo.name}Mapper, $!{tableInfo.name}> implements $!{tableInfo.name}Service {

}

3.5、controller

##定义初始变量
#set($tableName = $tool.append($tableInfo.name, "Controller"))
##设置回调
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/controller"))
##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
    #set($pk = $tableInfo.pkColumn.get(0))
#end

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

import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

/**
 * $!{tableInfo.comment}($!{tableInfo.name})表服务控制层
 *
 * @author liuyzh
 * @since $!time.currTime()
 */
@Api(tags = "$!{tableInfo.comment}($!{tableInfo.name})") 
@Validated
@RestController
@AllArgsConstructor
@RequestMapping("$!tool.firstLowerCase($tableInfo.name)")
public class $!{tableName} {
    @Resource
    private final $!{tableInfo.name}Service $!tool.firstLowerCase($tableInfo.name)Service;

}

3.6、mapper.xml

##引入mybatis支持
$!mybatisSupport

##设置保存名称与保存位置
$!callback.setFileName($tool.append($!{tableInfo.name}, "Mapper.xml"))
$!callback.setSavePath($tool.append($modulePath, "/src/main/resources/mapper"))

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

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="$!{tableInfo.savePackageName}.mapper.$!{tableInfo.name}Mapper">

    <resultMap type="$!{tableInfo.savePackageName}.entity.$!{tableInfo.name}" id="$!{tableInfo.name}Map">
#foreach($column in $tableInfo.fullColumn)
        <result property="$!column.name" column="$!column.obj.name" jdbcType="$!column.ext.jdbcType"/>
#end
    </resultMap>

</mapper>

四、添加类型映射

点击 File --> Settings --> Other Settings --> Easy Code --> Type Mapper,如下图所示:

在我们生成类文件之前,我们也可以在 idea 的 Database 中的 某个表 中,右键:EasyCode --> Config Table,来修改字段类型和字段备注等。

五、快速生成代码

点击 idea 的 Database,选择其中一个表,右键:EasyCode --> Generate Code,来快速生成 entity 、mapper、service、controller 等文件。如下图所示:

其中 Package 路径为 Application 类的根路径。点击 "OK",实现代码的快速生成。

这个 Easy Code 插件,配合着自己定义的宏操作,用的确实太爽了,解放劳动力啊。生成完代码之后,我们只需要在其中写业务代码即可。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值