1015.利用QxOrm库操作数据库示例代码

QxOrm 示例代码

承接上一遍的QxOrm框架开发环境搭建,本篇幅梳理一下QxOrm示例,互联网资料并不多,主要还是在参考官方示例代码的基础上,写了本篇的示例小demo.

1 ORM介绍

对象关系映射(Object Relational Mapping,简称ORM) 是通过使用描述对象和数据库之间映射的元数据,将面向对象语言程序中的对象自动持久化到关系数据库中。本质上就是将数据从一种形式转换到另外一种形式。 这也同时暗示着额外的执行开销;然而,如果ORM作为一种中间件实现,则会有很多机会做优化,而这些在手写的持久层并不存在。 更重要的是用于控制转换的元数据需要提供和管理;但是同样,这些花费要比维护手写的方案要少;而且就算是遵守ODMG规范的对象数据库依然需要类级别的元数据。

2 定义产品类product

product.h 头文件如下

#pragma once

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

/**
 * 自定义一个产品类.add by xhome 2020/3/11
 * QX_PRODUCT_DLL_EXPORT 导出符号必须存在,因为我们自定义的类需要给dao层调用.
 */
class QX_PRODUCT_DLL_EXPORT product {
public:
	long  id;  //id
	QString name; //名称
	QString description; //描述

	product():id(0) { ; }

	virtual ~product() { ; }

};

QX_REGISTER_HPP_QX_PRODUCT(product, qx::trait::no_base_class_defined, 0)

typedef std::shared_ptr<product> product_ptr;
typedef std::vector<product_ptr> list_product;

product.cpp 文件如下

#include <QxOrm_Impl.h>
#include "product.h"
#include "precompiled.h"
//
//注册cpp 类
namespace qx {
	template <> void register_class(QxClass<product> & t)
	{
		//生成表结构schema, 创建表格时回调使用.
		t.id(&product::id, "id", 0);
		t.data(&product::name, "name", 0);
		t.data(&product::description, "desc", 0);
	}
}

3 预编译头文件与导出库

export.h 文件如下

#pragma once

#ifdef _BUILDING_QX_PRODUCT
#define QX_PRODUCT_DLL_EXPORT QX_DLL_EXPORT_HELPER
#else // _BUILDING_QX_BLOG
#define QX_PRODUCT_DLL_EXPORT QX_DLL_IMPORT_HELPER
#endif // _BUILDING_QX_BLOG


#ifdef _BUILDING_QX_PRODUCT
#define QX_REGISTER_HPP_QX_PRODUCT     QX_REGISTER_HPP_EXPORT_DLL
#define QX_REGISTER_CPP_QX_PRODUCT     QX_REGISTER_CPP_EXPORT_DLL
#else // _BUILDING_QX_PRODUCT
#define QX_REGISTER_HPP_QX_PRODUCT     QX_REGISTER_HPP_IMPORT_DLL
#define QX_REGISTER_CPP_QX_PRODUCT     QX_REGISTER_CPP_IMPORT_DLL
#endif // _BUILDING_QX_PRODUCT

precompiled.h 文件如下

#pragma once

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

4 主函数简单调用

main.cpp 文件如下

#include "testQxORM.h"
#include <QtWidgets/QApplication>
#include <QStringLiteral>

#include <QxOrm_Impl.h>
#include "product.h"

int main(int argc, char *argv[])
{
	QApplication a(argc, argv);
	
	QFile::remove("./product.sqlite");
	
	// Parameters to connect to database
	qx::QxSqlDatabase::getSingleton()->setDriverName("QSQLITE");
	qx::QxSqlDatabase::getSingleton()->setDatabaseName("./product.sqlite");
	qx::QxSqlDatabase::getSingleton()->setHostName("localhost");
	qx::QxSqlDatabase::getSingleton()->setUserName("root");
	qx::QxSqlDatabase::getSingleton()->setPassword("");
	qx::QxSqlDatabase::getSingleton()->setFormatSqlQueryBeforeLogging(true);
	qx::QxSqlDatabase::getSingleton()->setDisplayTimerDetails(true);

	// Only for debug purpose : assert if invalid offset detected fetching a relation
	qx::QxSqlDatabase::getSingleton()->setVerifyOffsetRelation(true);

	//创建数据库表.
	QSqlError daoError = qx::dao::create_table<product>();

	product_ptr product_1;
	product_ptr product_2;

	//智能指针托管.将普通指针转换为智能指针,由系统进行维护管理.
	product_1.reset(new product());
	product_1->id = 1;
	product_1->name = QStringLiteral("苹果");
	product_1->description = QStringLiteral("熟透了的红苹果");

	product_2.reset(new product());
	product_2->id = 2;
	product_2->name = QStringLiteral("香蕉");
	product_2->description = QStringLiteral("很新鲜的香蕉");

	list_product productX;

	productX.push_back(product_1);
	productX.push_back(product_2);

	daoError = qx::dao::insert(productX);


	return 0;
}

5 运行效果图

运行程序生成数据库product.sqlite ,通过navcat 软件查看数据表如下效果.
在这里插入图片描述

6 项目配置上需要注意点

项目结构如下
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

发布了53 篇原创文章 · 获赞 13 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/xiongjia516/article/details/104811951