# mybatis-plus-generator **Repository Path**: OpenBodhi/mybatis-plus-generator ## Basic Information - **Project Name**: mybatis-plus-generator - **Description**: mybatis-plus-generator代码生成器 - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-05-10 - **Last Updated**: 2026-05-10 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## 💡 项目简介 基于 Spring Boot 4.x & MyBatis-Plus 的代码生成器,自定义controller模板,生成entity、mapper、mapper xml、service、service impl、controller等文件,可快速生成Restful API项目。如有需要可自定义其他产物。 ## 🛠️ 核心技术栈 本项目严格遵循现代化 Java 开发标准: | 组件 | 版本/说明 | 用途 | | :--- |:----------| :--- | | **Spring Boot** | `4.0+` | 核心框架 | | **MyBatis-Plus** | `3.5.16+` | 增强版 ORM 框架,提供强大的 CRUD 能力 | | **Dynamic Datasource** | `4.0+` | 强大的多数据源解决方案,支持读写分离 | | **SpringDoc** | `3.0+` | OpenAPI 3 接口文档生成器 (替代 Swagger) | | **Lombok** | - | 简化 Java Bean 冗余代码 | | **HikariCP** | 默认 | Spring Boot 4 内置高性能连接池 | | **Velocity** | `2.4+` | 代码生成器模板引擎 | ## 📦 环境要求 - **JDK:** 17 或以上 (推荐 25) - **Maven:** 3.6.3+ - **MySQL:** 8.0+ - **IDE:** IntelliJ IDEA (推荐) ## 🚀 快速开始 ### 1. 克隆项目 ```bash git clone https://gitee.com/OpenBodhi/mybatis-plus-generator.git ``` ### 2. 创建数据库表 > sql文件位于sql/db.sql > 创建数据库db_test,字符集为utf8mb4,排序字符集为utf8mb4_0900_ai_ci,执行sql文件。 ### 3. 生成代码 > 配置信息参考**CodeGenerator.java**的基础配置变量区域,根据自身需要配置。 打开CodeGenerator.java,配置数据库连接信息,执行 CodeGenerator程序生成代码 ```java // 数据库连接信息 private static final String DB_URL = "jdbc:mysql://localhost:3306/db_test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false"; private static final String DB_USERNAME = "root"; private static final String DB_PASSWORD = "123456"; ``` ### 4. 测试 打开application.yaml,配置数据库连接信息,执行DemoApplication运行。 ```yaml url: jdbc:mysql://localhost:3306/db_test?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai username: root password: 123456 ``` 项目启动后,可通过以下地址查看自动生成的 API 文档: 👉 Swagger UI: http://localhost:8080/swagger-ui.html ## 🗂️ 项目结构 ```md demo/ ├── sql/ │ └── db.sql // 测试SQL文件 ├── src/ │ ├── main/ │ │ ├── java/ │ │ │ └── com.example.demo/ │ │ │ ├── common/ │ │ │ │ └── r/ │ │ │ │ ├── R.java // 统一响应结果 │ │ │ │ └── RCode.java // 响应结果枚举类 │ │ │ ├── config/ │ │ │ │ └── MybatisPlusConfig.java // MyBatis-Plus配置,如分页配置 │ │ │ ├── CodeGenerator.java // MyBatis-Plus 代码生成器入口 │ │ │ └── DemoApplication.java // Spring Boot 启动类 │ │ └── resources/ │ │ ├── templates/ │ │ │ └── controller.java.vm // controller模板 │ │ └── application.yaml │ └── test/ └── pom.xml ``` ## 📦产物效果展示 ### entity User.java ```java package com.example.demo.entity; import com.baomidou.mybatisplus.annotation.FieldFill; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableLogic; import com.baomidou.mybatisplus.annotation.TableName; import io.swagger.v3.oas.annotations.media.Schema; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; import lombok.experimental.Accessors; import java.io.Serial; import java.io.Serializable; import java.math.BigDecimal; import java.time.LocalDate; import java.time.LocalDateTime; /** *
* *
* * @author YourName * @since 2026-05-10 */ @Data @Builder @NoArgsConstructor @AllArgsConstructor @TableName("tb_user") @Accessors(chain = true) @Schema(name = "User", description = "") public class User implements Serializable { @Serial private static final long serialVersionUID = 1L; /** * 主键ID */ @Schema(description = "主键ID") @TableId(value = "id", type = IdType.AUTO) private Long id; /** * 用户名 */ @TableField("username") @Schema(description = "用户名") private String username; /** * 年龄 */ @TableField("age") @Schema(description = "年龄") private Integer age; /** * 性别 (0-未知 1-男 2-女) */ @TableField("gender") @Schema(description = "性别 (0-未知 1-男 2-女)") private Integer gender; /** * 余额 (DECIMAL类型测试) */ @TableField("balance") @Schema(description = "余额 (DECIMAL类型测试)") private BigDecimal balance; /** * 体重 (FLOAT类型测试) */ @TableField("weight_float") @Schema(description = "体重 (FLOAT类型测试)") private Double weightFloat; /** * 身高 (DOUBLE类型测试) */ @TableField("height_double") @Schema(description = "身高 (DOUBLE类型测试)") private Double heightDouble; /** * 纬度 (科学计算/坐标) */ @TableField("latitude") @Schema(description = "纬度 (科学计算/坐标)") private Double latitude; /** * 头像URL */ @TableField("avatar") @Schema(description = "头像URL") private String avatar; /** * 邮箱 */ @TableField("email") @Schema(description = "邮箱") private String email; /** * 手机号 */ @TableField("phone") @Schema(description = "手机号") private String phone; /** * 生日 */ @TableField("birthday") @Schema(description = "生日") private LocalDate birthday; /** * 最后登录时间 */ @TableField("login_time") @Schema(description = "最后登录时间") private LocalDateTime loginTime; /** * 个人简介 */ @TableField("bio") @Schema(description = "个人简介") private String bio; /** * 扩展信息(JSON) */ @TableField("extra_info") @Schema(description = "扩展信息(JSON)") private String extraInfo; /** * 乐观锁版本号 */ @TableField("version") @Schema(description = "乐观锁版本号") private Integer version; /** * 备注 */ @TableField("remark") @Schema(description = "备注") private String remark; /** * 排序 */ @TableField("sort") @Schema(description = "排序") private Integer sort; /** * 角色状态(1-正常 0-禁用) */ @TableField("status") @Schema(description = "角色状态(1-正常 0-禁用)") private Integer status; /** * 创建者ID */ @TableField("create_by") @Schema(description = "创建者ID") private Long createBy; /** * 创建时间 */ @Schema(description = "创建时间") @TableField(value = "create_time", fill = FieldFill.INSERT) private LocalDateTime createTime; /** * 更新者ID */ @TableField("update_by") @Schema(description = "更新者ID") private Long updateBy; /** * 更新时间 */ @Schema(description = "更新时间") @TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE) private LocalDateTime updateTime; /** * 逻辑删除标识(1-已删除 0-未删除) */ @TableLogic @TableField("is_deleted") @Schema(description = "逻辑删除标识(1-已删除 0-未删除)") private Integer isDeleted; } ``` ### mapper UserMapper.java ```java package com.example.demo.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.example.demo.entity.User; import org.apache.ibatis.annotations.Mapper; /** ** Mapper 接口 *
* * @author YourName * @since 2026-05-10 */ @Mapper public interface UserMapper extends BaseMapper* 服务类 *
* * @author YourName * @since 2026-05-10 */ public interface IUserService extends IService* 服务实现类 *
* * @author YourName * @since 2026-05-10 */ @Service public class UserServiceImpl extends ServiceImpl* 测试用户 前端控制器 *
* * @author YourName * @since 2026-05-10 */ @RestController @RequiredArgsConstructor @RequestMapping("/api/v1/users") @Tag(name = "测试用户管理", description = "包含测试用户增删改查及分页查询接口") public class UserController { private final IUserService userService; @PostMapping @Operation(summary = "新增测试用户", description = "新增测试用户") public R