智能BI项目-初始化(二)
往期开发(已经完成)
- 智能 BI 项目-介绍(一)
- 智能 BI 项目-初始化(二)
- 智能 BI 项目-初学 AI 分析(三)
- 智能 BI 项目-AI 接口调用(四)
- 智能 BI 项目-接口优化(五)
- 智能 BI 项目-接口的异步化(六)
- 智能 BI 项目-引入 RabbitMQ(七)
后端初始化
1.后端框架的搭建
- 拉取自己提前写好的 springboot 基础整合,项目文件结构如下:

- 使用 Mybatis-X 生成基础代码,移动 entity,service,mapper 等文件到指定目录
2.设计数据库以及表
- 执行如下 sql 进行库和表的创建:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38-- 创建库
create database if not exists bi;
-- 切换库
use bi;
-- 用户表
create table if not exists user
(
id bigint auto_increment comment 'id' primary key,
userAccount varchar(256) not null comment '账号',
userPassword varchar(512) not null comment '密码',
userName varchar(256) null comment '用户昵称',
userAvatar varchar(1024) null comment '用户头像',
userRole varchar(256) default 'user' not null comment '用户角色:user/admin',
createTime datetime default CURRENT_TIMESTAMP not null comment '创建时间',
updateTime datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间',
isDelete tinyint default 0 not null comment '是否删除',
index idx_userAccount (userAccount)
) comment '用户' collate = utf8mb4_unicode_ci;
-- 图表表
create table if not exists chart
(
id bigint auto_increment comment 'id' primary key,
goal text null comment '分析目标',
`name` varchar(128) null comment '图表名称',
chartData text null comment '图表数据',
chartType varchar(128) null comment '图表类型',
genChart text null comment '生成的图表数据',
genResult text null comment '生成的分析结论',
status varchar(128) not null default 'wait' comment 'wait,running,succeed,failed',
execMessage text null comment '执行信息',
userId bigint null comment '创建用户 id',
createTime datetime default CURRENT_TIMESTAMP not null comment '创建时间',
updateTime datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间',
isDelete tinyint default 0 not null comment '是否删除'
) comment '图表信息表' collate = utf8mb4_unicode_ci;使用 idea 中的 database 连接 mysql 数据源查看执行 sql 后生成的表格如下:

封装通用类以及异常
封装统一的返回结果包装类

封装全局异常类

配置类

manager 通用 service

定义 controller 继承基础整合中的正删改查
- ChartController 的基础接口代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158/**
* 创建
*
* @param chartAddRequest
* @param request
* @return
*/
public BaseResponse<Long> addChart( ChartAddRequest chartAddRequest, HttpServletRequest request) {
if (chartAddRequest == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
Chart chart = new Chart();
BeanUtils.copyProperties(chartAddRequest, chart);
User loginUser = userService.getLoginUser(request);
chart.setUserId(loginUser.getId());
boolean result = chartService.save(chart);
ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
long newChartId = chart.getId();
return ResultUtils.success(newChartId);
}
/**
* 删除
*
* @param deleteRequest
* @param request
* @return
*/
public BaseResponse<Boolean> deleteChart( DeleteRequest deleteRequest, HttpServletRequest request) {
if (deleteRequest == null || deleteRequest.getId() <= 0) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
User user = userService.getLoginUser(request);
long id = deleteRequest.getId();
// 判断是否存在
Chart oldChart = chartService.getById(id);
ThrowUtils.throwIf(oldChart == null, ErrorCode.NOT_FOUND_ERROR);
// 仅本人或管理员可删除
if (!oldChart.getUserId().equals(user.getId()) && !userService.isAdmin(request)) {
throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
}
boolean b = chartService.removeById(id);
return ResultUtils.success(b);
}
/**
* 更新(仅管理员)
*
* @param chartUpdateRequest
* @return
*/
public BaseResponse<Boolean> updateChart( ChartUpdateRequest chartUpdateRequest) {
if (chartUpdateRequest == null || chartUpdateRequest.getId() <= 0) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
Chart chart = new Chart();
BeanUtils.copyProperties(chartUpdateRequest, chart);
long id = chartUpdateRequest.getId();
// 判断是否存在
Chart oldChart = chartService.getById(id);
ThrowUtils.throwIf(oldChart == null, ErrorCode.NOT_FOUND_ERROR);
boolean result = chartService.updateById(chart);
return ResultUtils.success(result);
}
/**
* 根据 id 获取
*
* @param id
* @return
*/
public BaseResponse<Chart> getChartById(long id, HttpServletRequest request) {
if (id <= 0) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
Chart chart = chartService.getById(id);
if (chart == null) {
throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
}
return ResultUtils.success(chart);
}
/**
* 分页获取列表(封装类)
*
* @param chartQueryRequest
* @param request
* @return
*/
public BaseResponse<Page<Chart>> listChartByPage( ChartQueryRequest chartQueryRequest,
HttpServletRequest request) {
long current = chartQueryRequest.getCurrent();
long size = chartQueryRequest.getPageSize();
// 限制爬虫
ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
Page<Chart> chartPage = chartService.page(new Page<>(current, size),
getQueryWrapper(chartQueryRequest));
return ResultUtils.success(chartPage);
}
/**
* 分页获取当前用户创建的资源列表
*
* @param chartQueryRequest
* @param request
* @return
*/
public BaseResponse<Page<Chart>> listMyChartByPage( ChartQueryRequest chartQueryRequest,
HttpServletRequest request) {
if (chartQueryRequest == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
User loginUser = userService.getLoginUser(request);
chartQueryRequest.setUserId(loginUser.getId());
long current = chartQueryRequest.getCurrent();
long size = chartQueryRequest.getPageSize();
// 限制爬虫
ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
Page<Chart> chartPage = chartService.page(new Page<>(current, size),
getQueryWrapper(chartQueryRequest));
return ResultUtils.success(chartPage);
}
// endregion
/**
* 编辑(用户)
*
* @param chartEditRequest
* @param request
* @return
*/
public BaseResponse<Boolean> editChart( ChartEditRequest chartEditRequest, HttpServletRequest request) {
if (chartEditRequest == null || chartEditRequest.getId() <= 0) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
Chart chart = new Chart();
BeanUtils.copyProperties(chartEditRequest, chart);
User loginUser = userService.getLoginUser(request);
long id = chartEditRequest.getId();
// 判断是否存在
Chart oldChart = chartService.getById(id);
ThrowUtils.throwIf(oldChart == null, ErrorCode.NOT_FOUND_ERROR);
// 仅本人或管理员可编辑
if (!oldChart.getUserId().equals(loginUser.getId()) && !userService.isAdmin(loginUser)) {
throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
}
boolean result = chartService.updateById(chart);
return ResultUtils.success(result);
}- 该代码中将每个请求体进行了单独的封装,以便为了前端能够更好地进行参数的传递。到此后端的基础初始化已经结束。
前端初始化
安装 ant desgin pro 脚手架
创建一个文件夹,打开 cmd 执行命令 pro create bi-frontend 创建项目
创建完成之后,执行 package.json 中的相关删除指令(遇到 bug 到 github 上 issue 中查找),删除用不到的功能,减少项目的负重。
找到 config 文件夹 中的 config.ts 修改以下代码:

生成后端文档接口的信息,自动生成 service 接口信息。
修改 app.tsx 中信息,添加 baseURL 改为自己的后端接口地址。至此前端的初始化完成。
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 keep初心!

