当前位置: 移动技术网 > IT编程>开发语言>Java > idea的easyCode的 MybatisPlus模板的配置详解

idea的easyCode的 MybatisPlus模板的配置详解

2020年09月09日  | 移动技术网IT编程  | 我要评论
easycode 插件easycode 插件 是一款根据表结构生成代码的很方便的idea插件, 强烈推荐. 并且可以自定义模板来控制生成的类我在使用的过程中发现一些问题,现在把解决办法记录下来, 我主

easycode 插件

easycode 插件 是一款根据表结构生成代码的很方便的idea插件, 强烈推荐. 并且可以自定义模板来控制生成的类
 我在使用的过程中发现一些问题,现在把解决办法记录下来, 我主要使用的是插件自带的mybatisplus模板

1. 生成的代码中有大量的get set方法

lombok 插件是个好东西, 我删除了模板中的get和set方法, 添加了lombok 的注解, '

2. 如果数据库中的表都有前缀"t_" 导致生成的类名中都有一个前缀 “t”

这个问题困扰我很久,改了各种模板 , 最后发现把init文件的第一行代码复制到define文件的第一行就可以, init文件根本就没有用.

3, 生成的类中没有dto对象

直接把entity模板文件复制一份改改就有了

下面分享下我修改后的模板

template setting 配置项 group name : mybatisplus

如果没有mybatisplus 的group name, 可以新增一个

dto文件

##导入宏定义
$!define

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

##包路径(宏定义)
#setpackagesuffix("dto")


##自动导入包(全局变量)
$!autoimport
##import com.baomidou.mybatisplus.extension.activerecord.model;
import java.io.serializable;
import lombok.data;
##import com.baomidou.mybatisplus.annotation.idtype;
##import com.baomidou.mybatisplus.annotation.tableid;

##表注释(宏定义)
#tablecomment("表实体类")
@data
@suppresswarnings("serial")
public class $!{tableinfo.name}dto implements serializable {
 
#foreach($column in $tableinfo.fullcolumn)
  #if(${column.comment})/**${column.comment}*/#end

  private $!{tool.getclsnamebyfullname($column.type)} $!{column.name};
#end

###foreach($column in $tableinfo.fullcolumn)
##  #getsetmethod($column)
###end

###foreach($column in $tableinfo.pkcolumn)
##  /**
##   * 获取主键值
##   *
##   * @return 主键值
##   */
##  @override
##  protected serializable pkval() {
##    return this.$!column.name;
##  }
##  #break
###end
}

controller 文件

##导入宏定义
$!define

##设置表后缀(宏定义)
#settablesuffix("controller")

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

##包路径(宏定义)
#setpackagesuffix("controller")

##定义服务名
#set($servicename = $!tool.append($!tool.firstlowercase($!tableinfo.name), "service"))

##定义实体对象名
#set($entityname = $!tool.firstlowercase($!tableinfo.name))

import com.baomidou.mybatisplus.core.conditions.query.querywrapper;
import com.baomidou.mybatisplus.extension.api.apicontroller;
import com.baomidou.mybatisplus.extension.api.r;
import com.baomidou.mybatisplus.extension.plugins.pagination.page;
import $!{tableinfo.savepackagename}.entity.$!tableinfo.name;
import $!{tableinfo.savepackagename}.service.$!{tableinfo.name}service;
import org.springframework.web.bind.annotation.*;

import javax.annotation.resource;
import java.io.serializable;
import java.util.list;

##表注释(宏定义)
#tablecomment("表控制层")
@restcontroller
@requestmapping("$!tool.firstlowercase($!tableinfo.name)")
public class $!{tablename} extends apicontroller {
  /**
   * 服务对象
   */
  @resource
  private $!{tableinfo.name}service $!{servicename};

  /**
   * 分页查询所有数据
   *
   * @param page 分页对象
   * @param $!entityname 查询实体
   * @return 所有数据
   */
  @getmapping
  public r selectall(page<$!tableinfo.name> page, $!tableinfo.name $!entityname) {
    return success(this.$!{servicename}.page(page, new querywrapper<>($!entityname)));
  }

  /**
   * 通过主键查询单条数据
   *
   * @param id 主键
   * @return 单条数据
   */
  @getmapping("{id}")
  public r selectone(@pathvariable serializable id) {
    return success(this.$!{servicename}.getbyid(id));
  }

