Nenhuma programação necessária, com base no código zero do banco de dados Oracle para gerar a interface CRUD CRUD CRUD RESTful API

Nenhuma programação necessária, com base no código zero do banco de dados Oracle para gerar a interface CRUD CRUD CRUD RESTful API

Reveja

Através da introdução do artigo anterior Sem programação, baseado no código zero PostgreSQL para gerar CRUD adicionar, excluir, modificar e verificar a interface , o padrão de design abstract factory foi adotado e o banco de dados elefante PostgreSQL foi suportado. Antes, gerar instruções SQL DDL por concatenação de strings era complicado. No início deste artigo, o mecanismo de modelo do FreeMarker é apresentado para criar e modificar instruções SQL de estrutura de tabela física configurando modelos, simplificando muito código e melhorando a eficiência e configurando modelos SQL de banco de dados oracle, com base em banco de dados oracle, código zero para obter adições, exclusões, alterações e verificações brutas.

Introdução ao FreeMarker

O FreeMarker é um mecanismo de template: uma ferramenta genérica para gerar texto de saída (páginas HTML, e-mails, arquivos de configuração, código fonte, etc.) com base em templates e nos dados a serem alterados. Não se destina a usuários finais, mas a uma biblioteca de classes Java, um componente que os programadores podem incorporar nos produtos que desenvolvem. Os modelos são escritos em FreeMarker Template Language (FTL). É uma linguagem simples e especializada, não uma linguagem de programação completa como o PHP. Isso significa preparar dados para exibição em linguagens de programação reais, como consultas de banco de dados e operações de negócios, e então os modelos exibem os dados preparados. No modelo, você pode se concentrar em como apresentar os dados e, fora do modelo, pode se concentrar em quais dados exibir.

Interface do usuário

Tomando o objeto do produto como exemplo, sem programação, com base no banco de dados Oracle, a adição, exclusão, modificação e consulta de CRUD API RESTful e interface do usuário de gerenciamento podem ser realizadas configurando código zero.

produtoMetaCriar um produto

tabelaEditar dados do produto

lista de produtosLista de dados do produto

Desenvolvedor Oracle SQLConsultar dados Oracle por meio do Oracle SQL Developer

Definindo o Modelo de Objeto de Metadados

Tabela de metadados ca_meta_table

ca_meta_tableA tabela de metadados ca_meta_table é utilizada para registrar as informações básicas da tabela.

Objeto TableEntity

TableEntity é um objeto "metadata table", correspondente ao campo ca_meta_table

public class TableEntity {
    private Long id;

    private String name;

    private String caption;

    private String description;

    private Timestamp createdDate;

    private Timestamp lastModifiedDate;

    private String pluralName;

    private String tableName;

    private EngineEnum engine;

    private Boolean createPhysicalTable;

    private Boolean reverse;

    private Boolean systemable;

    private Boolean readOnly;

    private List<ColumnEntity> columnEntityList;

    private List<IndexEntity> indexEntityList;
}
复制代码

Coluna de metadados ca_meta_column

ca_meta_column 元数据列ca_meta_column,用于记录表字段信息,比如类型,长度,默认值等。

ColumnEntity对象

ColumnEntity为“元数据列”对象,和ca_meta_column字段对应

public class ColumnEntity {
  private Long id;

  private String name;

  private String caption;

  private String description;

  private Timestamp createdDate;

  private Timestamp lastModifiedDate;

  private Integer displayOrder;

  private DataTypeEnum dataType;

  private IndexTypeEnum indexType;

  private IndexStorageEnum indexStorage;

  private String indexName;

  private Integer length;

  private Integer precision;

  private Integer scale;

  private String defaultValue;

  private Long seqId;

  private Boolean unsigned;

  private Boolean autoIncrement;

  private Boolean nullable;

  private Boolean insertable;

  private Boolean updatable;

  private Boolean queryable;

  private Boolean displayable;

  private Boolean systemable;

  private Long tableId;
}
复制代码

元数据索引ca_meta_index

ca_meta_index 元数据索引ca_meta_index,用于记录表联合索引信息,比如索引类型,名称等。

IndexEntity对象

IndexEntity为“元数据索引”对象,和ca_meta_index字段对应

public class IndexEntity {
  private Long id;

  private String name;

  private String caption;

  private String description;

  private Timestamp createdDate;

  private Timestamp lastModifiedDate;

  private IndexTypeEnum indexType;

  private IndexStorageEnum indexStorage;

  private Long tableId;

  private List<IndexLineEntity> indexLineEntityList;
}
复制代码

