欢迎访问Spring Cloud中国社区

《重新定义Spring Cloud实战》由Spring Cloud中国社区倾力打造,基于Spring Cloud的Finchley.RELEASE版本,本书内容宽度足够广、深度足够深,而且立足于生产实践,直接从生产实践出发,包含大量生产实践的配置。欢迎加微信Software_King进群答疑,国内谁在使用Spring Cloud?欢迎登记

spring-boot-plus后台快速开发脚手架之代码生成器使用

geekidea · 1月前 · 49 ·

代码生成 Generator

spring-boot-plus在mybatis-plus基础上,新增param/vo等模板

拓展controller/service/mapper/xml,生成通用CRUD/分页方法

代码生成模板:spring-boot-plus/src/test/resources/templates

Purpose

数据库新建表,即可生成后台CRUD/分页基础代码,还有swagger!

官网地址:springboot.plus
GITHUB:https://github.com/geekidea/spring-boot-plus
GITEE:https://gitee.com/geekidea/spring-boot-plus
  1. _ _ _ _
  2. (_) | | | | | |
  3. ___ _ __ _ __ _ _ __ __ _ ______| |__ ___ ___ | |_ ______ _ __ | |_ _ ___
  4. / __| '_ \| '__| | '_ \ / _` |______| '_ \ / _ \ / _ \| __|______| '_ \| | | | / __|
  5. \__ \ |_) | | | | | | | (_| | | |_) | (_) | (_) | |_ | |_) | | |_| \__ \
  6. |___/ .__/|_| |_|_| |_|\__, | |_.__/ \___/ \___/ \__| | .__/|_|\__,_|___/
  7. | | __/ | | |
  8. |_| |___/ |_|
  9. :: Spring Boot :: (v2.1.6.RELEASE)
  10. :: Spring Boot Plus :: (v1.0.0.RELEASE)

代码生成步骤

  1. 创建数据库表,例如:sys_log

