03. 使用代码生成器自动生成后端代码

1010 字约 3 分钟读完191 次阅读更新于 2026/5/3

在 Plugin 里面安装 EasyCode 插件

模板代码

Entity.java.vm

Repository.java,vm

## 1. 自动计算保存路径并设置文件名
$!{callback.setSavePath($tool.append($tableInfo.savePath, "/repository"))}
$!{callback.setFileName($tool.append($tableInfo.name, "Repository.java"))}

## 2. 获取基础包名
package $!{tableInfo.savePackageName}.repository;

import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

/**
 * $!{tableInfo.comment} 数据库操作层
 * 由 EasyCode 自动生成
 */
@Repository
public interface $!{tableInfo.name}Repository extends JpaRepository<$!{tableInfo.name}, Integer> {

}

Service.java.vm

Controller.java.vm

建表规则

  • 表必须有 ID 的主键 主键是自增的
  • 表必须写注释
  • 字段必须写注释
  • 字段不能是数据库或者 java 的关键字 class(clazz)
-- auto-generated definition
create table book
(
    id        int auto_increment comment 'ID'
        primary key,
    name      varchar(50)    null comment '名称',
    img       varchar(255)   null comment '封面',
    price     decimal(10, 2) null comment '价格',
    author    varchar(50)    null comment '作者',
    publisher varchar(50)    null comment '出版社',
    date      varchar(50)    null comment '出版日期'
)
    comment '图书信息';


decimal(10,2) -> BigDecimal

配置 EasyCode 的 Type Mapper