无需编程,基于PostgreSQL零代码生成CRUD增删改查RESTful API接口
无需编程,基于PostgreSQL零代码生成CRUD增删改查RESTful API接口
回顾
在前面文章中,已经介绍了crudapi和MySQL数据库集成方式,为了支持更多数据库,对代码进行了重构,采用抽象工厂设计模式,可以无缝切换不同类型的数据库,添加了对大象数据库PostgreSQL的支持。
抽象工厂模式
抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。在抽象工厂模式中,接口是负责创建一个相关对象的工厂,不需要显式指定它们的类。每个生成的工厂都能按照工厂模式提供对象。
UI界面
通过学生对象为例,无需编程,基于PostgreSQL数据库,通过配置零代码实现CRUD增删改查RESTful API接口和管理UI。
创建学生表
编辑学生数据
学生数据列表
通过pgadmin查询postsql数据
实现原理
基类
CrudAbstractRepository为抽象类,主要功能为数据库表的crud增删改查操作。
public abstract class CrudAbstractRepository {
public Long create(String tableName, Map<String, Object> map) {
log.info("CrudAbstractRepository->create");
}
}
CrudAbstractFactory为工厂类,用于创建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子类
CrudAbstractRepository实现了通用数据库处理功能,MySql中如果有不同的处理方法,可以通过Override复写对应的方法,最终子类覆盖父类方法,比如MySqlCrudRepository重新实现了create添加数据功能。
@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子类
和MySql类似,PostSqlCrudRepository中如果有需要重写的部分,直接覆盖同名方法即可。
@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
通过CrudDatasourceProperties读取spring.datasource.driverClassName
@ConfigurationProperties(prefix = "spring.datasource")
@Component
public class CrudDatasourceProperties {
private String driverClassName;
public String getDriverClassName() {
return driverClassName;
}
public void setDriverClassName(String driverClassName) {
this.driverClassName = driverClassName;
}
}
根据spring.datasource.driverClassName的值,通过反射动态创建MySqlCrudFactory或者PostSqlCrudFactory工厂对象,
@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;
}
}
类似RestTemplate,CrudTemplate最终实现了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
需要根据需要配置数据库连接驱动,无需重新发布,就可以切换不同的数据库。
#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=
小结
本文主要介绍了crudapi支持多数据库实现原理,并且以学生对象为例,零代码实现了CRUD增删改查RESTful API,后续计划支持更多的数据库,比如Oracle,MSSQL Server,Mongodb等。
- 点赞
- 收藏
- 关注作者
评论(0)