3

使用C++的ORM框架QxORM - 李涛贤贤

 2 years ago
source link: https://www.cnblogs.com/Kevinsh-Lee/p/16412968.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

使用C++的ORM框架QxORM

QxORM中,我们用的最多的无非是这两点

官方表述是这样的:

  持久性: 支持最常见的数据库,如 SQLite、MySQL、PostgreSQL、Oracle、MS SQL Server、MongoDB(具有 1-1、1-n、n-1 和 n-n 关系);
  序列化: JSON、二进制和 XML 格式;

简洁一点就是:

  连接访问各类主流数据库

  能够将数据导入导出

下面我们进入主题:

  第一步:建立对象模型。相对于常规的结构体,只是多了一些宏定义与声明。

  头文件:QxModels.h

#include "precompiled.h"

/***************************************************************
* @projectName  pluqt
* @brief        自定义ORM模型
* @author       lzw
* @date         2022-01-04
***************************************************************/
struct User
{
    long id;
    QString name;
    int age;
    QString hobbies;
};

QX_REGISTER_HPP_QX_DLL1(User, qx::trait::no_base_class_defined, 1)

  源文件:QxModels.cpp

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

QX_REGISTER_CPP_QX_DLL1(User)

namespace qx
{
    template <> void register_class(QxClass<User> & t)
    {
        // 设置表名
        t.setName("User");
        // 注册 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");
        // 注册 User::hobbies 属性,使用的 key 是 hobbies。
        t.data(&User::hobbies, "hobbies");
    }
}

  第二步:连接数据库。

    QString in_db = QCoreApplication::applicationDirPath();
    in_db.append("/database/plulocal.db");

    QFile::remove(in_db);

    qx::QxSqlDatabase::getSingleton()->setDriverName("QSQLITE");
    qx::QxSqlDatabase::getSingleton()->setDatabaseName(in_db);
    qx::QxSqlDatabase::getSingleton()->setHostName("localhost");
    qx::QxSqlDatabase::getSingleton()->setUserName("root");
    qx::QxSqlDatabase::getSingleton()->setPassword("");
    qx::QxSqlDatabase::getSingleton()->setSqlPlaceHolderStyle(qx::QxSqlDatabase::ph_style_2_point_name);
    qx::QxSqlDatabase::getSingleton()->setTraceSqlQuery(true);
    qx::QxSqlDatabase::getSingleton()->setTraceSqlRecord(false);

  第三步:使用ORM。建表,新增,简单查询,复杂查询等等

    // 建表
    QSqlError daoError1 = qx::dao::create_table<User>();

    // 产生100条模拟数据
    for(int in_idx=0; in_idx<100; ++in_idx)
    {
        auto in_user = new User();
        in_user->name = "lzw"+QString::number(in_idx);
        in_user->age = 20+in_idx;
        in_user->hobbies = "play";

        auto daoError1 = qx::dao::insert(in_user);
    }

    // 查询单条记录
    User in_pointUser; in_pointUser.id = 3;
    qDebug()<<in_pointUser.name;
    QSqlError daoError11 = qx::dao::fetch_by_id(in_pointUser);
    qDebug()<<in_pointUser.name;

    // 查询一定年龄段的集合记录
    //typedef std::shared_ptr<User> UserPtr;
    //typedef qx::QxCollection<long, UserPtr> UserList;
    UserList in_userList;
    qx_query in_query("select * from user where age>=20 and age<=25");
    daoError11 = qx::dao::execute_query(in_query, in_userList);
    qAssert(! daoError11.isValid()); qAssert(in_userList.count() > 0);
    qx::dump(in_userList);

有图有真相。执行前:

1262411-20220626091839173-328726078.png

  执行后:(并不需要写一条sql语句,当然也支持写原生sql语句)

1262411-20220626092132104-1044711727.png

 补充:序列化的两个函数

    // 导出binary流
    qx::serialization::qt::to_file(in_pointUser, "user.txt");
    // 导出json文本
    qx::serialization::json::to_file(in_userList, "list_of_user.json");

  一气呵成,大功告成。各位很简单吧


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK