网站首页 > 博客文章 正文
每一个成功人士的背后,必定曾经做出过勇敢而又孤独的决定。
放弃不难,但坚持很酷~
之前无意中了解到了 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 插件,配合着自己定义的宏操作,用的确实太爽了,解放劳动力啊。生成完代码之后,我们只需要在其中写业务代码即可。
点关注,不迷路
好了各位,以上就是这篇文章的全部内容了,能看到这里的人呀,都是人才。
白嫖不好,创作不易。各位的支持和认可,就是我创作的最大动力,我们下篇文章见!
如果本篇博客有任何错误,请批评指教,不胜感激 !
? ambari 自定义服务集成原理讲解,自定义服务项目结构解读
? Ambari 2.7.3.0 安装部署 hadoop 3.1.0.0 集群视频完整版
? 都快2020年了,ambari自定义服务集成,你还没掌握吗?文末有福利
欢迎大家留言讨论
?? ?? ??
朕已阅
猜你喜欢
- 2024-11-22 Idea常用配置,这样做更好用
- 2024-11-22 idea开发工具简介—界面常用设置介绍(一)
- 2024-11-22 IntelliJ IDEA中一键生成Controller、Service、Dao、Model层代码
- 2024-11-22 Spring Boot自定义配置的提示
- 2024-11-22 idea的基础配置和常用快捷键
- 2024-11-22 IntelliJ IDEA 如何在 Java 中进行快速注释
- 2024-11-22 Spring Boot源码学习:自动配置与自定义注解详解
- 2024-11-22 推荐 33 个 IDEA 最牛配置,写代码太爽了
- 2024-11-22 2w字长文给你讲透了配置类为什么要添加 @Configuration注解
- 2024-11-22 IDEA安装后要做哪些配置你知道吗?这篇文章统统告诉你
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- powershellfor (55)
- messagesource (56)
- aspose.pdf破解版 (56)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- ubuntu升级gcc (58)
- nacos启动失败 (64)
- ssh-add (70)
- jwt漏洞 (58)
- macos14下载 (58)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- vue回到顶部 (57)
- qcombobox样式表 (68)
- vue数组concat (56)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)