1. 程式人生 > 程式設計 >idea的easyCode的 MybatisPlus模板的配置詳解

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

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配置內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!