元数据索引行ca_meta_index_line

ca_meta_index_line 元数据索引行ca_meta_index_line,用于记录表联合索引行信息,一个联合索引可以对应多个联合索引行,表示由多个字段组成。

IndexLineEntity对象

IndexLineEntity“元数据索行”对象,和ca_meta_index_line字段对应

public class IndexLineEntity {
  private Long id;

  private Long columnId;

  private ColumnEntity columnEntity;

  private Long indexId;
}
复制代码

定义FreeMarker模版

创建表create-table.sql.ftl

CREATE TABLE "${tableName}" (
<#list columnEntityList as columnEntity>
  <#if columnEntity.dataType == "BOOL">
    "${columnEntity.name}" NUMBER(1)<#if columnEntity.defaultValue??> DEFAULT <#if columnEntity.defaultValue == "true">1<#else>0</#if></#if><#if columnEntity.nullable != true> NOT NULL</#if><#if columnEntity_has_next>,</#if>
  <#elseif columnEntity.dataType == "INT">
    "${columnEntity.name}" INT<#if columnEntity.defaultValue??> DEFAULT ${columnEntity.defaultValue}</#if><#if columnEntity.nullable != true> NOT NULL</#if><#if columnEntity.indexType?? && columnEntity.indexType == "PRIMARY"> PRIMARY KEY</#if><#if columnEntity_has_next>,</#if>
  <#elseif columnEntity.dataType == "BIGINT">
    "${columnEntity.name}" INT<#if columnEntity.defaultValue??> DEFAULT ${columnEntity.defaultValue}</#if><#if columnEntity.nullable != true> NOT NULL</#if><#if columnEntity.indexType?? && columnEntity.indexType == "PRIMARY"> PRIMARY KEY</#if><#if columnEntity_has_next>,</#if>
  <#elseif columnEntity.dataType == "FLOAT">
    "${columnEntity.name}" FLOAT<#if columnEntity.defaultValue??> DEFAULT ${columnEntity.defaultValue}</#if><#if columnEntity.nullable != true> NOT NULL</#if><#if columnEntity_has_next>,</#if>
  <#elseif columnEntity.dataType == "DOUBLE">
    "${columnEntity.name}" REAL<#if columnEntity.defaultValue??> DEFAULT ${columnEntity.defaultValue}</#if><#if columnEntity.nullable != true> NOT NULL</#if><#if columnEntity_has_next>,</#if>
  <#elseif columnEntity.dataType == "DECIMAL">
    "${columnEntity.name}" DECIMAL<#if columnEntity.defaultValue??> DEFAULT ${columnEntity.defaultValue}</#if><#if columnEntity.nullable != true> NOT NULL</#if><#if columnEntity_has_next>,</#if>
  <#elseif columnEntity.dataType == "DATE">
    "${columnEntity.name}" DATE<#if columnEntity.defaultValue??> DEFAULT ${columnEntity.defaultValue}</#if><#if columnEntity.nullable != true> NOT NULL</#if><#if columnEntity_has_next>,</#if>
  <#elseif columnEntity.dataType == "TIME">
    "${columnEntity.name}" CHAR(8)<#if columnEntity.defaultValue??> DEFAULT ${columnEntity.defaultValue}</#if><#if columnEntity.nullable != true> NOT NULL</#if><#if columnEntity_has_next>,</#if>
  <#elseif columnEntity.dataType == "DATETIME">
    "${columnEntity.name}" DATE<#if columnEntity.defaultValue??> DEFAULT ${columnEntity.defaultValue}</#if><#if columnEntity.nullable != true> NOT NULL</#if><#if columnEntity_has_next>,</#if>
  <#elseif columnEntity.dataType == "TIMESTAMP">
    "${columnEntity.name}" TIMESTAMP<#if columnEntity.defaultValue??> DEFAULT ${columnEntity.defaultValue}</#if><#if columnEntity.nullable != true> NOT NULL</#if><#if columnEntity_has_next>,</#if>
  <#elseif columnEntity.dataType == "CHAR">
    "${columnEntity.name}" CHAR(${columnEntity.length})<#if columnEntity.defaultValue??> DEFAULT '${columnEntity.defaultValue}'</#if><#if columnEntity.nullable != true> NOT NULL</#if><#if columnEntity.indexType?? && columnEntity.indexType == "PRIMARY"> PRIMARY KEY</#if><#if columnEntity_has_next>,</#if>
  <#elseif columnEntity.dataType == "VARCHAR">
    "${columnEntity.name}" VARCHAR(${columnEntity.length})<#if columnEntity.defaultValue??> DEFAULT '${columnEntity.defaultValue}'</#if><#if columnEntity.nullable != true> NOT NULL</#if><#if columnEntity.indexType?? && columnEntity.indexType == "PRIMARY"> PRIMARY KEY</#if><#if columnEntity_has_next>,</#if>
  <#elseif columnEntity.dataType == "PASSWORD">
    "${columnEntity.name}" VARCHAR(200)<#if columnEntity.defaultValue??> DEFAULT '${columnEntity.defaultValue}'</#if><#if columnEntity.nullable != true> NOT NULL</#if><#if columnEntity_has_next>,</#if>
  <#elseif columnEntity.dataType == "ATTACHMENT">
    "${columnEntity.name}" VARCHAR(4000)<#if columnEntity.defaultValue??> DEFAULT '${columnEntity.defaultValue}'</#if><#if columnEntity.nullable != true> NOT NULL</#if><#if columnEntity_has_next>,</#if>
  <#elseif columnEntity.dataType == "TEXT">
    "${columnEntity.name}" VARCHAR(4000)<#if columnEntity.defaultValue??> DEFAULT '${columnEntity.defaultValue}'</#if><#if columnEntity.nullable != true> NOT NULL</#if><#if columnEntity_has_next>,</#if>
  <#elseif columnEntity.dataType == "LONGTEXT">
    "${columnEntity.name}" LONG<#if columnEntity.defaultValue??> DEFAULT ${columnEntity.defaultValue}</#if><#if columnEntity.nullable != true> NOT NULL</#if><#if columnEntity_has_next>,</#if>
  <#elseif columnEntity.dataType == "BLOB">
    "${columnEntity.name}" BLOB<#if columnEntity.defaultValue??> DEFAULT ${columnEntity.defaultValue}</#if><#if columnEntity.nullable != true> NOT NULL</#if><#if columnEntity_has_next>,</#if>
  <#elseif columnEntity.dataType == "LONGBLOB">
    "${columnEntity.name}" BLOB<#if columnEntity.defaultValue??> DEFAULT ${columnEntity.defaultValue}</#if><#if columnEntity.nullable != true> NOT NULL</#if><#if columnEntity_has_next>,</#if>
  <#else>
    "${columnEntity.name}" VARCHAR(200)<#if columnEntity.defaultValue??> DEFAULT ${columnEntity.defaultValue}</#if><#if columnEntity.nullable != true> NOT NULL</#if><#if columnEntity.indexType?? && columnEntity.indexType == "PRIMARY"> PRIMARY KEY</#if><#if columnEntity_has_next>,</#if>
  </#if>
</#list>
);