  /**
   * 新增数据
   *
   * @param $!entityname 实体对象
   * @return 新增结果
   */
  @postmapping
  public r insert(@requestbody $!tableinfo.name $!entityname) {
    return success(this.$!{servicename}.save($!entityname));
  }

  /**
   * 修改数据
   *
   * @param $!entityname 实体对象
   * @return 修改结果
   */
  @putmapping
  public r update(@requestbody $!tableinfo.name $!entityname) {
    return success(this.$!{servicename}.updatebyid($!entityname));
  }

  /**
   * 删除数据
   *
   * @param idlist 主键结合
   * @return 删除结果
   */
  @deletemapping
  public r delete(@requestparam("idlist") list<long> idlist) {
    return success(this.$!{servicename}.removebyids(idlist));
  }
}

serviceimpl 文件

##导入宏定义
$!define

##设置表后缀(宏定义)
#settablesuffix("serviceimpl")

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

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

import com.baomidou.mybatisplus.extension.service.impl.serviceimpl;
import $!{tableinfo.savepackagename}.dao.$!{tableinfo.name}dao;
import $!{tableinfo.savepackagename}.entity.$!{tableinfo.name};
import $!{tableinfo.savepackagename}.service.$!{tableinfo.name}service;
import org.springframework.stereotype.service;

##表注释(宏定义)
#tablecomment("表服务实现类")
@service("$!tool.firstlowercase($tableinfo.name)service")
public class $!{tablename} extends serviceimpl<$!{tableinfo.name}dao, $!{tableinfo.name}> implements $!{tableinfo.name}service {

}

service文件

##导入宏定义
$!define

##设置表后缀(宏定义)
#settablesuffix("service")

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

##包路径(宏定义)
#setpackagesuffix("service")

import com.baomidou.mybatisplus.extension.service.iservice;
import $!{tableinfo.savepackagename}.entity.$!tableinfo.name;

##表注释(宏定义)
#tablecomment("表服务接口")
public interface $!{tablename} extends iservice<$!tableinfo.name> {

}

dao文件

##导入宏定义
$!define

##设置表后缀(宏定义)
#settablesuffix("dao")

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

##包路径(宏定义)
#setpackagesuffix("dao")

import com.baomidou.mybatisplus.core.mapper.basemapper;
import $!{tableinfo.savepackagename}.entity.$!tableinfo.name;

##表注释(宏定义)
#tablecomment("表数据库访问层")
public interface $!{tablename} extends basemapper<$!tableinfo.name> {

}

entity 文件

##导入宏定义
$!define

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

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

##自动导入包(全局变量)
$!autoimport
import com.baomidou.mybatisplus.extension.activerecord.model;
import java.io.serializable;
import lombok.data;
import com.baomidou.mybatisplus.annotation.idtype;
import com.baomidou.mybatisplus.annotation.tableid;

##表注释(宏定义)
#tablecomment("表实体类")
@data
@suppresswarnings("serial")
public class $!{tableinfo.name} extends model<$!{tableinfo.name}> {
 @tableid(type = idtype.auto)
#foreach($column in $tableinfo.fullcolumn)
  #if(${column.comment})/**${column.comment}*/#end

  private $!{tool.getclsnamebyfullname($column.type)} $!{column.name};
#end

###foreach($column in $tableinfo.fullcolumn)
##  #getsetmethod($column)
###end

#foreach($column in $tableinfo.pkcolumn)
  /**
   * 获取主键值
   *
   * @return 主键值
   */
  @override
  protected serializable pkval() {
    return this.$!column.name;
  }
  #break
#end
}

global config 配置项 group name : default

下面的文件都是group name 为 default 的

mybatissupport 文件

##针对mybatis 进行支持,主要用于生成xml文件
#foreach($column in $tableinfo.fullcolumn)
  ##储存列类型
  $tool.call($column.ext.put("sqltype", $tool.getfield($column.obj.datatype, "typename")))
  #if($tool.newhashset("java.lang.string").contains($column.type))
    #set($jdbctype="varchar")
  #elseif($tool.newhashset("java.lang.boolean", "boolean").contains($column.type))
    #set($jdbctype="boolean")
  #elseif($tool.newhashset("java.lang.byte", "byte").contains($column.type))
    #set($jdbctype="byte")
  #elseif($tool.newhashset("java.lang.integer", "int", "java.lang.short", "short").contains($column.type))
    #set($jdbctype="integer")
  #elseif($tool.newhashset("java.lang.long", "long").contains($column.type))
    #set($jdbctype="integer")
  #elseif($tool.newhashset("java.lang.float", "float", "java.lang.double", "double").contains($column.type))
    #set($jdbctype="numeric")
  #elseif($tool.newhashset("java.util.date", "java.sql.timestamp", "java.time.instant", "java.time.localdatetime", "java.time.offsetdatetime", " java.time.zoneddatetime").contains($column.type))
    #set($jdbctype="timestamp")
  #elseif($tool.newhashset("java.sql.date", "java.time.localdate").contains($column.type))
    #set($jdbctype="timestamp")
  #else
    ##其他类型
    #set($jdbctype="other")
  #end
  $tool.call($column.ext.put("jdbctype", $jdbctype))
