当前位置:网站首页>集群聊天服务器:Model数据层的框架设计和数据库代码的封装
集群聊天服务器:Model数据层的框架设计和数据库代码的封装
2022-07-23 21:17:00 【_索伦】
框架设计
User, Group, GroupUser是和用户相关操作,即设置或返回用户、群组或群成员的信息。而Model代表着对数据库的增删改查操作。

各个类的功能
User
User即就是可以管理用户的信息
#ifndef USER_H
#define USER_H
#include <string>
using namespace std;
class User
{
public:
User(int id = -1, string name = "", string password = "", string state = "offline")
: m_id(id), m_name(name), m_password(password), m_state(state)
{
}
void setId(int id) {
this->m_id = id; }
void setName(string name) {
this->m_name = name; }
void setPwd(string password) {
this->m_password = password; }
void setState(string state) {
this->m_state = state; }
int getId() const {
return this->m_id; }
string getName() const {
return this->m_name; }
string getPwd() const {
return this->m_password; }
string getState() const {
return this->m_state; }
protected:
int m_id;
string m_name;
string m_password;
string m_state;
};
#endif
GroupUser
这个类是针对群组成员,那么只需要继承User类,多添加一个身份信息,就可得知某个用户是管理员还是普通成员。
#ifndef GROUPUSER_H
#define GROUPUSER_H
#include "user.hpp"
// 群组用户,多了一个role角色信息(管理员或普通成员)
// 从User类直接继承,复用User的其他信息
class GroupUser : public User
{
public:
void setRole(string role) {
this->role = role; }
string getRole() {
return this->role; }
private:
string role;
};
#endif
Group
群组类的设计也比较简单,只有群组id,群名称和群描述。
#ifndef GROUP_H
#define GROUP_H
#include "groupUser.hpp"
#include <vector>
#include <string>
using namespace std;
// User表的ORM类
class Group
{
public:
Group(int id = -1, string name = "", string desc = "")
{
this->id = id;
this->name = name;
this->desc = desc;
}
void setId(int id) {
this->id = id; }
void setName(string name) {
this->name = name; }
void setDesc(string desc) {
this->desc = desc; }
int getId() const {
return this->id; }
string getName() const {
return this->name; }
string getDesc() const {
return this->desc; }
vector<GroupUser>& getUsers() {
return this->users;}
private:
int id;
string name;
string desc;
vector<GroupUser> users;
};
#endif
UserModel
Model类都是对数据库的操作,即增删改查,这里有个头文件是数据库连接池,稍后介绍。
#ifndef USERMODEL_H
#define USERMODEL_H
#include "user.hpp"
#include "dbConnectionPool.h"
extern ConnectionPool* sp;
// User表的数据操作类
class UserModel {
public:
// User表的增加操作
bool insert(User& user);
// 根据用户号码返回用户信息
User query(int id);
// 更新用户的状态信息
bool updateState(User user);
// 重置用户得状态信息
void resetState();
};
#endif
GroupModel
#ifndef GROUPMODEL_H
#define GROUPMODEL_H
#include "group.hpp"
using namespace std;
// 维护群组信息的操作接口方法
class GroupModel
{
public:
// 创建群组
bool createGroup(Group& group);
// 加入群组
void addGroup(int userid, int groupid, string role);
// 查询用户所在群组信息
vector<Group> queryGroups(int userid);
// 根据指定的groupid查询群组用户id列表
// 除了userid自己,主要用户群聊业务给群组其他成员群发消息
vector<int> queryGroupUsers(int userid, int groupid);
};
#endif
FriendModel
#ifndef FRIENDMODEL_H
#define FIRENDMODEL_H
#include "user.hpp"
#include <string>
#include <vector>
using namespace std;
// 维护好友信息的操作接口方法
class FriendModel
{
public:
// 添加好友关系
void insert(int userid, int friendid);
// 返回用户好友列表 两个表的联合查询
vector<User> query(int userid);
};
#endif
OfflineMsgModel
#ifndef OFFLINEMESSAGEMODEL_H
#define OFFLINEMESSAGEMODEL_H
#include "dbConnectionPool.h"
#include <string>
#include <vector>
using namespace std;
extern ConnectionPool* sp;
// 提供离线消息表的操作接口方法
class OfflineMsgModel
{
public:
// 存储用户的离线消息
void insert(int userId, string msg);
// 删除用户的离线消息
void remove(int userId);
// 查询用户的离线消息
vector<string> query(int userId);
};
#endif
使用数据库连接池
为了提高MySQL数据库(基于C/S设计)的访问瓶颈,除了在服务器端增加缓存服务器缓存常用的数据之外(例如redis),还可以增加连接池,来提高MySQL Server的访问效率,在高并发情况下,大量的TCP三次握手、MySQL Server连接认证、MySQL Server关闭连接回收资源和TCP四次挥手所耗费的性能时间也是很明显的,增加连接池就是为了减少这一部分的性能损耗。
连接池的实现:【数据库连接池】
该连接池是单例模式的
// 获取连接池对象实例 静态的
static ConnectionPool* getConnectionPool();
// 给外部提供接口,提供一个空闲的连接
shared_ptr<Connection> getConnection();
// 初始化连接池
ConnectionPool* sp = ConnectionPool::getConnectionPool();
在连接池的源文件初始化连接池,因为要在Model层对数据库操作,所以在要对访问MySQL的文件使用extern引入该连接池,示例:
extern ConnectionPool* sp

边栏推荐
- [wechat applet] do you know about applet development?
- Network learning infrared module, 8-way emission independent control
- Junior intern, ByteDance, after sharing, has been offered
- 1309_STM32F103上增加GPIO的翻转并用FreeRTOS调度测试
- OpenCV图像处理——拉普拉斯金字塔
- High numbers | calculation of double integral 2 | high numbers | handwritten notes
- 数据库压力测试方法小结
- 模块化开发
- High numbers | calculation of double integral 3 | high numbers | handwritten notes
- Pay more attention to which securities company has the lowest commission? Is it safe to open an account online?
猜你喜欢

Network learning infrared module, 8-way emission independent control

Unity解决动画不可用:The AnimationClip ‘XXX‘ used by the Animation component ‘XXX‘ must be marked as Legacy.
![[Yugong series] June 2022.Net architecture class 084- micro service topic ABP vNext micro service communication](/img/29/b73edbdb2409f40c904d126f9185d1.png)
[Yugong series] June 2022.Net architecture class 084- micro service topic ABP vNext micro service communication

Chapter 2 回归

221. Largest square ● &1277. Square submatrix with statistics all 1 ● ●

手机测试相关基础知识

Chapter1 数据清洗

【着色器实现RoundWave圆形波纹效果_Shader效果第六篇】

High numbers | calculation of double integral 4 | high numbers | handwritten notes

SQLite database
随机推荐
UnauthorizedAccessException:Access to the path “/xx/xx.xx“ is denied
Broadcast (broadcast)
第三届SLAM技术论坛-吴毅红教授
手机测试相关基础知识
Oom mechanism
ES6 feature: Promise (custom encapsulation)
scala編程(初級)
1063 Set Similarity
数据库压力测试方法小结
现在完全不知道怎么同步
Vrrp+mstp configuration details [Huawei ENSP experiment]
Unity—3D数学-Vector3
Stm32c8t6 driven lidar (I)
[Yunxiang book club No. 13] Chapter IV packaging format and coding format of audio files
做一个有职业操守的软件匠人
Protocol buffers 的问题和滥用
Ssm+mysql to realize snack mall system (e-commerce shopping)
Green-Tao 定理的证明 (1): 准备, 记号和 Gowers 范数
分布式能源的不确定性——风速测试(Matlab代码实现)
WinDbg实践--入门篇