文章目錄
一、概述
EasyCode可以自動根據表格生成:entity,dao,service,serviceImpl,controller等等
官方文檔:https://gitee.com/makejava/EasyCode/wikis/pages
二、安裝
1. 安裝插件
在File --> setting --> Plugins --> Marketplace 中,搜索 easy code
,點擊Install進行安裝,安裝之後需要重啓IDEA
2. 連接數據庫
(a)打開Database,如果IDEA右側邊欄有的話可以直接點開,如果沒有則通過如下方式點開:
(b)點擊加號,選擇數據源,這裏選擇MySql,其它數據庫配置類似
(c)添加JDBC驅動,選擇驅動jar包的位置
(d)配置數據庫
三、生成代碼
連接成功數據庫後,選擇要生成的表,鼠標右擊,選擇EasyCode --> Generate Code用來生成代碼,Config Table用來配置表
點擊Generate Code後,會彈出如下的窗口:
四、配置EasyCode
在 File --> Settings --> Other Settings --> Easy Code 中對EasyCode進行配置
- Type Mapper:類型映射,配置數據庫中字段類型(支持正則表達式)與java對象的屬性類型的映射
- Template Setting:配置模板信息,默認在安裝了EasyCode後,自動帶有兩套模板,一套是默認,一套是MybatisPuls(後面會稍做修改),下一章提供了Spring Data JPA模板
- Global Config:全局配置,可以在模板中引用全局變量
五、Spring Data JPA模板
生成實體類請參考:Spring Data JPA詳細入門
1. 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}.entity.$!{tableInfo.name};
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
/**
* $!{tableInfo.comment}($!{tableInfo.name})表控制層
*
* @author $!author
* @since $!time.currTime()
*/
@RestController
@RequestMapping("$!tool.firstLowerCase($tableInfo.name)")
public class $!{tableName} {
/**
* 服務對象
*/
@Resource
private $!{tableInfo.name}Service $!tool.firstLowerCase($tableInfo.name)Service;
/**
* 通過主鍵查詢單條數據
*
* @param id 主鍵
* @return 單條數據
*/
@GetMapping("selectOne")
public $!{tableInfo.name} selectOne($!pk.shortType id) {
return this.$!{tool.firstLowerCase($tableInfo.name)}Service.queryById(id);
}
}
2. 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 $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import java.util.List;
import org.springframework.data.domain.Page;
/**
* $!{tableInfo.comment}($!{tableInfo.name})表服務接口
*
* @author zry
* @since $!time.currTime()
*/
public interface $!{tableName} {
$!{tableInfo.name} queryById($!pk.shortType $!pk.name);
Page<$!{tableInfo.name}> queryAllByLimit(int offset, int limit);
$!{tableInfo.name} insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
$!{tableInfo.name} update($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
boolean deleteById($!pk.shortType $!pk.name);
List<$!{tableInfo.name}> getall();
}
3. serviceImpl實現類
##定義初始變量
#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 $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import $!{tableInfo.savePackageName}.dao.$!{tableInfo.name}Dao;
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import javax.annotation.Resource;
import java.util.List;
import org.springframework.data.domain.Page;
/**
* $!{tableInfo.comment}($!{tableInfo.name})表服務實現類
*
* @author $!author
* @since $!time.currTime()
*/
@Service("$!tool.firstLowerCase($!{tableInfo.name})Service")
public class $!{tableName} implements $!{tableInfo.name}Service {
@Resource
private $!{tableInfo.name}Dao $!tool.firstLowerCase($!{tableInfo.name})Dao;
@Override
public $!{tableInfo.name} queryById($!pk.shortType $!pk.name) {
return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.getOne($!pk.name);
}
@Override
public List<$!{tableInfo.name}> getall() {
return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.findAll();
}
@Override
public Page<$!{tableInfo.name}> queryAllByLimit(int offset, int limit) {
return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.findAll(PageRequest.of((offset-1)
*limit,limit));
}
@Override
public $!{tableInfo.name} insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {
return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.save($!tool.firstLowerCase($!{tableInfo.name}));
}
@Override
public $!{tableInfo.name} update($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {
return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.save($!tool.firstLowerCase($!{tableInfo.name}));
}
@Override
public boolean deleteById($!pk.shortType $!pk.name) {
try{
this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.deleteById($!pk.name) ;
}catch (Exception ex){
return false;
}
return true;
}
}
4. dao接口
##定義初始變量
#set($tableName = $tool.append($tableInfo.name, "Dao"))
##設置回調
$!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 $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* $!{tableInfo.comment}($!{tableInfo.name})表數據庫訪問層
*
* @author zry
* @since $!time.currTime()
*/
public interface $!{tableName} extends JpaRepository<$!{tableInfo.name} ,$!pk.shortType>{
}
六、MyBatisPlus模板修改
修改時,建議先把原來的模板複製一份做備份。
1. entity實體類
原來的entity會生成大量的getter和setter,這裏使用lombok
來簡化代碼
##導入宏定義
$!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} 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
}
2. 表以"t_"開頭
原來的模板:當表以"t_
"開頭時,生成的所有類和接口,都會以T
開頭。例如表 t_user
生成的實體類爲 TUser
修改:找到全局配置 Easy Code --> Global Config --> define
文件,將下面的代碼添加到文件開關即可(代碼也比較好懂,若是其它前綴,稍微修改一下即可):
##去掉表的t_前綴
#if($tableInfo.obj.name.startsWith("t_"))
$!tableInfo.setName($tool.getClassName($tableInfo.obj.name.substring(2)))
#end