一、QxOrm框架之编译使用案例

本文介绍了QxOrm,一个适用于QT开发的C++ ORM框架,支持多种主流数据库。提供了框架的下载地址,并详细阐述了如何编译DEBUG版本,特别是批处理工具的使用。接着,通过步骤指导,展示了如何在新项目中设置QxOrm文件夹,配置项目文件,以及创建必要的头文件和源文件。
摘要由CSDN通过智能技术生成

一、框架简介

C++ 数据库 ORM 框架比较有代表性的有两个:ODB 和 QxOrm,若是使用 QT 开发,建议使用 QxOrm。QxOrm 几乎支持所有的主流数据库,比如 SQLite、MySQL、PostgreSQL、Oracle、MS SQL Server、MongoDB 等。

二、下载地址

参见 https://github.com/QxOrm/QxOrm

三、编译说明

下载完毕之后,目录如下:
在这里插入图片描述
我们重点关注 tools 文件夹,里面存放所有版本的编译批处理工具,如下:
在这里插入图片描述
这里以编译 DEBUG 版本的 qxorm 为例进行讲解,批处理工具为:mingw_build_all_debug_qt5_minimal.bat(PS:上面的 **full版本需要 boost 支持,这里不再演示,自行摸索)。
执行 mingw_build_all_debug_qt5_minimal.bat 之后,需要重点关注三个文件夹,如下:
在这里插入图片描述
这三个文件夹会在项目中使用。

四、使用案例

1.新建项目,在项目下新建 QxOrm 文件夹,文件夹里存放上面三个文件夹,如下:

在这里插入图片描述
在这里插入图片描述

2.项目配置文件如下:

#-------------------------------------------------
#
# Project created by QtCreator 2020-06-30T22:46:06
#
#-------------------------------------------------

QT       += core gui sql

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = ormDemo
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

#通过它可以知道项目是否正在编译
DEFINES += _BUILDING_USER

# 预编译头文件
!contains(DEFINES, _QX_NO_PRECOMPILED_HEADER) {
    PRECOMPILED_HEADER = precompiled.h
}

# QxOrm 库相关配置
INCLUDEPATH += $$PWD/QxOrm/include
LIBS += -L$$PWD/QxOrm/lib

# 设置生成的目标名称、添加依赖库
CONFIG(debug, debug|release) {
    LIBS += -lQxOrmd
} else {
    LIBS += -lQxOrm
}

SOURCES += \
        main.cpp \
        dialog.cpp \
    user.cpp

HEADERS += \
        dialog.h \
    export.h \
    precompiled.h \
    user.h

FORMS += \
        dialog.ui

3.export.h文件(必须)

#ifndef EXPORT_H
#define EXPORT_H

#ifdef _BUILDING_USER
#define USER_DLL_EXPORT  QX_DLL_EXPORT_HELPER
#else
#define USER_DLL_EXPORT  QX_DLL_IMPORT_HELPER
#endif

#ifdef _BUILDING_USER
#define QX_REGISTER_HPP_USER  QX_REGISTER_HPP_EXPORT_DLL
#define QX_REGISTER_CPP_USER  QX_REGISTER_CPP_EXPORT_DLL
#else
#define QX_REGISTER_HPP_USER  QX_REGISTER_HPP_IMPORT_DLL
#define QX_REGISTER_CPP_USER  QX_REGISTER_CPP_IMPORT_DLL
#endif

#endif // EXPORT_H

4.precompiled.h文件(必须)

#ifndef PRECOMPILED_H
#define PRECOMPILED_H

#include <QxOrm.h>
#include "export.h"

#endif // PRECOMPILED_H

5.user.h/user.cpp文件

user.h

#ifndef USER_H
#define USER_H

#include <QObject>
#include "precompiled.h"

class USER_DLL_EXPORT User
{
public:
    User() : id(0) { }
    virtual ~User() { }

    long id;
    QString name;
    int age;
};
//QX_REGISTER_PRIMARY_KEY(User, QString) //主键不是整数类型的时候使用
/*
 * 用于将 User 类注册到 QxOrm 的上下文中:
 * 参数一:表示要注册的当前类 - User;
 * 参数二:基类,如果没有基类,则使用 qx::trait::no_base_class_defined;
 * 参数三:用于序列化的类版本。
 */
QX_REGISTER_HPP_USER(User, qx::trait::no_base_class_defined, 1)


#endif // USER_H

user.cpp

#include "precompiled.h"
#include "user.h"
#include <QxOrm_Impl.h>

//和 QX_REGISTER_HPP_USER  相同,QX_REGISTER_CPP_USER 宏也是必需的,用于将 User 类注册到 QxOrm 的上下文中。
QX_REGISTER_CPP_USER(User)