<#list columnEntityList as columnEntity>
  <#if columnEntity.indexType?? && columnEntity.indexType == "UNIQUE">
    ALTER TABLE "${tableName}" ADD CONSTRAINT "${columnEntity.indexName}" UNIQUE("${columnEntity.name}");
  </#if>

  <#if columnEntity.indexType?? && (columnEntity.indexType == "INDEX" || columnEntity.indexType == "FULLTEXT")>
    CREATE INDEX "${columnEntity.indexName}" ON "${tableName}" ("${columnEntity.name}");
  </#if>
</#list>

<#if indexEntityList??>
  <#list indexEntityList as indexEntity>
    <#if indexEntity.indexType?? && indexEntity.indexType == "UNIQUE">
      ALTER TABLE "${tableName}" ADD CONSTRAINT "${indexEntity.name}" UNIQUE(<#list indexEntity.indexLineEntityList as indexLineEntity>"${indexLineEntity.columnEntity.name}"<#if indexLineEntity_has_next>,</#if></#list>);
    </#if>

    <#if indexEntity.indexType?? && (indexEntity.indexType == "INDEX" || indexEntity.indexType == "FULLTEXT")>
      CREATE INDEX "${indexEntity.name}" ON "${tableName}" (<#list indexEntity.indexLineEntityList as indexLineEntity>"${indexLineEntity.columnEntity.name}"<#if indexLineEntity_has_next>,</#if></#list>);
    </#if>
  </#list>
</#if>

COMMENT ON TABLE "${tableName}" IS '${caption}';

<#list columnEntityList as columnEntity>
  COMMENT ON COLUMN "${tableName}"."${columnEntity.name}" IS '${columnEntity.caption}';
</#list>
复制代码

模版解析SQL

首先保存元数据信息,下一步传递模版名称和元数据model,动态解析成创建表SQL语句,然后创建物理表,这样元数据和物理表就关联上了。运行时通过解析元数据动态生成insert,select,update,delete等SQL语句,零代码实现业务数据crud功能。

public String processTemplateToString(String database, String templateName, Object dataModel) {
    String str = null;
    StringWriter stringWriter = new StringWriter();
    try {
        Configuration config = new Configuration(Configuration.VERSION_2_3_31);
        config.setNumberFormat("#");
        String templateValue = getTemplate(database, templateName);
        if (templateValue == null) {
          return str;
        }

        Template template = new Template(templateName, templateValue, config);
        template.process(dataModel, stringWriter);

        str = stringWriter.getBuffer().toString().trim();
        log.info(str);
    } catch (Exception e) {
        e.printStackTrace();
        throw new BusinessException(ApiErrorCode.DEFAULT_ERROR, e.getMessage());
    }

    return str;
}

public List<String> toCreateTableSql(TableEntity tableEntity) {
  String createTableSql = processTemplateToString("create-table.sql.ftl", tableEntity);

  if (createTableSql == null) {
    throw new BusinessException(ApiErrorCode.DEFAULT_ERROR, "create-table.sql is empty!");
  }

  List<String> sqls = new ArrayList<String>();
  String[] subSqls = createTableSql.split(";");
  for (String t : subSqls) {
    String subSql = t.trim();
    if (!subSql.isEmpty()) {
      sqls.add(t);
    }
  }

  return sqls;
}

public Long create(TableDTO tableDTO) {
  TableEntity tableEntity = tableMapper.toEntity(tableDTO);
  //TODO
  Long tableId = crudService.create(TABLE_TABLE_NAME, tableEntity);
  List<String> sqlList = crudService.toCreateTableSql(tableEntity);
  for (String sql: sqlList) {
    execute(sql);
  }
  //TODO
  return tableId;
}
复制代码

修改表

freemarker.png 包括表结构和索引的修改,删除等,和创建表原理类似。

application.properties

需要根据需要配置数据库连接驱动,无需重新发布,就可以切换不同的数据库。

#oracle
spring.datasource.url=jdbc:oracle:thin:@//localhost:1521/XEPDB1
spring.datasource.driverClassName=oracle.jdbc.OracleDriver
spring.datasource.username=crudapi
spring.datasource.password=crudapi
spring.datasource.initialization-mode=always
spring.datasource.schema=classpath:schema.sql
复制代码

小结

本文主要介绍了crudapi支持oracle数据库实现原理,并且以产品对象为例,零代码实现了CRUD增删改查RESTful API,后续介绍更多的数据库,比如MSSQL Server,Mongodb等。

实现方式 代码量 时间 稳定性
传统开发 1000行左右 2天/人 5个bug左右
crudapi系统 0行 1分钟 基本为0

综上所述,利用crudapi系统可以极大地提高工作效率和节约成本,让数据处理变得更简单!

crudapi简介

Crudapi é uma combinação de crud+api, que significa adicionar, deletar, modificar e checar interfaces, é um produto configurável de código zero. O uso do crudapi pode dizer adeus à adição, exclusão, modificação e verificação chatas de código, permitindo que você se concentre mais nos negócios, economize muitos custos e melhore a eficiência do trabalho. O objetivo do crudapi é tornar o trabalho com dados mais fácil e gratuito para todos! Sem programação, ele pode gerar automaticamente dados brutos, adicionar, excluir, modificar e verificar a API RESTful por meio da configuração e fornecer UI em segundo plano para gerenciar dados de negócios. Com base na estrutura de código aberto convencional, possui direitos de propriedade intelectual independentes e suporta desenvolvimento secundário.

apresentação de demonstração

O Crudapi é uma plataforma de código zero no nível do produto. Ao contrário dos geradores de código automáticos, o crudapi não precisa gerar códigos de negócios como Controlador, Serviço, Repositório e Entidade. O programa pode ser usado quando está em execução. código e pode abranger APIs RESTful CRUD independentes de negócios básicas.

Endereço do site oficial: crudapi.cn
Endereço de teste: demo.crudapi.cn/crudapi/log…

Com endereço de código fonte

Endereço do GitHub

github.com/crudapi/cru…

Endereço do Gitee

gitee.com/crudapi/cru…

Por motivos de rede, o GitHub pode ser lento, você pode alterá-lo para acessar o Gitee e o código será atualizado de forma síncrona.

Acho que você gosta

Origin juejin.im/post/7084158231316004872
Recomendado
Clasificación