简介
:::info
企业追求敏捷开发自然离不开低代码平台.低代码平台的常见能力,如:模型编排,流程编排.服务编排,界面编排等.其实低代码就是对共性抽取,对特性扩展.使得开发人员专注于业务的开发.
那一键生成代码怎么做?
- 正向工程:就是通过可视化界面等创建表到生成crud (推荐若依开源框架https://gitee.com/y_project/RuoYi/tree/master/ruoyi-generator)
- 逆向工程:通过已知表,直接生成crud
:::
需求:输入表名自动生成crud代码?(逆向工程)
实现:
- 通过表名,去数据库查询表信息(如:表注释),表字段信息(如:字段名,字段类型,字段注释等)
- 然后通过模板引擎去生成代码
实现逆向工程
mysql为例:数据表以及字段信息需要如下sql
--显示user表所有字段
SHOW FULL COLUMNS FROM user;
select COLUMN_NAME, DATA_TYPE, COLUMN_COMMENT
from information_schema.columns
where TABLE_NAME = 'user';
select COLUMN_NAME, DATA_TYPE, COLUMN_COMMENT
from information_schema.COLUMNS
where table_name = 'user'
and table_schema = 'spring_db';
select TABLE_NAME, TABLE_COMMENT
from information_schema.TABLES
where table_schema = 'spring_db'
and table_name = 'user';
创建springboot项目引入如下依赖
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!--velocity代码生成使用模板 -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.3</version>
</dependency>
<!-- 阿里JSON解析器 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
</dependency>
</dependencies>
application.yml
server:
port: 8080
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/spring_db?characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
username: root
password: 123456
type: com.zaxxer.hikari.HikariDataSource
hikari:
connection-timeout: 30000
idle-timeout: 30000
auto-commit: 'true'
minimum-idle: 5
maximum-pool-size: 15
pool-name: HikariCP
connection-test-query: SELECT 1 FROM DUAL
max-lifetime: 1800000
mybatis-plus:
mapper-locations: classpath*:mybatis/mapper/*.xml
type-aliases-package: com.itmck.pojo
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
表字段信息实体
package com.itmck.ormgeneratortool.dao.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName("information_schema.columns")
public class ColumnInformation {
/**
* field名
*/
@TableField("columnName")
private String columnName;
/**
* 类型
*/
@TableField("dataType")
private String dataType;
/**
* 注释
*/
@TableField("columnComment")
private String columnComment;
}
数据表信息实体
package com.itmck.ormgeneratortool.dao.entity;
import lombok.Data;
import lombok.experimental.Accessors;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
/**
* 业务表 gen_table
*
* @author ruoyi
*/
@Data
@Accessors(chain = true)
public class TableInformation {
private static final long serialVersionUID = 1L;
/**
* 表名称
*/
private String tableName;
/**
* 表描述
*/
private String tableComment;
}
mapper
package com.itmck.ormgeneratortool.dao.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itmck.ormgeneratortool.dao.entity.ColumnInformation;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface ColumnInformationMapper extends BaseMapper<ColumnInformation> {
@Select(value = "select COLUMN_NAME, DATA_TYPE, COLUMN_COMMENT from information_schema.COLUMNS where table_name = #{tableName} and table_schema = #{dbName}")
List<ColumnInformation> showColumns(@Param("tableName") String tableName, @Param("dbName") String dbName);
}
package com.itmck.ormgeneratortool.dao.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itmck.ormgeneratortool.dao.entity.TableInformation;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface TableInformationMapper extends BaseMapper<TableInformation> {
@Select(value = "select TABLE_NAME,TABLE_COMMENT from information_schema.TABLES where table_schema = #{tableSchema} and table_name =#{tableName}")
TableInformation showTableInformation(@Param("tableName") String tableName, @Param("tableSchema") String tableSchema);
}
其实到这里就可以查出表的信息以及字段信息.剩下的就是将查询出来的信息填充到模板引擎对应的变量位置.
其余代码这里就不贴出来,后续参考我git[https://gitee.com/itmc/orm-generator-tool.git]