EasyCode 是一个可以从数据库表中快速生成代码的插件,但是原有的默认配置已经不太适用了。以下配置是笔者自用的,记录下来作备忘,也供大家借鉴。
涉及到的第三方库及版本:
第三方库 | 版本 |
---|
springboot | 2.6.1 | lombok | - | springdoc-openapi-ui | 1.5.13 | hutool | 5.7.17 |
模板配置
entity
##导入宏定义
$!init
$!define
##保存文件(宏定义)
#save("/pojo", ".java")
##包路径(宏定义)
#setPackageSuffix("pojo")
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.experimental.Accessors;
import java.io.Serializable;
##自动导入包(全局变量)
$!autoImport
##表注释(宏定义)
#tableComment("表实体类")
@Data
@Accessors(chain = true)
@Schema(title = "$!{tableInfo.name}实体类")
@TableName(value = "tb_$!tool.firstLowerCase($!tableInfo.name)")
public class $!{tableInfo.name} implements Serializable {
#foreach($column in $tableInfo.fullColumn)
#if(${column.comment})#end
#{if}($column.name=="id")@TableId(type = IdType.AUTO)
#{end}@TableField(value = "$!{tool.hump2Underline($column.name)}")
@Schema(title = "#if(${column.comment})${column.comment}#end")
private $!{tool.getClsNameByFullName($column.type)} $!{column.name};
#end
}
dao
##导入宏定义
$!init
$!define
##设置表后缀(宏定义)
#setTableSuffix("Mapper")
##保存文件(宏定义)
#save("/dao", "Mapper.java")
##包路径(宏定义)
#setPackageSuffix("dao")
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import $!{tableInfo.savePackageName}.pojo.$!tableInfo.name;
##表注释(宏定义)
#tableComment("表数据库访问层")
public interface $!{tableName} extends BaseMapper<$!tableInfo.name> {
}
service
##导入宏定义
$!init
$!define
##设置表后缀(宏定义)
#setTableSuffix("Service")
##保存文件(宏定义)
#save("/service", "Service.java")
##定义实体对象名
#set($entityName = $!tool.firstLowerCase($!tableInfo.name))
##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
#set($pk = $tableInfo.pkColumn.get(0))
#end
##包路径(宏定义)
#setPackageSuffix("service")
import com.baomidou.mybatisplus.extension.service.IService;
import $!{tableInfo.savePackageName}.pojo.$!tableInfo.name;
import top.zainzh.entity.PageResult;
import java.util.List;
##表注释(宏定义)
#tableComment("表服务接口")
public interface $!{tableName} extends IService<$!tableInfo.name> {
PageResult<$!tableInfo.name> findPage(int page, int size);
PageResult<$!tableInfo.name> findPage($!tableInfo.name $entityName, int page, int size);
$!tableInfo.name selectOne($!pk.shortType $!pk.name);
void insert($!tableInfo.name $!entityName);
void update($!tableInfo.name $!entityName);
void delete($!pk.shortType $!pk.name);
List<$!tableInfo.name> selectList($!tableInfo.name $!entityName);
List<$!tableInfo.name> selectAll();
}
serviceImpl
##导入宏定义
$!init
$!define
##设置表后缀(宏定义)
#setTableSuffix("ServiceImpl")
##保存文件(宏定义)
#save("/service/impl", "ServiceImpl.java")
##定义实体对象名
#set($entityName = $!tool.firstLowerCase($!tableInfo.name))
##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
#set($pk = $tableInfo.pkColumn.get(0))
#end
##包路径(宏定义)
#setPackageSuffix("service.impl")
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import $!{tableInfo.savePackageName}.dao.$!{tableInfo.name}Mapper;
import $!{tableInfo.savePackageName}.pojo.$!{tableInfo.name};
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import top.zainzh.entity.PageResult;
import javax.annotation.Resource;
import java.util.List;
##表注释(宏定义)
#tableComment("表服务实现类")
@Service("$!tool.firstLowerCase($tableInfo.name)Service")
public class $!{tableName} extends ServiceImpl<$!{tableInfo.name}Mapper, $!{tableInfo.name}> implements $!{tableInfo.name}Service {
@Resource
private $!{tableInfo.name}Mapper $!{entityName}Mapper;
@Override
public PageResult<$!tableInfo.name> findPage(int page, int size) {
IPage<$!tableInfo.name> iPage = this.page(new Page<>(page, size), new QueryWrapper<>());
return new PageResult<>(iPage.getTotal(), iPage.getRecords());
}
@Override
public PageResult<$!tableInfo.name> findPage($!tableInfo.name $entityName, int page, int size) {
IPage<$!tableInfo.name> iPage = this.page(new Page<>(page, size), this.createQueryWrapper($entityName));
return new PageResult<>(iPage.getTotal(), iPage.getRecords());
}
@Override
public $!tableInfo.name selectOne($!pk.shortType $!pk.name) {
return this.getById($!pk.name);
}
@Override
public void insert($!tableInfo.name $!entityName) {
this.save($!entityName);
}
@Override
public void update($!tableInfo.name $!entityName) {
this.updateById($!entityName);
}
@Override
public void delete($!pk.shortType $!pk.name) {
this.removeById($!pk.name);
}
@Override
public List<$!tableInfo.name> selectList($!tableInfo.name $!entityName) {
QueryWrapper<$!tableInfo.name> queryWrapper = this.createQueryWrapper($!entityName);
return this.list(queryWrapper);
}
@Override
public List<$!tableInfo.name> selectAll() {
return this.list(new QueryWrapper<>());
}
public QueryWrapper<$!tableInfo.name> createQueryWrapper($!tableInfo.name $!entityName){
QueryWrapper<$!tableInfo.name> queryWrapper = new QueryWrapper<>();
if(null != $!entityName){
#foreach($column in $tableInfo.fullColumn)
if(!StrUtil.hasEmpty(StrUtil.toString($!{entityName}.get$!{tool.firstUpperCase($column.name)}()))){
queryWrapper.eq("$column.name", $!{entityName}.get$!{tool.firstUpperCase($column.name)}());
}
#end}
return queryWrapper;
}
}
controller
此处的PageResult 与Result 类为自封装的结果集类。
##导入宏定义
$!init
$!define
##设置表后缀(宏定义)
#setTableSuffix("Controller")
##保存文件(宏定义)
#save("/controller", "Controller.java")
##定义服务名
#set($serviceName = $!tool.append($!tool.firstLowerCase($!tableInfo.name), "Service"))
##定义实体对象名
#set($entityName = $!tool.firstLowerCase($!tableInfo.name))
##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
#set($pk = $tableInfo.pkColumn.get(0))
#end
##包路径(宏定义)
#setPackageSuffix("controller")
import cn.hutool.http.HttpStatus;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;
import $!{tableInfo.savePackageName}.pojo.$!tableInfo.name;
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import top.zainzh.entity.PageResult;
import top.zainzh.entity.Result;
import javax.annotation.Resource;
import java.util.List;
##表注释(宏定义)
#tableComment("表控制层")
@Tag(name = "$!{tableName} API")
@RestController
@RequestMapping("/$!tool.firstLowerCase($!tableInfo.name)")
@SuppressWarnings("rawtypes")
public class $!{tableName} {
@Resource
private $!{tableInfo.name}Service $!{serviceName};
@Operation(summary = "分页查询所有数据", tags = "$!{tableName} API")
@GetMapping(value = "/search/{page}/{size}")
public Result<PageResult<$!{tableInfo.name}>> findPage(@PathVariable int page, @PathVariable int size) {
PageResult<$!{tableInfo.name}> pageResult = $!{serviceName}.findPage(page, size);
return new Result<>(true, HttpStatus.HTTP_OK, "查询成功", pageResult);
}
@Operation(summary = "按条件分页查询", tags = "$!{tableName} API")
@PostMapping(value = "/search/{page}/{size}")
public Result<PageResult<$!{tableInfo.name}>> findPage(@RequestBody(required = false) $!{tableInfo.name} $entityName, @PathVariable int page, @PathVariable int size) {
PageResult<$!{tableInfo.name}> pageResult = $!{serviceName}.findPage($entityName, page, size);
return new Result<>(true, HttpStatus.HTTP_OK, "查询成功", pageResult);
}
@Operation(summary = "通过主键查询单条数据", tags = "$!{tableName} API")
@GetMapping("/{$!pk.name}")
public Result<$!{tableInfo.name}> selectOne(@PathVariable $!pk.shortType $!pk.name) {
$!{tableInfo.name} $!entityName = $!{serviceName}.selectOne($!pk.name);
return new Result<>(true, HttpStatus.HTTP_OK, "查询成功", $!entityName);
}
@Operation(summary = "新增数据", tags = "$!{tableName} API")
@PostMapping
public Result insert(@RequestBody $!tableInfo.name $!entityName) {
$!{serviceName}.insert($!entityName);
return new Result(true, HttpStatus.HTTP_OK, "添加成功");
}
@Operation(summary = "修改数据", tags = "$!{tableName} API")
@PutMapping(value = "/{$!pk.name}")
public Result update(@RequestBody $!tableInfo.name $!entityName, @PathVariable $!pk.shortType $!pk.name) {
$!{entityName}.setId($!pk.name);
$!{serviceName}.update($!entityName);
return new Result(true, HttpStatus.HTTP_OK, "修改成功");
}
@Operation(summary = "删除数据", tags = "$!{tableName} API")
@DeleteMapping(value = "/{$!pk.name}" )
public Result delete(@PathVariable $!pk.shortType $!pk.name) {
$!{serviceName}.delete($!pk.name);
return new Result(true, HttpStatus.HTTP_OK, "删除成功");
}
@Operation(summary = "查询所有", tags = "$!{tableName} API")
@GetMapping
public Result<List<$!tableInfo.name>> selectAll() {
List<$!tableInfo.name> list = $!{serviceName}.selectAll();
return new Result<>(true, HttpStatus.HTTP_OK, "查询成功", list);
}
@Operation(summary = "按条件查询", tags = "$!{tableName} API")
@PostMapping("/search")
public Result<List<$!tableInfo.name>> selectList($!tableInfo.name $!entityName) {
List<$!tableInfo.name> list = $!{serviceName}.selectList($!entityName);
return new Result<>(true, HttpStatus.HTTP_OK, "查询成功", list);
}
}
|