头图

No programming required, based on PostgreSQL zero code to generate CRUD add, delete, modify and check RESTful API interface

review

In the previous article, the main functions and usage of crudapi have been introduced. Crudapi 1.2.0 only supports MySQL database. In order to support more databases, the code has been refactored, and the abstract factory design pattern can be used to seamlessly switch between different types of Database, as of crudapi version 1.3.0, added support for the elephant database PostgreSQL.

Abstract Factory Pattern

Abstract Factory Pattern (Abstract Factory Pattern) is to create other factories around a super factory. The Gigafactory is also known as the factory of other factories. This type of design pattern is a creational pattern, which provides an optimal way to create objects. In the abstract factory pattern, an interface is a factory responsible for creating a related object without explicitly specifying their class. Each generated factory can provide objects according to the factory pattern.

UI interface

Taking the student object as an example, without programming, based on the PostgreSQL database, CRUD addition, deletion, modification and query RESTful API interface and management UI can be realized by configuring zero code.

table
Create student table

table
Edit student data

customer
Student Data List

customer
Query postsql data through pgadmin

Implementation principle

base class

CrudAbstractRepository is an abstract class whose main function is the crud addition, deletion, modification, and query operations of database tables.

public abstract class CrudAbstractRepository {
  public Long create(String tableName, Map<String, Object> map) {
    log.info("CrudAbstractRepository->create");
  }
}

CrudAbstractFactory is a factory class used to create CrudAbstractRepository.

public abstract class CrudAbstractFactory {
  public abstract CrudAbstractRepository getCrudRepository();

  public Long create(String tableName, Map<String, Object> map) {
    log.info("CrudAbstractFactory->create");
    CrudAbstractRepository repository = this.getCrudRepository();
    return repository.create(tableName, map);
  }
}
MySql subclass

CrudAbstractRepository implements the general database processing function. If there are different processing methods in MySql, you can override the corresponding method through Override, and finally the subclass overrides the parent class method. For example, MySqlCrudRepository reimplemented the create function of adding data.

@Component
public class MySqlCrudRepository extends CrudAbstractRepository {
  @Override
  public Long create(String tableName, Map<String, Object> map) {
    log.info("MySqlCrudRepository->create");

    return super.create(tableName, map);
  }
}
public class MySqlCrudFactory extends CrudAbstractFactory {
  @Autowired
  private MySqlCrudRepository mySqlCrudRepository;
  
  @Override
  public CrudAbstractRepository getCrudRepository() {
    return mySqlCrudRepository;
  }
}
PostSql subclass

Similar to MySql, if there is a part that needs to be rewritten in PostSqlCrudRepository, you can directly override the method of the same name.

@Component
public class PostSqlCrudRepository extends CrudAbstractRepository {
  @Override
  public Long create(String tableName, Map<String, Object> map) {
    log.info("PostSqlCrudRepository->create");
    return super.create(tableName, obj);
  }
}
public class PostSqlCrudFactory extends CrudAbstractFactory {
  @Autowired
  private PostSqlCrudRepository postSqlCrudRepository;
  
  @Override
  public CrudAbstractRepository getCrudRepository() {
    return postSqlCrudRepository;
  }
}
CrudTemplate

Read spring.datasource.driverClassName via CrudDatasourceProperties

@ConfigurationProperties(prefix = "spring.datasource")
@Component
public class CrudDatasourceProperties {
  private String driverClassName;

  public String getDriverClassName() {
    return driverClassName;
  }

  public void setDriverClassName(String driverClassName) {
    this.driverClassName = driverClassName;
  }
}

According to the value of spring.datasource.driverClassName, dynamically create MySqlCrudFactory or PostSqlCrudFactory factory objects through reflection,

@Configuration
public class CrudTemplateConfig {
  public static final String MYSQL_DRIVER_NAME = "com.mysql.cj.jdbc.Driver";
   
  Map<String, String> driverClassNameMap = new HashMap<String, String>() {
    private static final long serialVersionUID = 1L;
    {
      put("com.mysql.cj.jdbc.Driver", "cn.crudapi.core.repository.mysql.MySqlCrudFactory");
      put("org.postgresql.Driver", "cn.crudapi.core.repository.postsql.PostSqlCrudFactory");
    }
  };

  
  @Autowired
  private CrudDatasourceProperties crudDatasourceProperties;
  
  @Bean
  public CrudTemplate crudTemplate(CrudAbstractFactory factory) {
    CrudTemplate crudTemplate =  new CrudTemplate(factory);
    return crudTemplate;
  }
    
  @Bean
  public CrudAbstractFactory crudAbstractFactory() {
    CrudAbstractFactory crudAbstractFactory = null;
    String driverClassName = crudDatasourceProperties.getDriverClassName();
    log.info("CrudTemplateConfig->driverClassName: " + driverClassName);
    
    try {
      String factoryClassName = driverClassNameMap.get(driverClassName);
      if (factoryClassName == null) {
        factoryClassName = driverClassNameMap.get(MYSQL_DRIVER_NAME);
      }
      log.info("CrudTemplateConfig->factoryClassName: " + factoryClassName);
      
      Class<?> cls = Class.forName(factoryClassName);
      Object obj = cls.newInstance();
      
      crudAbstractFactory = (CrudAbstractFactory)obj;
    } catch (Exception e) {
      e.printStackTrace();
    }
    
    return crudAbstractFactory;
  }
}

Similar to RestTemplate, CrudTemplate finally realizes the function of adding, deleting, modifying and checking crud

public class CrudTemplate {
  @Nullable
  private volatile CrudAbstractFactory crudFactory;

  public CrudTemplate() {
    super();
    log.info("CrudTemplate->Constructor");
  }
  
  public CrudTemplate(CrudAbstractFactory crudFactory) {
    super();
    log.info("CrudTemplate->Constructor crudFactory");
    this.crudFactory = crudFactory;
  }
  
  public Long create(String tableName, Map<String, Object> map) {
    log.info("CrudTemplate->create");
    return crudFactory.create(tableName, map);
  }
}
application.properties

You need to configure the database connection driver as needed, and you can switch between different databases without republishing.

#mysql
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/crudapi
spring.datasource.username=
spring.datasource.password=

#postgresql
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/crudapi
spring.datasource.username=
spring.datasource.password=

summary

This article mainly introduces the implementation principle of crudapi supporting multiple databases, and takes the student object as an example, zero-code implementation of CRUD addition, deletion, modification, and query RESTful API, and subsequent plans to support more databases, such as Oracle, MSSQL Server, Mongodb, etc.

Method to realizeamount of codetimestability
traditional development1000 lines or so2 days/personabout 5 bugs
crudapi system0 lines1 minutebasically 0

To sum up, using the crudapi system can greatly improve work efficiency and save costs, and make data processing easier!

Introduction to crudapi

Crudapi is a combination of crud+api, which means adding, deleting, modifying and checking interfaces. It is a zero-code configurable product. Using crudapi can say goodbye to the boring addition, deletion, modification and checking of code, allowing you to focus more on business, save a lot of costs, and improve work efficiency.
The goal of crudapi is to make working with data easier and free for everyone!
Without programming, it can automatically generate crud, add, delete, modify and check RESTful API through configuration, and provide background UI to manage business data. Based on the mainstream open source framework, it has independent intellectual property rights and supports secondary development.

demo

Crudapi is a product-level zero-code platform. Unlike automatic code generators, crudapi does not need to generate business codes such as Controller, Service, Repository, and Entity. The program can be used when it is running. It is truly zero-code and can cover basic business-independent CRUD. RESTful APIs.

Official website address: https://crudapi.cn
Test address: https://demo.crudapi.cn/crudapi/login

With source code address

GitHub address

https://github.com/crudapi/crudapi-admin-web

Gitee address

https://gitee.com/crudapi/crudapi-admin-web

Due to network reasons, GitHub may be slow, you can change it to access Gitee, and the code will be updated synchronously.


crudapi
38 声望4 粉丝

crudapi是crud+api组合,表示增删改查接口,是一款零代码可配置的产品。使用crudapi可以告别枯燥无味的增删改查代码,让您更加专注业务,节约大量成本,从而提高工作效率。crudapi的目标是让处理数据变得更简单!