EasyCode插件使用及模板參考
1、介紹安裝
Easycode是idea的一個插件,可以直接對數據的表生成entity、controller、service、dao、mapper無需任何編碼,簡單而強大。
我這里的話是已經那裝好了。
建議大家在安裝一個插件,叫做Lombok。
Lombok能通過注解的方式,在編譯時自動為屬性生成構造器、getter/setter、equals、hashcode、toString方法。出現的神奇就是在源碼中沒有getter和setter方法,但是在編譯生成的字節碼文件中有getter和setter方法。
2、在IDEA配置連接數據庫
在這個之前,新建一個Springboot項目,這個應該是比較簡單的。
建好SpringBoot項目之后,如下圖所示,配置數據源,我這里是已經配置完成的。
3、開始生成代碼
在這個里面找到你想生成的表,然后右鍵,就會出現如下所示的截面。這里點擊后會選擇生成的文件類型,以及生成文件的位置。
如下圖所示,這里是,我們的選擇方式:
注意:我們在模板中配置過,這里選擇適合,Package選項是選擇到倒數第二層的位置,下面模板會自動找到所在位置。
4、模板配置
我們在這里進行配置,模板配置語言是velocity語言,這里配置的是SPring+通用Mapper
4.1entity層配置
##導入宏定義
$!define
##保存文件(宏定義)
#save("/entity", ".java")
##包路徑(宏定義)
#setPackageSuffix("entity")
##自動導入包(全局變量)
$!autoImport
import tk.mybatis.mapper.annotation.NameStyle;
import tk.mybatis.mapper.code.Style;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.Data;
##表注釋(宏定義)
#tableComment("表實體類")
@Data
@NameStyle(Style.normal)
@Table(name = "$!{tableInfo.name}")
public class $!{tableInfo.name}{
#foreach($column in $tableInfo.fullColumn)
#if(${column.comment})//${column.comment}#end
private $!{tool.getClsNameByFullName($column.type)} $!{column.name};
#end
}
注意:
$!define 這個我們不用關注,默認即可
save("/entity", ".java"),這里是定義了實體類的所在包名,后綴是.java
setPackageSuffix("entity") ,這里是包路徑
tableComment("表實體類") 是一種注釋
在配置界面有着詳細的方法注釋,可以看着注釋進行更加詳細的配置。
4.2dao層配置
##定義初始變量
#set($tableName = $tool.append($tableInfo.name, "Mapper"))
##設置回調
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/dao"))
##拿到主鍵
#if(!$tableInfo.pkColumn.isEmpty())
#set($pk = $tableInfo.pkColumn.get(0))
#end
#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}dao;
import tk.mybatis.mapper.common.Mapper;
import com.ctbt.entity.$!{tableInfo.name};
/**
* $!{tableInfo.comment}($!{tableInfo.name})表數據庫訪問層
*
* @author $!author
* @since $!time.currTime()
*/
public interface $!{tableName} extends Mapper<$!{tableInfo.name}>{
}
注意:
1、這里我們注意前三行的配置,第一行是一個字符串連接操作得到,NameMapper,這種樣子;設置文件名字,后綴是.java;設置保存路徑
2、注意接下來是拿到主鍵,這個拿到的主鍵$pk不能直接用
3、接下來是配置注釋信息,我們也可以使用#tableComment("表實體類")來代替這么多行內容
4.3Service層配置
##定義初始變量
#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}.entity.$!{tableInfo.name};
import $!{tableInfo.savePackageName}.dao.$!{tableInfo.name}Mapper;
import cn.hutool.core.collection.CollUtil;
import com.ctbt.enums.ExceptionEnum;
import com.ctbt.exception.CtbtException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
/**
* $!{tableInfo.comment}($!{tableInfo.name})表服務接口
*
* @author $!author
* @since $!time.currTime()
*/
@Service
@Transactional
public class $!{tableName} {
@Autowired(required = false)
private $!{tableInfo.name}Mapper $!{tool.firstLowerCase($tableInfo.name)}Mapper;
/**
*根據參數查詢數據,參數為空時,查詢所有
* @param $!tool.firstLowerCase($!tableInfo.name)
* @return
*/
public List<$!{tableInfo.name}> queryAll($!{tableInfo.name} $!tool.firstLowerCase($!tableInfo.name)) {
List<$!{tableInfo.name}> select = $!{tool.firstLowerCase($tableInfo.name)}Mapper.select($!tool.firstLowerCase($!tableInfo.name));
if (CollUtil.isEmpty(select)) {
throw new CtbtException(ExceptionEnum.NOT_FOUND_OR_FOUND_ERROR);
}
return select;
}
/**
* 插入數據
* @param $!tool.firstLowerCase($!tableInfo.name)
*/
public void add($!{tableInfo.name} $!tool.firstLowerCase($!tableInfo.name)) {
if($!{tool.firstLowerCase($tableInfo.name)}Mapper.selectByPrimaryKey($!{tool.firstLowerCase($!tableInfo.name)}.getId())!=null){
throw new CtbtException(ExceptionEnum.DATA_ALREADY_EXISTS);
}
int insert = $!{tool.firstLowerCase($tableInfo.name)}Mapper.insert($!tool.firstLowerCase($!tableInfo.name));
if (insert != 1) {
throw new CtbtException(ExceptionEnum.INSERT_ERROR);
}
}
/**
* 更新數據
* @param $!tool.firstLowerCase($!tableInfo.name)
*/
public void update($!{tableInfo.name} $!tool.firstLowerCase($!tableInfo.name)) {
int i = $!{tool.firstLowerCase($tableInfo.name)}Mapper.updateByPrimaryKey($!tool.firstLowerCase($!tableInfo.name));
if (i != 1) {
throw new CtbtException(ExceptionEnum.UPDATE_ERROR);
}
}
#foreach($column in $tableInfo.pkColumn)
/**
* 根據主鍵刪除數據
* @param $column.name
*/
public void delete($!{tool.getClsNameByFullName($column.type)} $column.name) {
int i = $!{tool.firstLowerCase($tableInfo.name)}Mapper.deleteByPrimaryKey($column.name);
if (i != 1) {
throw new CtbtException(ExceptionEnum.DELETE_ERROR);
}
}
#break
#end
}
注意:
注意前三行以及開頭注釋不再贅述
下面是CRUD操作,注意刪除操作時候,這里是選擇一個循環操作,因為這里拿到的主鍵,設定不是一個,而默認是多個,所以我們用循環拿主鍵。
4.4Controller層配置
##定義初始變量
#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}.entity.$!{tableInfo.name};
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* $!{tableInfo.comment}($!{tableInfo.name})表控制層
*
* @author $!author
* @since $!time.currTime()
*/
@CrossOrigin
@RestController
@RequestMapping("$!tool.firstLowerCase($tableInfo.name)")
public class $!{tableName} {
/**
* 服務對象
*/
@Autowired
private $!{tableInfo.name}Service $!tool.firstLowerCase($tableInfo.name)Service;
/**
* 根據屬性查詢 (當參數為空時候)查詢所有
* @param $!tool.firstLowerCase($!tableInfo.name)
* @return
*/
@GetMapping("list")
public ResponseEntity<List<$!{tableInfo.name}>> query(@RequestBody(required = false) $!tableInfo.name $!tool.firstLowerCase($!tableInfo.name)){
return ResponseEntity.ok($!{tool.firstLowerCase($!{tableInfo.name})}Service.queryAll($!tool.firstLowerCase($!tableInfo.name)));
}
/**
* 插入一條數據
* @param $!tool.firstLowerCase($!tableInfo.name)
* @return
*/
@PostMapping("add")
public ResponseEntity<List<$!{tableInfo.name}>> add(@RequestBody $!{tableInfo.name} $!tool.firstLowerCase($tableInfo.name)){
$!{tool.firstLowerCase($tableInfo.name)}Service.add($!tool.firstLowerCase($tableInfo.name));
return ResponseEntity.ok($!{tool.firstLowerCase($!{tableInfo.name})}Service.queryAll(new $!{tableInfo.name}()));
}
/**
* 根據主鍵更新數據
* @param $!tool.firstLowerCase($!tableInfo.name)
* @return
*/
@PutMapping("update")
public ResponseEntity<List<$!{tableInfo.name}>> update(@RequestBody $!{tableInfo.name} $!tool.firstLowerCase($tableInfo.name)){
$!{tool.firstLowerCase($tableInfo.name)}Service.update($!tool.firstLowerCase($tableInfo.name));
return ResponseEntity.ok($!{tool.firstLowerCase($!{tableInfo.name})}Service.queryAll(new $!{tableInfo.name}()));
}
#foreach($column in $tableInfo.pkColumn)
/**
* 根據主鍵刪除
* @param $column.name
* @return
*/
@DeleteMapping("del/{$column.name}")
public ResponseEntity<List<$!{tableInfo.name}>> delete(@PathVariable("$column.name") $!{tool.getClsNameByFullName($column.type)} $column.name){
$!{tool.firstLowerCase($tableInfo.name)}Service.delete($column.name);
return ResponseEntity.ok($!{tool.firstLowerCase($!{tableInfo.name})}Service.queryAll(new $!{tableInfo.name}()));
}
#break
#end
}
注意:此處配置和Service層差別不大。
總結
這里,我們的配置不再詳細描述了,根據模板配置的頁面可以很好的可以理解,這里提一下其中一個語法,這里給出一個對比
1、
$!{tool.firstLowerCase($tableInfo.name)}Service.delete($column.name); 正確寫法
$!tool.firstLowerCase($tableInfo.name)Service.delete($column.name); 錯誤寫法
這里第一行和第二行的差別是缺少了一個{},因此要格外注意這個點
2、
private $!{tool.getClsNameByFullName($column.type)} $!{column.name};
private $tool.getClsNameByFullName($column.type) $column.name;
這里看到第一行相對第二行缺少一個!,這里是沒有影響的,個人對這個原因不是很清楚,也沒有去查velocity的語法,各位懂這個的可以指出,多謝!