注意:记得加上表注释,字段列注释,方便生成类注释、swagger注释

  1. -- ----------------------------
  2. -- Table structure for sys_log
  3. -- ----------------------------
  4. DROP TABLE IF EXISTS `sys_log`;
  5. CREATE TABLE `sys_log` (
  6. `log_id` bigint(18) NOT NULL COMMENT '主键',
  7. `type` tinyint(1) NULL DEFAULT NULL COMMENT '类型',
  8. `content` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '内容',
  9. `create_id` bigint(18) NULL DEFAULT NULL COMMENT '创建人ID',
  10. `create_time` datetime(0) NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  11. PRIMARY KEY (`log_id`) USING BTREE
  12. ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '系统日志' ROW_FORMAT = Dynamic;
  1. 代码生成配置
    1. spring-boot-plus/src/test/java/io/geekidea/springbootplus/test/CodeGenerator.java
    代码生成器位置

2.1 修改数据库连接配置

  1. private static final String USER_NAME = "root";
  2. private static final String PASSWORD = "rootroot";
  3. private static final String DRIVER_NAME = "com.mysql.jdbc.Driver";
  4. private static final String DRIVER_URL = "jdbc:mysql://localhost:3306/spring_boot_plus?useUnicode=true&characterEncoding=UTF-8&useSSL=false";

2.2 修改模块、表、作者等配置

  1. // ############################ 配置部分 start ############################
  2. // 模块名称
  3. private static final String MODULE_NAME = "system";
  4. // 作者
  5. private static final String AUTHOR = "geekidea";
  6. // 生成的表名称
  7. private static final String TABLE_NAME = "sys_log";
  8. // 主键数据库列名称
  9. private static final String PK_ID_COLUMN_NAME = "id";
  10. // ############################ 配置部分 end ############################
  • MODULE_NAME 模块名称,在目前项目上以单独的文件夹形式体现
  • AUTHOR 作者名称,在类的注释上体现
  • TABLE_NAME 表名称,当前需要生成的表名称,关联实体类等
  • PK_ID_COLUMN_NAME 主键列名称,默认是id,如果是其它名称,可在这里配置
  1. 运行CodeGenerator.java

3.1 控制台输出生成日志

  1. 11:33:43.442 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator - ==========================准备生成文件...==========================
  2. 11:33:44.167 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 创建目录: [E:\github\spring-boot-plus/src/main/java\io\geekidea\springbootplus\system\entity]
  3. 11:33:44.169 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 创建目录: [E:\github\spring-boot-plus/src/main/java\io\geekidea\springbootplus\system\web\controller]
  4. 11:33:44.170 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 创建目录: [E:\github\spring-boot-plus/src/main/java\io\geekidea\springbootplus\system\service]
  5. 11:33:44.170 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 创建目录: [E:\github\spring-boot-plus/src/main/java\io\geekidea\springbootplus\system\mapper]
  6. 11:33:44.171 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 创建目录: [E:\github\spring-boot-plus/src/main/java\io\geekidea\springbootplus\system\service\impl]
  7. ...
  8. 11:33:44.294 [main] DEBUG org.apache.velocity - ResourceManager : found /templates/mapper.xml.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
  9. 11:33:44.308 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 模板:/templates/mapper.xml.vm; 文件:E:\github\spring-boot-plus/src/main/resources/mapper/system/SysLogMapper.xml
  10. 11:33:44.313 [main] DEBUG org.apache.velocity - ResourceManager : found /templates/queryParam.java.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
  11. 11:33:44.314 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 模板:/templates/queryParam.java.vm; 文件:E:\github\spring-boot-plus/src/main/java/io/geekidea/springbootplus/system/web/param/SysLogQueryParam.java
  12. 11:33:44.332 [main] DEBUG org.apache.velocity - ResourceManager : found /templates/queryVo.java.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
  13. 11:33:44.337 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 模板:/templates/queryVo.java.vm; 文件:E:\github\spring-boot-plus/src/main/java/io/geekidea/springbootplus/system/web/vo/SysLogQueryVo.java
  14. 11:33:44.347 [main] DEBUG org.apache.velocity - ResourceManager : found /templates/entity.java.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
  15. 11:33:44.357 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 模板:/templates/entity.java.vm; 文件:E:\github\spring-boot-plus/src/main/java\io\geekidea\springbootplus\system\entity\SysLog.java
  16. 11:33:44.359 [main] DEBUG org.apache.velocity - ResourceManager : found /templates/mapper.java.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
  17. 11:33:44.360 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 模板:/templates/mapper.java.vm; 文件:E:\github\spring-boot-plus/src/main/java\io\geekidea\springbootplus\system\mapper\SysLogMapper.java
  18. 11:33:44.362 [main] DEBUG org.apache.velocity - ResourceManager : found /templates/service.java.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
  19. 11:33:44.364 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 模板:/templates/service.java.vm; 文件:E:\github\spring-boot-plus/src/main/java\io\geekidea\springbootplus\system\service\SysLogService.java
  20. 11:33:44.367 [main] DEBUG org.apache.velocity - ResourceManager : found /templates/serviceImpl.java.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
  21. 11:33:44.369 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 模板:/templates/serviceImpl.java.vm; 文件:E:\github\spring-boot-plus/src/main/java\io\geekidea\springbootplus\system\service\impl\SysLogServiceImpl.java
  22. 11:33:44.373 [main] DEBUG org.apache.velocity - ResourceManager : found /templates/controller.java.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
  23. 11:33:44.376 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 模板:/templates/controller.java.vm; 文件:E:\github\spring-boot-plus/src/main/java\io\geekidea\springbootplus\system\web\controller\SysLogController.java
  24. 11:33:44.376 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator - ==========================文件生成完成!!!==========================
  25. Process finished with exit code 0

3.2 生成的模块和包结构

  1. ├─system 模块包
  2. ├─entity 实体类包
  3. ├─mapper mybatis mapper接口包
  4. ├─service 服务接口包
  5. └─impl 服务实现包
  6. └─web 提供前端结果相关包
  7. ├─controller 控制器包
  8. ├─param 参数包
  9. └─vo 值对象,响应结果包

3.3 生成的包及相关的类

  1. ├─system
  2. ├─entity
  3. SysLog.java 实体类,已生成swagger注释
  4. ├─mapper
  5. SysLogMapper.java mapper接口
  6. ├─service
  7. SysLogService.java 服务接口,已继承公共service
  8. └─impl
  9. SysLogServiceImpl.java 服务实现类,已继承公共service impl
  10. └─web
  11. ├─controller
  12. SysLogController.java 控制器类,已生成CRUD,分页controller方法,已生成swagger文档
  13. ├─param
  14. SysLogQueryParam.java 请求参数类,用于条件分页查询等
  15. └─vo
  16. SysLogQueryVo.java 响应结果类,用于自定义查询响应结果等

启动项目

  1. SpringBootPlusApplication.java
  1. 2019-07-27 12:11:45.298 INFO 21856 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8888 (http) with context path ''
  2. 2019-07-27 12:11:45.301 INFO 21856 --- [ main] i.g.s.SpringBootPlusApplication : Started SpringBootPlusApplication in 9.66 seconds (JVM running for 10.988)
  3. 2019-07-27 12:11:45.304 INFO 21856 --- [ main] i.g.s.util.PrintApplicationInfo : projectFinalName : spring-boot-plus
  4. 2019-07-27 12:11:45.305 INFO 21856 --- [ main] i.g.s.util.PrintApplicationInfo : projectVersion : 1.0.0.RELEASE
  5. 2019-07-27 12:11:45.305 INFO 21856 --- [ main] i.g.s.util.PrintApplicationInfo : profileActive : local
  6. 2019-07-27 12:11:45.305 INFO 21856 --- [ main] i.g.s.util.PrintApplicationInfo : contextPath : /
  7. 2019-07-27 12:11:45.305 INFO 21856 --- [ main] i.g.s.util.PrintApplicationInfo : port : 8888
  8. 2019-07-27 12:11:45.308 INFO 21856 --- [ main] i.g.s.util.PrintApplicationInfo : home:http://192.168.1.168:8888/
  9. 2019-07-27 12:11:45.308 INFO 21856 --- [ main] i.g.s.util.PrintApplicationInfo : docs:http://192.168.1.168:8888/docs
  10. 2019-07-27 12:11:45.308 INFO 21856 --- [ main] i.g.s.util.PrintApplicationInfo : spring-boot-plus project start success...........
  11. 2019-07-27 12:11:45.309 INFO 21856 --- [ main] i.g.s.util.PrintApplicationInfo :
  12. ____ __ __ ____
  13. /\ _`\ /\ \__ /\ \__ /\ _`\
  14. \ \,\L\_\ \ ,_\ __ _ __\ \ ,_\ \ \,\L\_\ __ __ ___ ___ __ ____ ____
  15. \/_\__ \\ \ \/ /'__`\ /\`'__\ \ \/ \/_\__ \ /\ \/\ \ /'___\ /'___\ /'__`\ /',__\ /',__\
  16. /\ \L\ \ \ \_/\ \L\.\_\ \ \/ \ \ \_ /\ \L\ \ \ \_\ \/\ \__//\ \__//\ __//\__, `\/\__, `\
  17. \ `\____\ \__\ \__/.\_\\ \_\ \ \__\ \ `\____\ \____/\ \____\ \____\ \____\/\____/\/\____/
  18. \/_____/\/__/\/__/\/_/ \/_/ \/__/ \/_____/\/___/ \/____/\/____/\/____/\/___/ \/___/

访问项目

http://localhost:8888/swagger-ui.html

或者访问本地ip

http://192.168.xxx.xxx:8888/swagger-ui.html

  • 自动生成swagger CRUD、分页接口文档
    swagger
  1. add 添加接口swagger
    add 添加接口swagger

  2. delete 删除接口swagger
    delete 删除接口swagger

  3. getPageList 分页接口swagger
    getPageList 分页接口swagger

  4. info 详情接口swagger
    info 详情接口swagger

  5. update 修改接口swagger
    update 修改接口swagger