QxOrm框架

目录

 一、设置.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

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值