# 《AI智能客服系统》落地实现-第02节:数据模型与数据存储层的数据与实现

作者:冰河
星球:http://m6z.cn/6aeFbs (opens new window)
博客:https://binghe.gitcode.host (opens new window)
文章汇总:https://binghe.gitcode.host/md/all/all.html (opens new window)
源码获取地址:https://t.zsxq.com/0dhvFs5oR (opens new window)

沉淀,成长,突破,帮助他人,成就自我。

  • 本章难度:★☆☆☆☆
  • 本章重点:对AI智能客服系统的数据模型与数据存储层进行设计与实现,从全局角度掌握AI智能客服系统的整体项目结构。重点掌握AI智能客服系统的通用设计思路和设计方法,并能够将其灵活应用到自身实际项目中。

大家好,我是冰河~~

截止到目前,AI智能客服系统的所有准备工作都已经完毕,接下来,就到了真正手撸源码的环节了。

# 一、前言

搭建完项目主体结构后,接下来,就进入手撸源码环节了。AI智能客服系统后端服务主要分为数据模型、数据存储层、业务逻辑层和接口层。在实现上,我们将数据模型与数据存储层合并为一节,业务逻辑层与接口层合并为一节。接下来,我们就重点对数据模型与数据存储层进行设计与实现。

# 二、本节诉求

对AI智能客服系统的数据模型与数据存储层进行设计与实现,从全局角度掌握AI智能客服系统的整体项目结构。重点掌握AI智能客服系统的通用设计思路和设计方法,并能够将其灵活应用到自身实际项目中。

# 三、核心设计

注意:本节只给大家展示AI智能客服系统数据模型与数据存储层的核心类实现关系,其他代码的实现细节,大家可以自行到本节对应的源码分支进行查看,这里不再赘述。

AI智能客服系统数据模型与数据存储层的核心类设计如图2-1所示。


本节,主要涉及的类和接口有:AiRequest、AiResponse、ChatLogs和ChatLogsMapper。

  • AiRequest类:前端与后端交互的数据模型,主要负责接收前端传递的数据。
  • AiResponse类:前端与后端交互的数据模型,主要负责向前端响应后端的结果数据。
  • ChatLogs类:聊天记录实体模型,与数据表字段一一对应。
  • ChatLogsMapper接口:操作数据表的Mapper接口。

# 四、编码实现

本节只给大家展示AI智能客服系统数据模型与数据存储层的核心类编码实现,其他代码的实现细节,大家可以自行到本节对应的源码分支进行查看,这里不再赘述。

(1)实现ChatLogs类

ChatLogs类是聊天记录的实体模型类,与数据表字段一一对应。

源码详见:io.binghe.springai.bot.entity.ChatLogs。

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ChatLogs {
    private Long id;
    private String userRequest;
    private String aiResponse;
    private LocalDateTime createdTime;
    private String sessionId;
}

1
2
3
4
5
6
7
8
9
10
11
12

(2)实现AiRequest类

AiRequest类是前端与后端交互的数据模型类,主要负责接收前端传递的数据。

源码详见:io.binghe.springai.bot.request.AiRequest。

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class AiRequest {
    private String sessionId;
    private String message;
}
1
2
3
4
5
6
7
8

(3)实现AiResponse类

AiResponse类是前端与后端交互的数据模型类,主要负责向前端响应后端的结果数据。

源码详见:io.binghe.springai.bot.response.AiResponse。

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class AiResponse {

    private String sessionId;
    private String message;
    private Long responseTime;
    private boolean success;
    private String error;

    public AiResponse(String message, String sessionId, Long responseTime) {
        this.message = message;
        this.sessionId = sessionId;
        this.responseTime = responseTime;
        this.success = true;
    }

    public static AiResponse error(String error, String sessionId) {
        AiResponse response = new AiResponse();
        response.setError(error);
        response.setSessionId(sessionId);
        response.setSuccess(false);
        return response;
    }
}
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

(4)实现ChatLogsMapper接口

ChatLogsMapper接口是主要操作数据表的接口,AI智能客服系统的消息记录数据都是由ChatLogsMapper接口进行CRUD操作。

# 查看完整文章

加入冰河技术 (opens new window)知识星球,解锁完整技术文章、小册、视频与完整代码