写在前面的话:
本脚本模板不收取任何费用,初衷就是不想重复造梯子,如果对各位有帮助的话,可以给作者打上一杯奶茶钱,目前插件还是有点小 Bug,但是不影响使用,现在各平台相关的代码生成插件也比较成熟了,并且带有独特的特色和一些自带的代码提示功能,使用感受整体都不错,但是好用的插件肯定是要收费的啦~具体的请参考下面相关插件查看
查看视频教程
功能介绍
本脚本只适用于 EasyCode 模板搭配使用
支持生成代码具体如下:
是不是很方便,一键生成到底,基本满足你日常 CRUD
虽然还是有点 Bug,但是我觉得他已经成熟了,因为不影响使用,具体 Bug 我会在下文中讲些
使用教程
前置条件
随意创建一个 SpringBoot 项目,在 pom 里面引入相关依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.1</version> </dependency>
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.5.1</version> </dependency>
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.6</version> </dependency>
|
搜索 EasyCode 插件安装
在文件>设置>其他设置>EasyCode>Template 中添加组名,命名随意,可以看到,除了第一个,下面四个都插件自带的,你们也可以尝试一下
接下来就是最重要的添加模板了
pojo.java.vm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| $!{define.vm} #set($author = "SerMs") #save("/pojo", ".java")
#setPackageSuffix("pojo")
import com.baomidou.mybatisplus.annotation.*; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor;
import java.io.Serializable; import java.util.Date;
#tableComment("实体类") @Data @AllArgsConstructor @NoArgsConstructor public class $!{tableInfo.name} implements Serializable{ private static final long serialVersionUID= $!tool.serial(); #foreach($column in $tableInfo.pkColumn) #if(${column.comment})
#end
@TableId(value = "$!{column.obj.name}" , type = IdType.AUTO) private $!{tool.getClsNameByFullName($column.type)} $!{column.name}; #end #foreach($column in $tableInfo.otherColumn)
#if(${column.comment})#end #if(!($column.name.equals('deleted')||$column.name.equals('updateTime')||$column.name.equals('createTime')))@TableField(value = "$!{column.obj.name}")#end #if($column.name.equals('createTime')) @TableField(value = "$!{column.obj.name}" , fill = FieldFill.INSERT)#end #if($column.name.equals('updateTime')) @TableField(value = "$!{column.obj.name}" , fill = FieldFill.INSERT_UPDATE)#end #if($column.name.equals('deleted'))@TableLogic #end private $!{tool.getClsNameByFullName($column.type)} $!{column.name}; #end }
|
serviceImpl.java.vm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| ##定义初始变量 #set($author = "SerMs") #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 com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import $!{tableInfo.savePackageName}.pojo.$!{tableInfo.name}; import $!{tableInfo.savePackageName}.mapper.$!{tableInfo.name}Mapper; import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service; import org.springframework.stereotype.Service;
@Service("$!tool.firstLowerCase($!{tableInfo.name})Service") public class $!{tableName} extends ServiceImpl<$!{tableInfo.name}Mapper, $!{tableInfo.name}> implements $!{tableInfo.name}Service {}
|
mapper.java.vm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| #set($tableName = $tool.append($tableInfo.name, "Mapper")) #set($author = "SerMs")
$!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 com.baomidou.mybatisplus.core.mapper.BaseMapper; import $!{tableInfo.savePackageName}.pojo.$!tableInfo.name;
public interface $!{tableName} extends BaseMapper<$!tableInfo.name> {}
|
service.java.vm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| #set($author = "SerMs") #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}.pojo.$!{tableInfo.name}; import com.baomidou.mybatisplus.extension.service.IService;
public interface $!{tableName} extends IService<$!tableInfo.name> {}
|
controller.java.vm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| #set($author = "SerMs") #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 com.baomidou.mybatisplus.extension.plugins.pagination.Page; import $!{tableInfo.savePackageName}.pojo.$!{tableInfo.name}; import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service; import $!{tableInfo.savePackageName}.util.R; import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource; import java.io.Serializable; import java.util.List;
@RestController @RequestMapping("$!tool.firstLowerCase($tableInfo.name)") public class $!{tableName} {
@Resource private $!{tableInfo.name}Service $!tool.firstLowerCase($tableInfo.name)Service;
@GetMapping public R page(@RequestParam int current,@RequestParam int size){ Page<$!{tableInfo.name}> page=new Page<>(current,size); return R.ok().setData(this.$!{tool.firstLowerCase($tableInfo.name)}Service.page(page));}
@GetMapping("{id}") public R selectOne(@PathVariable Serializable id){ return R.ok().setData(this.$!{tool.firstLowerCase($tableInfo.name)}Service.getById(id));}
@PostMapping public R save(@RequestBody $!{tableInfo.name} $!tool.firstLowerCase($tableInfo.name)){ return R.ok().setData(this.$!{tool.firstLowerCase($tableInfo.name)}Service.save($!tool.firstLowerCase($tableInfo.name)));}
@PutMapping public R updateById(@RequestBody $!{tableInfo.name} $!tool.firstLowerCase($tableInfo.name)){ return R.ok().setData(this.$!{tool.firstLowerCase($tableInfo.name)}Service.updateById($!tool.firstLowerCase($tableInfo.name)));}
@DeleteMapping public R delete(@RequestParam List<Long> id){ return R.ok().setData(this.$!{tool.firstLowerCase($tableInfo.name)}Service.removeByIds(id));} }
|
r.java.vm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| $!{define.vm} #set($author = "SerMs")
#save("/util", "R.java")
#setPackageSuffix("util")
import lombok.Data;
@Data public class R { private int code; private String message; private Object data;
public R setData(Object data) { this.data = data; return this; }
public static R ok() { R r = new R(); r.code = 200; r.message = "OK"; return r; }
public static R fail() { R r = new R(); r.code = 205; r.message = "fail"; return r; }
public static R exp() { R r = new R(); r.code = 500; r.message = "exception"; return r; } }
|
interceptor3.5.1.java.vm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| $!{define.vm} #set($author = "SerMs")
#save("/interceptor", "MybatisPlusPageInterceptor.java")
#setPackageSuffix("interceptor")
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;
@Configuration public class MybatisPlusPageInterceptor {
@Bean public MybatisPlusInterceptor getPaginationInnerInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new PaginationInnerInterceptor()); return interceptor; } }
|
handler3.5.1.java.vm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| $!{define.vm} #set($author = "SerMs")
#save("/handler","MyMetaObjectHandler.java")
#setPackageSuffix("handler")
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; import org.apache.ibatis.reflection.MetaObject; import org.springframework.stereotype.Component;
import java.util.Date;
@Component public class MyMetaObjectHandler implements MetaObjectHandler {
@Override public void insertFill(MetaObject metaObject) { this.setFieldValByName("createTime" , new Date(), metaObject); this.setFieldValByName("updateTime" , new Date(), metaObject); }
@Override public void updateFill(MetaObject metaObject) { this.setFieldValByName("updateTime" , new Date(), metaObject); } }
|
application.yaml.vm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| ##设置保存名称与保存位置 $!callback.setFileName("application.yaml") $!callback.setSavePath($tool.append($modulePath, "/src/main/resources")) spring: datasource: url: jdbc:mysql: username: root password: 123456 type: com.alibaba.druid.pool.DruidDataSource druid: # 下面为连接池的补充设置,应用到上面所有数据源中 # 初始化大小,最小,最大 initial-size: 5 min-idle: 5 max-active: 20 # 配置获取连接等待超时的时间 max-wait: 60000 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 time-between-eviction-runs-millis: 60000 # 配置一个连接在池中最小生存的时间,单位是毫秒 min-evictable-idle-time-millis: 300000
#配置日志,我们所用的sql现在是不可见的,我们希望知道他是怎么执行的,所以我们必须要查看日志! mybatis-plus: global-config: db-config: logic-delete-field: deleted # 全局逻辑删除的实体字段名(since 3.3.0,配置后可以忽略不配置步骤2) logic-delete-value: 1 # 逻辑已删除值(默认为 1) logic-not-delete-value: 0 # 逻辑未删除值(默认为 0) configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
|
添加完后点击应用
一键生成
选择数据库表
右击选择EasyCode
选择第一个
注意看,目前可以看到我项目左右目录里面并没有什么代码~
选择项目工程模块,选择模块对应的包,选择你创建好的组名
选择单表,选择单表,第一次使用模板不要多表!!!!!!!
单表构建全选所有配置,侧面按照我选的来选就行,下面我会讲解,因为这里有 Bug
点击确定之后可以看到我左侧项目目录代码已经生成完毕了~
接下来如果你想多表创建就可以这样选择,不需要在选择applicationYAML&interceptor类&R类&handler配置类了
配置
因为本人技术有限,有些地方还是会有一点小 Bug
如下图所示:在生成的文件里面会发现有 handler 处理类 interceptor 拦截器 util 工具类都相应的爆红了~
不要慌,这里的原因很简单,解决方案也就更很简单了
把文件名修改更 class 类名一直就欧克了
然后修改 applicationYml 文件,把你数据库名修改一下,账号密码修改一下,就可以跑了~
去 mian 访问一下~
分页查询一下 200OK 没问题,
其余的自己测试,这里就不多做阐述了~
注意:Bug!!!!
在项目第一次使用模板的时候建议先单表生成,因为applicationYAML&interceptor类&R类&handler配置类都只需生成一次,如果你多表一次性全部全选,就会创建多个applicationYAML&interceptor类&R类&handler配置类,到时候出事就别说我没提醒~
如有更好的建议,可以在下面留言反馈~
23-12-04更新
dto.java.vm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| $!{define.vm} #set($author = "SerMs") #save("/", ".java") package $!{tableInfo.savePackageName} import com.baomidou.mybatisplus.annotation.*; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.AllArgsConstructor; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; import lombok.experimental.Accessors;
import java.io.Serializable; import java.util.Date;
#tableComment("实体类") @Data @EqualsAndHashCode(callSuper = false) @Accessors(chain = true) @TableName("learning_lesson") @ApiModel(value = "${tableInfo.name}对象", description = "${tableInfo.comment}") public class ${tableInfo.name} implements Serializable { private static final long serialVersionUID = ${tool.serial()};
#foreach($column in $tableInfo.pkColumn) @ApiModelProperty(value = "${column.comment}") @TableId(value = "${column.obj.name}", type = IdType.AUTO) private ${tool.getClsNameByFullName($column.type)} ${column.name}; #end
#foreach($column in $tableInfo.otherColumn) @ApiModelProperty(value = "${column.comment}") #if($column.name.equals("createTime")) @TableField(value = "${column.obj.name}", fill = FieldFill.INSERT) private ${tool.getClsNameByFullName($column.type)} ${column.name}; #elseif($column.name.equals("updateTime")) @TableField(value = "${column.obj.name}", fill = FieldFill.INSERT_UPDATE) private ${tool.getClsNameByFullName($column.type)} ${column.name}; #elseif($column.name.equals("deleted")) @ApiModelProperty(value = "是否删除") @TableLogic private ${tool.getClsNameByFullName($column.type)} ${column.name}; #else @TableField(value = "${column.obj.name}") private ${tool.getClsNameByFullName($column.type)} ${column.name}; #end #end }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| #set($author = "SerMs") #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 org.springframework.web.bind.annotation.*;
@RestController @RequestMapping("$!tool.firstLowerCase($tableInfo.name)") public class $!{tableName} { }
|