//在 user.cpp 文件中,需要实现 qx::register_class(),它是一个设置函数:
namespace qx {
    template <> void register_class(QxClass<User> & t)
    {
        // 注册 User::id <=> 数据库中的主键
        t.id(&User::id, "id");

        // 注册 User::name 属性,使用的 key 是 name,version 是 1。
        t.data(&User::name, "name", 1);

        // 注册 User::age 属性,使用的 key 是 age。
        t.data(&User::age, "age");
    }
}

6.main.cpp文件

#include "precompiled.h"
#include "user.h"
/*
 *     QxOrm_Impl.h,它的作用是检测内存泄露。如果使用 QxMemLeak 模块或 boost::serialization 引擎,
 * 应该在所有的 *.cpp 中包含它;否则,它便是可选的(非必须)
 */
#include <QxOrm_Impl.h>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    // 初始化参数,用于和数据库交互(sqlite)
//    qx::QxSqlDatabase::getSingleton()->setDriverName("QSQLITE");
//    qx::QxSqlDatabase::getSingleton()->setDatabaseName("./Users.db");
//    qx::QxSqlDatabase::getSingleton()->setHostName("localhost");
//    qx::QxSqlDatabase::getSingleton()->setUserName("root");
//    qx::QxSqlDatabase::getSingleton()->setPassword("");

    // 配置 MySQL
    qx::QxSqlDatabase::getSingleton()->setDriverName("QMYSQL");
    qx::QxSqlDatabase::getSingleton()->setDatabaseName("test");
    qx::QxSqlDatabase::getSingleton()->setHostName("127.0.0.1");
    qx::QxSqlDatabase::getSingleton()->setUserName("root");
    qx::QxSqlDatabase::getSingleton()->setPassword("root");
    qx::QxSqlDatabase::getSingleton()->setPort(3306);


    // 在数据库中创建 User 表
    QSqlError daoError = qx::dao::create_table<User>();

    // 创建 3 个用户
    // 可以使用 std 和 Qt 智能指针:std::shared_ptr、QSharedPointer 等...
    typedef  QSharedPointer<User> UserPtr;
    UserPtr u1;
    u1.reset(new User());
    u1->name = "Jack Ma";
    u1->age = 30;

    UserPtr u2;
    u2.reset(new User());
    u2->name = "Pony";
    u2->age = 25;

    UserPtr u3;
    u3.reset(new User());
    u3->name = "Waleon";
    u3->age = 18;

    // 将所有用户插入容器中
    // 可以使用 std、boost、Qt 和 qx::QxCollection<Key,Value> 中的许多容器
    typedef QVector<UserPtr> VectorUser;
    VectorUser users;
    users.push_back(u1);
    users.push_back(u2);
    users.push_back(u3);

    // 将容器中的所有用户插入到数据库中
    // p1、p2、p3 的 id 属性会自动更新
    daoError = qx::dao::insert(users);

    // 修改第二个用户的信息,并更新到数据库中
    u2->name = "Pony modified";
    u2->age = 38;
    daoError = qx::dao::update(u2);

    // 从数据库中删除第一个用户
    daoError = qx::dao::delete_by_id(u1);

    // 计算用户的数量
    long userCount = qx::dao::count<User>();
    qDebug() << "User Count: " << userCount;

    // 将 id 为 3 的用户取出,并传给一个新变量
    UserPtr userTmp;
    userTmp.reset(new User());
    userTmp->id = 3;
    daoError = qx::dao::fetch_by_id(userTmp);
    qDebug() << "User Tmp: " << userTmp->id << userTmp->name << userTmp->age;

#if _QX_SERIALIZE_XML
    // 将容器中的用户导出到 XML 文件中(序列化)
    qx::serialization::xml::to_file(users, "./export_users.xml");

    // 将 XML 中的用户导入至新容器
    VectorUser usersXmlTmp;
    qx::serialization::xml::from_file(usersXmlTmp, "./export_users.xml");
#endif

#ifndef _QX_NO_JSON
    // 将容器中的用户导出到 Json 文件中(序列化)
    qx::serialization::json::to_file(users, "./export_users.json");

    // 将 Json 文件中的用户导入至新容器
    VectorUser usersJsonTmp;
    qx::serialization::json::from_file(usersJsonTmp, "./export_users.json");
#endif

    // 克隆一个用户
    UserPtr uClone = qx::clone_to_qt_shared_ptr(*u1);
    qDebug() << "Clone from u1: " << uClone->id << uClone->name << uClone->age;

    // 按类名(factory)创建新用户
    qx::any uAny = qx::create("User");

    // 将用户插入到 qx::cache
    qx::cache::set("users", users);

    // 从 qx::cache 中删除所有元素
    qx::cache::clear();

    // 内存泄漏
//    User *user = new User();

    return a.exec();
}

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值