An overview,
EasyCode can automatically generate: Entity, DAO, Service, serviceImpl, Controller and so on
Official document: gitee.com/makejava/Ea…
Second, the installation
1. Install the plug-in
Search in File –> setting –> Plugins –> Marketplaceeasy code
, click Install to Install, after the installation, you need to restart IDEA
2. Connect to the database
(a) Open Database, if the right sidebar of IDEA is available, you can directly open the Database, if not, click the following way:(b) Click the plus sign, select the data source, here select MySql, other database configuration is similar(c) Add the JDBC driver and select the location of the driver JAR package(d) Configure the database
Generate code
After connecting to the database successfully, select the Table to be generated, right-click, choose EasyCode –> Generate Code to Generate Code, Config Table to configure the TableClick Generate Code and the following window will pop up:
Four, configure EasyCode
Configure EasyCode in File –> Settings –> Other Settings –> EasyCode
- Type Mapper: Maps field types in the database (which support regular expressions) to attribute types of Java objects
- After EasyCode is installed, two sets of templates are automatically installed. One is the default, and the other is MybatisPuls (which will be modified a little later). Spring Data JPA templates are provided in the next chapter
- Global Config: Global configuration. You can reference a Global variable in a template
Spring Data JPA template
To generate entity classes, see Spring Data JPA Detailed Introduction
1. The controller class
$tableName = $tool.append($tableName = $tool.append($tableinfo.name, $tableName)"Controller") ## set the callback $! callback.setFileName($tool.append($tableName,".java")) $! callback.setSavePath($tool.append($tableInfo.savePath,"/controller")) ## get the primary key #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}) table control layer * *@author$! author *@since$! time.currTime() */
@RestController
@RequestMapping("$! tool.firstLowerCase($tableInfo.name)")
public class $!{tableName} {
/**
* 服务对象
*/
@Resource
private$! {tableInfo.name}Service $! tool.firstLowerCase($tableInfo.name)Service;/** * Query a single entry of data by primary key **@param* id primary key@returnSingle piece of data */
@GetMapping("selectOne")
public$! {tableInfo.name} selectOne($! pk.shortType id) {return this. $! {tool.firstLowerCase($tableInfo.name)}Service.queryById(id); }}Copy the code
2. The service interface
$tableName = $tool.append($tableName = $tool.append($tableinfo.name, $tableName)"Service") ## set the callback $! callback.setFileName($tool.append($tableName,".java")) $! callback.setSavePath($tool.append($tableInfo.savePath,"/service")) ## get the primary key #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}) table service interface * *@author zry
* @since$! time.currTime() */
public interface $!{tableName} { $! {tableInfo.name} queryById($! pk.shortType $! pk.name); Page<$! {tableInfo.name}> queryAllByLimit(int offset, intlimit); $! {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(); }Copy the code
3. ServiceImpl implementation class
$tableName = $tool.append($tableName = $tool.append($tableinfo.name, $tableName)"ServiceImpl") ## set the callback $! callback.setFileName($tool.append($tableName,".java")) $! callback.setSavePath($tool.append($tableInfo.savePath,"/service/impl")) ## get the primary key #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}) table service implementation class * *@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
publicList<$! {tableInfo.name}> getall() {return this. $! {tool.firstLowerCase($! {tableInfo.name})}Dao.findAll(); }@Override
publicPage<$! {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; }}Copy the code
4. The dao interface
$tableName = $tool.append($tableName = $tool.append($tableinfo.name, $tableName)"Dao") ## set the callback $! callback.setFileName($tool.append($tableName,".java")) $! callback.setSavePath($tool.append($tableInfo.savePath,"/dao")) ## get the primary key #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}) table database access layer * *@author zry
* @since$! time.currTime() */
public interface $!{tableName} extends JpaRepository<$! {tableInfo.name} ,$! pk.shortType>{ }Copy the code
6. Modify MyBatisPlus template
Before modifying the template, you are advised to make a backup copy of the original template.
1. Entity class
The old Entity generated a lot of getters and setters, and here I use Lombok to simplify the code
## import macro definition $! Define ## save(macros)"/entity".".java"#setPackageSuffix() #setPackageSuffix()"entity") ## Automatically import packages (global variables) $! autoImport ##import com.baomidou.mybatisplus.extension.activerecord.Model;
import java.io.Serializable;
importlombok.Data; # #importcom.baomidou.mybatisplus.annotation.IdType; # #importcom.baomidou.mybatisplus.annotation.TableId; #tableComment(macro definition)"Table Entity Class")
@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) ##/** ## * get primary key ## * ## # *@returnPrimary key ## */# #@Override# #protected Serializable pkVal(a) {
## return this. $! column.name; # # # # #}break
###end
}
Copy the code
2. The table starts with “t_”
The original template: When a table begins with “t_”, all generated classes and interfaces begin with T. Easy Code –> Global Config –> define; add the following Code to the beginning of the file:
## Remove table t_ prefix #if($tableInfo.obj.name.startsWith("t_")) $! tableInfo.setName($tool.getClassName($tableInfo.obj.name.substring(2)))
#end
Copy the code