[源码:orm-generator-tool.zip]

简介

:::info 企业追求敏捷开发自然离不开低代码平台.低代码平台的常见能力,如:模型编排,流程编排.服务编排,界面编排等.其实低代码就是对共性抽取,对特性扩展.使得开发人员专注于业务的开发.
那一键生成代码怎么做?

  1. 通过表名,去数据库查询表信息(如:表注释),表字段信息(如:字段名,字段类型,字段注释等)
  2. 然后通过模板引擎去生成代码

实现逆向工程

mysql为例:数据表以及字段信息需要如下sql

  1. --显示user表所有字段
  2. SHOW FULL COLUMNS FROM user;
  3. select COLUMN_NAME, DATA_TYPE, COLUMN_COMMENT
  4. from information_schema.columns
  5. where TABLE_NAME = 'user';
  6. select COLUMN_NAME, DATA_TYPE, COLUMN_COMMENT
  7. from information_schema.COLUMNS
  8. where table_name = 'user'
  9. and table_schema = 'spring_db';
  10. select TABLE_NAME, TABLE_COMMENT
  11. from information_schema.TABLES
  12. where table_schema = 'spring_db'
  13. and table_name = 'user';

创建springboot项目引入如下依赖

  1. <dependencies>
  2. <dependency>
  3. <groupId>mysql</groupId>
  4. <artifactId>mysql-connector-java</artifactId>
  5. <scope>runtime</scope>
  6. </dependency>
  7. <dependency>
  8. <groupId>com.baomidou</groupId>
  9. <artifactId>mybatis-plus-boot-starter</artifactId>
  10. <version>3.5.2</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>junit</groupId>
  14. <artifactId>junit</artifactId>
  15. <scope>test</scope>
  16. </dependency>
  17. <!--velocity代码生成使用模板 -->
  18. <dependency>
  19. <groupId>org.apache.velocity</groupId>
  20. <artifactId>velocity-engine-core</artifactId>
  21. <version>2.3</version>
  22. </dependency>
  23. <!-- 阿里JSON解析器 -->
  24. <dependency>
  25. <groupId>com.alibaba</groupId>
  26. <artifactId>fastjson</artifactId>
  27. <version>1.2.83</version>
  28. </dependency>
  29. </dependencies>

application.yml

  1. server:
  2. port: 8080
  3. spring:
  4. datasource:
  5. driver-class-name: com.mysql.cj.jdbc.Driver
  6. url: jdbc:mysql://localhost:3306/spring_db?characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
  7. username: root
  8. password: 123456
  9. type: com.zaxxer.hikari.HikariDataSource
  10. hikari:
  11. connection-timeout: 30000
  12. idle-timeout: 30000
  13. auto-commit: 'true'
  14. minimum-idle: 5
  15. maximum-pool-size: 15
  16. pool-name: HikariCP
  17. connection-test-query: SELECT 1 FROM DUAL
  18. max-lifetime: 1800000
  19. mybatis-plus:
  20. mapper-locations: classpath*:mybatis/mapper/*.xml
  21. type-aliases-package: com.itmck.pojo
  22. configuration:
  23. log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

表字段信息实体

  1. package com.itmck.ormgeneratortool.dao.entity;
  2. import com.baomidou.mybatisplus.annotation.TableField;
  3. import com.baomidou.mybatisplus.annotation.TableName;
  4. import lombok.Data;
  5. @Data
  6. @TableName("information_schema.columns")
  7. public class ColumnInformation {
  8. /**
  9. * field名
  10. */
  11. @TableField("columnName")
  12. private String columnName;
  13. /**
  14. * 类型
  15. */
  16. @TableField("dataType")
  17. private String dataType;
  18. /**
  19. * 注释
  20. */
  21. @TableField("columnComment")
  22. private String columnComment;
  23. }

数据表信息实体

  1. package com.itmck.ormgeneratortool.dao.entity;
  2. import lombok.Data;
  3. import lombok.experimental.Accessors;
  4. import javax.validation.Valid;
  5. import javax.validation.constraints.NotBlank;
  6. import java.util.List;
  7. /**
  8. * 业务表 gen_table
  9. *
  10. * @author ruoyi
  11. */
  12. @Data
  13. @Accessors(chain = true)
  14. public class TableInformation {
  15. private static final long serialVersionUID = 1L;
  16. /**
  17. * 表名称
  18. */
  19. private String tableName;
  20. /**
  21. * 表描述
  22. */
  23. private String tableComment;
  24. }

mapper

  1. package com.itmck.ormgeneratortool.dao.mapper;
  2. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  3. import com.itmck.ormgeneratortool.dao.entity.ColumnInformation;
  4. import org.apache.ibatis.annotations.Mapper;
  5. import org.apache.ibatis.annotations.Param;
  6. import org.apache.ibatis.annotations.Select;
  7. import java.util.List;
  8. @Mapper
  9. public interface ColumnInformationMapper extends BaseMapper<ColumnInformation> {
  10. @Select(value = "select COLUMN_NAME, DATA_TYPE, COLUMN_COMMENT from information_schema.COLUMNS where table_name = #{tableName} and table_schema = #{dbName}")
  11. List<ColumnInformation> showColumns(@Param("tableName") String tableName, @Param("dbName") String dbName);
  12. }
  1. package com.itmck.ormgeneratortool.dao.mapper;
  2. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  3. import com.itmck.ormgeneratortool.dao.entity.TableInformation;
  4. import org.apache.ibatis.annotations.Mapper;
  5. import org.apache.ibatis.annotations.Param;
  6. import org.apache.ibatis.annotations.Select;
  7. @Mapper
  8. public interface TableInformationMapper extends BaseMapper<TableInformation> {
  9. @Select(value = "select TABLE_NAME,TABLE_COMMENT from information_schema.TABLES where table_schema = #{tableSchema} and table_name =#{tableName}")
  10. TableInformation showTableInformation(@Param("tableName") String tableName, @Param("tableSchema") String tableSchema);
  11. }

其实到这里就可以查出表的信息以及字段信息.剩下的就是将查询出来的信息填充到模板引擎对应的变量位置.

其余代码这里就不贴出来,后续参考我git[https://gitee.com/itmc/orm-generator-tool.git]