#end

##定义宏,查询所有列
#macro(allsqlcolumn)#foreach($column in $tableinfo.fullcolumn)$column.obj.name#if($velocityhasnext), #end#end#end

 autoimport 文件
##自动导入包(仅导入实体属性需要的包,通常用于实体类)
#foreach($import in $importlist)
import $!import;
#end

define 文件

##(velocity宏定义)
## 去掉表的t_前缀
$!tableinfo.setname($tool.getclassname($tableinfo.obj.name.replacefirst("t_","")))

##定义设置表名后缀的宏定义,调用方式:#settablesuffix("test")
#macro(settablesuffix $suffix)
  #set($tablename = $!tool.append($tableinfo.name, $suffix))
#end

##定义设置包名后缀的宏定义,调用方式:#setpackagesuffix("test")
#macro(setpackagesuffix $suffix)
  #if($suffix!="")package #end#if($tableinfo.savepackagename!="")$!{tableinfo.savepackagename}.#{end}$!suffix;
#end

##定义直接保存路径与文件名简化的宏定义,调用方式:#save("/entity", ".java")
#macro(save $path $filename)
  $!callback.setsavepath($tool.append($tableinfo.savepath, $path))
  $!callback.setfilename($tool.append($tableinfo.name, $filename))
#end

##定义表注释的宏定义,调用方式:#tablecomment("注释信息")
#macro(tablecomment $desc)
/**
 * $!{tableinfo.comment}($!{tableinfo.name})$desc
 *
 * @author $!author
 * @since $!time.currtime()
 */
#end

##定义get,set方法的宏定义,调用方式:#getsetmethod($column)
#macro(getsetmethod $column)

  public $!{tool.getclsnamebyfullname($column.type)} get$!{tool.firstuppercase($column.name)}() {
    return $!{column.name};
  }

  public void set$!{tool.firstuppercase($column.name)}($!{tool.getclsnamebyfullname($column.type)} $!{column.name}) {
    this.$!{column.name} = $!{column.name};
  }
#end

init 文件

##初始化区域

##去掉表的t_前缀
$!tableinfo.setname($tool.getclassname($tableinfo.obj.name.replacefirst("t_","")))

##参考阿里巴巴开发手册,pojo 类中布尔类型的变量,都不要加 is 前缀,否则部分框架解析会引起序列化错误
#foreach($column in $tableinfo.fullcolumn)
  #if($column.name.startswith("is") && $column.type.equals("java.lang.boolean"))
    $!column.setname($tool.firstlowercase($column.name.substring(2)))
  #end
#end

##实现动态排除列
#set($temp = $tool.newhashset("testcreatetime", "othercolumn"))
#foreach($item in $temp)
  #set($newlist = $tool.newarraylist())
  #foreach($column in $tableinfo.fullcolumn)
    #if($column.name!=$item)
    ##带有反回值的方法调用时使用$tool.call来消除返回值
      $tool.call($newlist.add($column))
    #end
  #end
##重新保存
  $tableinfo.setfullcolumn($newlist)
#end

##对importlist进行篡改
#set($temp = $tool.newhashset())
#foreach($column in $tableinfo.fullcolumn)
  #if(!$column.type.startswith("java.lang."))
  ##带有反回值的方法调用时使用$tool.call来消除返回值
    $tool.call($temp.add($column.type))
  #end
#end
##覆盖
#set($importlist = $temp)

到此这篇关于idea的easycode的 mybatisplus模板的配置详解的文章就介绍到这了,更多相关idea easycode mybatisplus配置内容请搜索移动技术网以前的文章或继续浏览下面的相关文章希望大家以后多多支持移动技术网!

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网