前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >懒人:使用 idea 插件 Easy Code 自定义 MybatisPlus 模板一键快速生成所需代码

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

作者头像
create17
发布2020-05-26 14:51:23
6K0
发布2020-05-26 14:51:23
举报

每一个成功人士的背后,必定曾经做出过勇敢而又孤独的决定。

放弃不难,但坚持很酷~

之前无意中了解到了 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
代码语言:javascript
复制
##导入宏定义
$!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
代码语言:javascript
复制
##定义初始变量
#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
代码语言:javascript
复制
##定义初始变量
#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
代码语言:javascript
复制
##导入宏定义
$!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
代码语言:javascript
复制
##定义初始变量
#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
代码语言:javascript
复制
##引入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 插件,配合着自己定义的宏操作,用的确实太爽了,解放劳动力啊。生成完代码之后,我们只需要在其中写业务代码即可。


点关注,不迷路

好了各位,以上就是这篇文章的全部内容了,能看到这里的人呀,都是人才

白嫖不好,创作不易。 各位的支持和认可,就是我创作的最大动力,我们下篇文章见!

如果本篇博客有任何错误,请批评指教,不胜感激 !

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-05-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 大数据实战演练 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、idea 安装 Easy Code 插件
  • 二、使用 idea 连接 MySQL 数据库
  • 三、配置 Easy Code 生成模板
    • 3.1、entity
      • 3.2、mapper
        • 3.3、service
          • 3.4、serviceImpl
            • 3.5、controller
              • 3.6、mapper.xml
              • 四、添加类型映射
              • 五、快速生成代码
              • 点关注,不迷路
              相关产品与服务
              云数据库 SQL Server
              腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档