VS2022项目 集成 QxOrm框架

一、QxOrm框架的编译环境是VS2012

下载完QxOrm源程序后,可以直接用VS2022打开QxOrm项目进行编译,该项目编译的环境如下图:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
该项目可以直接编译,生成项目dll文件。

二、建立VS2022项目,将QxOrm.dll集成到新项目中

建立VS2022新项目:QtConsoleApplication1。
项目配置如下:
在这里插入图片描述

1、配置QxOrm包含目录和库目录

在这里插入图片描述

2、配置链接器依赖项

在这里插入图片描述

3、建立预编译头文件export.h

由于时间关系,此处借用qxBlog项目的预编译头文件了

#ifndef _QX_BLOG_EXPORT_H_
#define _QX_BLOG_EXPORT_H_

#ifdef _BUILDING_QX_BLOG
#define QX_BLOG_DLL_EXPORT QX_DLL_EXPORT_HELPER
#else // _BUILDING_QX_BLOG
#define QX_BLOG_DLL_EXPORT QX_DLL_IMPORT_HELPER
#endif // _BUILDING_QX_BLOG

#ifdef _BUILDING_QX_BLOG
#define QX_REGISTER_HPP_QX_BLOG     QX_REGISTER_HPP_EXPORT_DLL
#define QX_REGISTER_CPP_QX_BLOG     QX_REGISTER_CPP_EXPORT_DLL
#else // _BUILDING_QX_BLOG
#define QX_REGISTER_HPP_QX_BLOG     QX_REGISTER_HPP_IMPORT_DLL
#define QX_REGISTER_CPP_QX_BLOG     QX_REGISTER_CPP_IMPORT_DLL
#endif // _BUILDING_QX_BLOG

#endif // _QX_BLOG_EXPORT_H_

4、配置预编译定义

在这里插入图片描述

5、定义实体类头文件author.h

#ifndef _QX_BLOG_AUTHOR_H_
#define _QX_BLOG_AUTHOR_H_

class QX_BLOG_DLL_EXPORT author
{
public:
	// -- typedef
	
	// -- enum
	enum enum_sex { male, female, unknown };
	// -- properties
	QString     m_id;
	QString     m_name;
	QDate       m_birthdate;
	enum_sex    m_sex;
	// -- contructor, virtual destructor
	author() : m_id("0"), m_sex(unknown) { ; }
	virtual ~author() { ; }
	// -- methods
	int age() const;
};

//QX_REGISTER_PRIMARY_KEY(author, QString)
QX_REGISTER_HPP_QX_BLOG(author, qx::trait::no_base_class_defined, 0)

typedef std::shared_ptr<author> author_ptr;
typedef qx::QxCollection<QString, author_ptr> list_author;

#endif // _QX_BLOG_AUTHOR_H_

6、定义实体类

#include "../include/precompiled.h"

#include "../include/author.h"

#include <QxOrm_Impl.h>

QX_REGISTER_CPP_QX_BLOG(author)

namespace qx {
	template <> void register_class(QxClass<author>& t)
	{
		t.data(&author::m_id, "author_id");

		t.data(&author::m_name, "name");
		t.data(&author::m_birthdate, "birthdate");
		t.data(&author::m_sex, "sex");

		t.fct_0<int>(std::mem_fn(&author::age), "age"); 
	}
}

int author::age() const
{
	if (!m_birthdate.isValid()) { return -1; }
	return (QDate::currentDate().year() - m_birthdate.year());
}

7、编写main.cpp

#include <QtCore/QCoreApplication>
#include "./include/precompiled.h"
#include "./include/author.h"


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

    // Parameters to connect to database
    qx::QxSqlDatabase::getSingleton()->setDriverName("QMYSQL");
    qx::QxSqlDatabase::getSingleton()->setDatabaseName("tky_lve");
    qx::QxSqlDatabase::getSingleton()->setHostName("192.168.236.128");
    qx::QxSqlDatabase::getSingleton()->setUserName("root");
    qx::QxSqlDatabase::getSingleton()->setPassword("root");
    qx::QxSqlDatabase::getSingleton()->setPort(3306);

    qx::QxSqlDatabase::getSingleton()->setFormatSqlQueryBeforeLogging(false);
    qx::QxSqlDatabase::getSingleton()->setDisplayTimerDetails(false);

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

    // Create all tables in database
    QSqlError daoError;
    //daoError = qx::dao::create_table<author>();

    // Create a list of 3 author
    author_ptr author_1; author_1.reset(new author());
    author_ptr author_2; author_2.reset(new author());
    author_ptr author_3; author_3.reset(new author());

    author_1->m_id = "author_id_1"; author_1->m_name = QStringLiteral("张三");
    author_1->m_sex = author::male; author_1->m_birthdate = QDate::currentDate();
    author_2->m_id = "author_id_2"; author_2->m_name = QStringLiteral("李四");
    author_2->m_sex = author::female; author_2->m_birthdate = QDate::currentDate();
    author_3->m_id = "author_id_3"; author_3->m_name = "author_3";
    author_3->m_sex = author::female; author_3->m_birthdate = QDate::currentDate();

    list_author authorX;
    authorX.insert(author_1->m_id, author_1);
    authorX.insert(author_2->m_id, author_2);
    authorX.insert(author_3->m_id, author_3);

    // Insert list of 3 author into database
    daoError = qx::dao::insert(authorX,0,true);



    qx_query testStoredProcBis("SELECT * FROM author");
    authorX.clear();
    daoError = qx::dao::execute_query(testStoredProcBis, authorX);
    qAssert(!daoError.isValid()); qAssert(authorX.count() > 0);
    qx::serialization::json::to_file(authorX, "./export_usersall.json");

    qx::dump(authorX, false);
    //qx::dump(authorX, true);
    qDebug() << authorX.size();
    author_ptr authorTmp;
    authorTmp.reset(new author());
    
    qx::QxSqlQuery query("WHERE author.sex = :sex");
    query.bind(":sex", "0");

    list_author list_of_name_author;
    // 查询单条
    //daoError = qx::dao::fetch_by_query(query, list_of_name_author);
    qx::dump(list_of_name_author, false);
    qx::serialization::json::to_file(list_of_name_author, "./export_users.json");
    // 查询多条
    qx_query in_query("select * from author where sex='0'");
    list_of_name_author.clear();
    daoError = qx::dao::execute_query(in_query, list_of_name_author);
    qx::dump(list_of_name_author, false);
    qDebug() << list_of_name_author.size();

    
    return a.exec();
}

8、完成,测试程序

在这里插入图片描述

9、Release模式下按照上述配置即可

10、Debug模式下尚存在问题

进行单条数据查询时,出现如下错误,仍没有解决。

list_author list_of_name_author;
 // 查询单条
 daoError = qx::dao::fetch_by_query(query, list_of_name_author);
 qx::dump(list_of_name_author, false);
 qx::serialization::json::to_file(list_of_name_author, "./export_users.json");

在这里插入图片描述
如有能够解决该问题同仁,评论区见。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值