如何使用C++的ORM框架QxORM
导读:本文共2159.5字符,通常情况下阅读需要7分钟。同时您也可以点击右侧朗读,来听本文内容。按键盘←(左) →(右) 方向键可以翻页。
摘要: QxORM中,我们用的最多的无非是这两点官方表述是这样的:持久性:支持最常见的数据库,如SQLite、MySQL、PostgreSQL、Oracle、MS SQL Server、MongoDB(具有 1-1、1-n、n-1 和 n-n 关系);序列化:JSON、二进制和 XML 格式;简洁一点就是:连接访问各类主流数据库能够将数据导入导出下面我们进入主题:第一步... ...
音频解说
目录
(为您整理了一些要点),点击可以直达。QxORM中,我们用的最多的无非是这两点
官方表述是这样的:
持久性:支持最常见的数据库,如SQLite、MySQL、PostgreSQL、Oracle、MS SQL Server、MongoDB(具有 1-1、1-n、n-1 和 n-n 关系);
序列化:JSON、二进制和 XML 格式;
简洁一点就是:
连接访问各类主流数据库
能够将数据导入导出
下面我们进入主题:
第一步:建立对象模型。相对于常规的结构体,只是多了一些宏定义与声明。
头文件:QxModels.h
#include"precompiled.h"/****************************************************************@projectNamepluqt*@brief自定义ORM模型*@authorlzw*@date2022-01-04***************************************************************/structUser{longid;QStringname;intage;QStringhobbies;};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)namespaceqx{template<>voidregister_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");}}
第二步:连接数据库。
QStringin_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。建表,新增,简单查询,复杂查询等等
//建表QSqlErrordaoError1=qx::dao::create_table<User>();//产生100条模拟数据for(intin_idx=0;in_idx<100;++in_idx){autoin_user=newUser();in_user->name="lzw"+QString::number(in_idx);in_user->age=20+in_idx;in_user->hobbies="play";autodaoError1=qx::dao::insert(in_user);}//查询单条记录Userin_pointUser;in_pointUser.id=3;qDebug()<<in_pointUser.name;QSqlErrordaoError11=qx::dao::fetch_by_id(in_pointUser);qDebug()<<in_pointUser.name;//查询一定年龄段的集合记录//typedefstd::shared_ptr<User>UserPtr;//typedefqx::QxCollection<long,UserPtr>UserList;UserListin_userList;qx_queryin_query("select*fromuserwhereage>=20andage<=25");daoError11=qx::dao::execute_query(in_query,in_userList);qAssert(!daoError11.isValid());qAssert(in_userList.count()>0);qx::dump(in_userList);
有图有真相。执行前:
执行后:(并不需要写一条sql语句,当然也支持写原生sql语句)
补充:序列化的两个函数
//导出binary流qx::serialization::qt::to_file(in_pointUser,"user.txt");//导出json文本qx::serialization::json::to_file(in_userList,"list_of_user.json");
</div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:
如何使用C++的ORM框架QxORM的详细内容,希望对您有所帮助,信息来源于网络。