Spring boot使用一个接口实现任意一张表的增删改查
文章目录
本文旨在:
-
(需要做额外的操作创建对应表和再一个配置表里配置相关的表)
-
使用一个接口的方式,下载任意一个表的Excel模板
-
使用一个接口的方式,将任意的excel中的数据导入到对应的表里面
-
使用一个接口的方式,将任意表中的数据查询出来并展示
-
使用一个接口的方式,将任意表中的数据查询出来并根据表中字段自动生成过滤条件展示
-
使用一个接口的方式,对于任意表中的数据进行新增
-
使用一个接口的方式,对任意表中的任意数量数据删除
-
使用一个接口的方式,对任意表中的任意一条数据进行修改
这个是我之前开发项目的时候遇到自定义数据CRUD时写的解决方案,因为觉得不错,所以把其中的业务部份的逻辑都剔除了,提取出了其中的重要的部分,用于帮助分享
0、讲解部分
首先为了保证数据的唯一性,我默认每一张自动定义的表里面肯定有一个相同的属性名字的属,这个属性一定是主键
另外就是我会单独的存储不同表的过滤条件。这个过滤条件可以手动加,也可以写接口帮助加。在自动创建表提交表单时就可以往过滤的表中存储这样的过滤字段
不过由于时间原因,我并没有写通过一个接口的方式可以创建这样的表的接口,如果大家有空的话可以写一下帮我加上代码
我会把带代码提交到GitHub中,是免费开源的
相应的这个博客解答了我在这个博客中抛给大家的问题《Spring boot结合easy excel实现低代码量的Excel导入导出》
1、准备工作
引入maven依赖
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.10</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.6</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<!--引入Knife4j的官方start包,该指南选择Spring Boot版本<3.0,开发者需要注意-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-openapi2-spring-boot-starter</artifactId>
<version>4.4.0</version>
</dependency>
<!-- 参数校验依赖 -->
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.25</version>
<scope>compile</scope>
</dependency>
其中主要是一下一些配置信息:
druid、knife4j、fastjson、mybatis-plus、动态数据源、MySQL驱动、lombok、hutool
我们使用的是spring boot 是2.5.6版本。
初始化SQL
create schema IF NOT EXISTS report collate utf8mb4_general_ci;
use
report;
create table if not exists report.filter_info
(
id bigint not null comment '主键',
table_code varchar(100) null comment '表编码',
column_code varchar(100) null comment '字段码',
column_name varchar(100) null comment '字段名称',
type varchar(32) null comment '类型',
dim_class varchar(100) null comment '对应维度值',
sort int null comment '排序',
is_required tinyint default 0 null comment '是否必须'
) comment '表筛选项表';
create table if not exists report.meta_info_mapping
(
sys_code varchar(100) null comment '元数据编码',
sys_name varchar(100) null comment '元数据名称',
sys_dim_class varchar(100) null comment '元数据类型'
) comment '元数据<下拉框使用>';
配置文件信息为
spring:
application:
name: xxxx
datasource:
dynamic:
# ?????
primary: master
datasource:
master:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/report?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
username: root
password: 123456
druid:
initialSize: 10
minIdle: 10
maxActive: 200
max-wait: 60000
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
validation-query: SELECT 'x'
test-on-borrow: false
test-on-return: false
test-while-idle: true
time-between-eviction-runs-millis: 60000 #???????????????????????????????
min-evictable-idle-time-millis: 300000 #??????????????????????
filters: stat,wall
wall:
comment-allow: true
multiStatementAllow: true
noneBaseStatementAllow: true
knife4j:
enable: true
# mybatis config
mybatis-plus:
mapper-locations: mapper/*.xml
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
2、代码实现
前后端对接包装类
@Getter
@Setter
@SuppressWarnings("ALL")
@Accessors(chain = true)
public class Result<T> {
public static final String DEF_ERROR_MESSAGE = "系统繁忙,请稍候再试";
public static final String HYSTRIX_ERROR_MESSAGE = "请求超时,请稍候再试";
public static final int SUCCESS_CODE = 200;
public static final int FAIL_CODE = 500;
public static final int TIMEOUT_CODE = 503;
/**
* 统一参数验证异常
*/
public static final int VALID_EX_CODE = 400;
public static final int OPERATION_EX_CODE = 400;
/**
* 调用是否成功标识,0:成功,-1:系统繁忙,此时请开发者稍候再试 详情见[ExceptionCode]
*/
@ApiModelProperty(value = "响应编码:0/200-请求处理成功")
private int state;
/**
* 调用结果
*/
@ApiModelProperty(value = "响应数据")
private T data;
/**
* 结果消息,如果调用成功,消息通常为空T
*/
@ApiModelProperty(value = "提示消息")
private String msg = "SUCCESS";
/**
* 附加数据
*/
// @ApiModelProperty(value = "附加数据")
// private Map<Object, Object> extra;
/**
* 响应时间
*/
@ApiModelProperty(value = "响应时间戳")
private long timestamp = System.currentTimeMillis();
/**
* 系统报错时,抛出的原生信息