当前位置: 首页 > 工具软件 > QxOrm > 使用案例 >

QxOrm框架

宫晟
2023-12-01

目录

 一、设置.pro文件中的include、lib路径与include(QxOrm.pri)

 二、头文件与宏函数注册

 三、cpp文件宏注册与模板实现

四、main函数实现


 一、设置.pro文件中的include、lib路径与include(QxOrm.pri)

QT -= gui

CONFIG += c++11 console
CONFIG -= app_bundle

include(QxOrm.pri)
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked 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

INCLUDEPATH += ./include

# You can also make your code fail to compile if it uses 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

SOURCES += \
        main.cpp \
    person.cpp

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

HEADERS += \
    person.h

unix|win32: LIBS += -L$$PWD/lib/ -lQxOrmd

 二、头文件与宏函数注册

#ifndef PERSON_H
#define PERSON_H

#include <QxOrm.h>
#include <QString>

class QX_DLL_EXPORT_HELPER Person
{
    QX_REGISTER_FRIEND_CLASS(Person)

    public:
        Person() : id(0) {}

    long id;

    QString name;
};

QX_REGISTER_HPP_EXPORT_DLL(Person, qx::trait::no_base_class_defined, 1);

#endif // PERSON_H

 三、cpp文件宏注册与模板实现

#include "person.h"

#include <QxOrm_Impl.h>

QX_REGISTER_CPP_EXPORT_DLL(Person)

namespace qx{
template<> void register_class(QxClass<Person> & t)
{
    t.id(&Person::id, "id");

    t.data(&Person::name, "name");
}
}

四、main函数实现

#include <QCoreApplication>

#include "person.h"

//#include <QxOrm.h>
//#include <QxOrm_Impl.h>

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

    //数据库连接
    qx::QxSqlDatabase::getSingleton()->setDriverName("QSQLITE");
    qx::QxSqlDatabase::getSingleton()->setDatabaseName("Test.db");

    //建立表,如存在可省略
    QSqlError error = qx::dao::create_table<Person>();

    typedef QSharedPointer<Person> PersonPointer;
//    PersonPointer p1;
//    p1.reset(new Person());
//    p1->name = "Jack";

//    PersonPointer p2;
//    p2.reset(new Person());
//    p2->name = "Mark";

//    PersonPointer p3;
//    p3.reset(new Person());
//    p3->name = "Jerry";

//    QVector<PersonPointer> vect;
//    vect.append(p1);
//    vect.append(p2);
//    vect.append(p3);

    PersonPointer ptr;
    ptr.reset(new Person());
    ptr->id = 4;
    error = qx::dao::fetch_by_id(ptr);
    qx_query que;
    que.where("name").isEqualTo("Jerry");
    qx_query que2;
    que2.where("id").isGreaterThanOrEqualTo(2);
    QList<Person> list {};
    error = qx::dao::fetch_by_query(que2, list);
    for(auto i : list)
        qDebug()<<i.id<<endl;


    return a.exec();
}

参考:https://blog.csdn.net/liang19890820/article/details/105071540

 类似资料: