最近在用MySQL数据库,QT提供了访问MySQL数据库的插件,但每次创建新表都要编写访问数据库的SQL语句,觉得很麻烦。前几天无意中发现了ORM开发方法。于是就想自己测试一下。没想到只是让QxORM中的Quick Sample例子运行起来,都用了差不多一个星期,当然其中也学到了很多东西。
对象关系映射(Object Relation Mapping,简称ORM),ORM简化了数据库查询过程,通过ORM持久层,用户不需要知道数据库的具体结构,就可以访问数据库。
由于我最近在使用QT,所以选了QxORM这个ORM产品。由于QxORM使用到了boost这个准C++库,所以编译QxORM之前要先安装boost。
1 测试环境
操作系统:win7
开发平台:Qt
2 boost的下载和编译
2.1 boost的下载
我开始选择了最新版boost_1_54_0,但在编译QxORM时出现了一系列的错误,后来为了排除错误就改为QxOrm 1.2.5使用的默认版本boost_1_42_0。Boost很多功能不用编译就可以直接使用,简单的测试如下。
#include <boost/timer.hpp> #include <QDebug> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); boost::timer tmr; qDebug() << tmr.elapsed() << "S" << endl; return a.exec(); }
当然不要忘了在QT的pro工程文件中添加boost源码的路径。
2.2 boost的编译
QxORM使用到了boost的serialization库,则需要单独编译该库。首先运行boost-1_42_0目录下的bootstrap.bat文件生成bjam.exe ,如果生成失败则可能是找不到C++编译器,我是直接装了VS2010(不知道gcc可不可以,没测过)编译命令为:
>bjam --with-serialization runtime-link=shared link=shared
但是默认情况下是使用VS的编译器,使用QtCreator+MinGW不能使用该编译器编译出来的库,所以我们要指定编译器,上面的命令改为:
>bjam toolset=gcc --with-serialization runtime-link=shared link=shared
其中link=shared link=shared表示生成动态运行库、动态链接库,在这里需要生成动态库,因为QxORM的例子是使用动态链接库,如果对静态链接感兴趣,则另外研究。
编译成功后会在stage/lib下生成很多lib,其中就有release版本的boost_serialization-mgw44-mt-1_42.dll、boost_serialization-mgw44-mt-1_42.lib以及debug版本的boost_serialization-mgw44-mt-d-1_42.dll、boost_serialization-mgw44-mt-d-1_42.lib。简单测试如下:
#include <boost/archive/text_oarchive.hpp> #include <iostream> #include <fstream> void save(){ std::ofstream file("archive.txt"); boost::archive::text_oarchive oa(file); std::string s = "Hello World!\n"; oa << s; } int main(int argc, char *argv[]){ QApplication a(argc, argv); MainWindow w; w.show(); save();
return a.exec();
}
在QT的pro工程文件中添加boost源码的路径以及boost_serialization-mgw44-mt-1_42.库,具体添加方法可以参考下面的例子。
3 QxORM的下载和编译
下载QxOrm 1.2.5,用Qt creatorn 打开QxORM根目录的工程,修改该工程里面的QxOrm.pri配置文件。
把
isEmpty(QX_BOOST_INCLUDE_PATH) { QX_BOOST_INCLUDE_PATH = $$quote(D:/Dvlp/_Libs/Boost/1_42/include) } isEmpty(QX_BOOST_LIB_PATH) { QX_BOOST_LIB_PATH = $$quote(D:/Dvlp/_Libs/Boost/1_42/lib_shared) } isEmpty(QX_BOOST_LIB_SERIALIZATION_DEBUG) { QX_BOOST_LIB_SERIALIZATION_DEBUG = "boost_serialization-vc90-mt-gd-1_42" } isEmpty(QX_BOOST_LIB_SERIALIZATION_RELEASE) { QX_BOOST_LIB_SERIALIZATION_RELEASE = "boost_serialization-vc90-mt-1_42" }
修改为:
isEmpty(QX_BOOST_INCLUDE_PATH) { QX_BOOST_INCLUDE_PATH = $$quote(D:/boost_1_42_0) } isEmpty(QX_BOOST_LIB_PATH) { QX_BOOST_LIB_PATH = $$quote(D:/boost_1_42_0/stage/lib) } isEmpty(QX_BOOST_LIB_SERIALIZATION_DEBUG) { QX_BOOST_LIB_SERIALIZATION_DEBUG = "boost_serialization-mgw44-mt-d-1_42" } isEmpty(QX_BOOST_LIB_SERIALIZATION_RELEASE) { QX_BOOST_LIB_SERIALIZATION_RELEASE = "boost_serialization-mgw44-mt-1_42" }
当然要根据自己实际的路径配置,这里只是给出一个例子。
分别编译release和debug两个版本,把生成的libQxOrm.a,QxOrm.dll(release版本)libQxOrmd.a,QxOrmd.dll(debug版本)四个文件拷贝到QxORM根目录的lib文件夹下。
4 QxORM的Quick Sample测试
新建Qt工程QxORMQuickSample,在该工程下新建文件夹QxORM,在QxORM文件夹下新建drug.h,drug.cpp,export.h,precomplied.h四个文件。
* ----------------------------------------------------------------------------------------------------- * 1- drug.h file : drug class definition with 3 properties : id, name and description * ----------------------------------------------------------------------------------------------------- #ifndef _CLASS_DRUG_H_ #define _CLASS_DRUG_H_ class drug { public: long id; QString name; QString description; drug() : id(0) { ; } virtual ~drug() { ; } }; QX_REGISTER_HPP_MY_TEST_EXE(drug, qx::trait::no_base_class_defined, 1) /* This macro is necessary to register 'drug' class in QxOrm context */ /* param 1 : the current class to register => 'drug' */ /* param 2 : the base class, if no base class, use the qx trait => 'qx::trait::no_base_class_defined' */ /* param 3 : the class version used by serialization to provide 'ascendant compatibility' */ #endif // _CLASS_DRUG_H_ * ---------------------------------------------------------------------------------------------------- * 2- drug.cpp file : 'setting function' implementation : void qx::register_class() * ---------------------------------------------------------------------------------------------------- #include "precompiled.h" // Precompiled-header with '#include <QxOrm.h>' and '#include "export.h"' #include "drug.h" // Class definition 'drug' #include <QxMemLeak.h> // Automatic memory leak detection QX_REGISTER_CPP_MY_TEST_EXE(drug) // This macro is necessary to register 'drug' class in QxOrm context namespace qx { template <> void register_class(QxClass<drug> & t) { t.id(& drug::id, "id"); // Register 'drug::id' <=> primary key in your database t.data(& drug::name, "name", 1); // Register 'drug::name' property with key 'name' and version '1' t.data(& drug::description, "desc"); // Register 'drug::description' property with key 'desc' }} * ---------------------------------------------------------------------------------------------------- * 3- export.h file * ---------------------------------------------------------------------------------------------------- #ifndef _QX_QUICK_SAMPLE_EXPORT_H_ #define _QX_QUICK_SAMPLE_EXPORT_H_ #define QX_REGISTER_HPP_MY_TEST_EXE QX_REGISTER_HPP_EXPORT_DLL #define QX_REGISTER_CPP_MY_TEST_EXE QX_REGISTER_CPP_EXPORT_DLL #endif * ---------------------------------------------------------------------------------------------------- * 4- precomplied.h file * ---------------------------------------------------------------------------------------------------- #ifndef _QX_PRECOMPILED_HEADER_H_ #define _QX_PRECOMPILED_HEADER_H_ #include <QxOrm.h> #include "export.h" #endif * ----------------------------------------------------------------------------------------------- * 5- main.cpp file : basic functionalities of QxOrm library with drug class * ----------------------------------------------------------------------------------------------- #include "QxORM/precompiled.h" #include "QxORM/drug.h" #include <QxMemLeak.h> int main(int argc, char * argv[]) { QApplication app(argc, argv); // Qt application // Create 3 new drugs // It is possible to use 'boost' and 'Qt' smart pointer : 'boost::shared_ptr', 'QSharedPointer', etc... typedef boost::shared_ptr<drug> drug_ptr; drug_ptr d1; d1.reset(new drug()); d1->name = "name1"; d1->description = "desc1"; drug_ptr d2; d2.reset(new drug()); d2->name = "name2"; d2->description = "desc2"; drug_ptr d3; d3.reset(new drug()); d3->name = "name3"; d3->description = "desc3"; // Insert drugs into container // It is possible to use a lot of containers from 'std', 'boost', 'Qt' and 'qx::QxCollection<Key, Value>' typedef std::vector<drug_ptr> type_lst_drug; type_lst_drug lst_drug; lst_drug.push_back(d1); lst_drug.push_back(d2); lst_drug.push_back(d3); // Init parameters to communicate with a database qx::QxSqlDatabase::getSingleton()->setDriverName("QSQLITE"); qx::QxSqlDatabase::getSingleton()->setDatabaseName("./test_qxorm.db"); qx::QxSqlDatabase::getSingleton()->setHostName("localhost"); qx::QxSqlDatabase::getSingleton()->setUserName("root"); qx::QxSqlDatabase::getSingleton()->setPassword(""); // Create table 'drug' into database to store drugs QSqlError daoError = qx::dao::create_table<drug>(); // Insert drugs from container to database // 'id' property of 'd1', 'd2' and 'd3' are auto-updated daoError = qx::dao::insert(lst_drug); // Modify and update the second drug into database d2->name = "name2 modified"; d2->description = "desc2 modified"; daoError = qx::dao::update(d2); // Delete the first drug from database daoError = qx::dao::delete_by_id(d1); // Count drugs into database long lDrugCount = qx::dao::count<drug>(); // Fetch drug with id '3' into a new variable drug_ptr d_tmp; d_tmp.reset(new drug()); d_tmp->id = 3; daoError = qx::dao::fetch_by_id(d_tmp); // Export drugs from container to a file under xml format (serialization) qx::serialization::xml::to_file(lst_drug, "./export_drugs.xml"); // Import drugs from xml file into a new container type_lst_drug lst_drug_tmp; qx::serialization::xml::from_file(lst_drug_tmp, "./export_drugs.xml"); // Clone a drug drug_ptr d_clone = qx::clone(* d1); // Create a new drug by class name (factory) boost::any d_any = qx::create("drug"); // Insert drugs container into 'qx::cache' qx::cache::set("drugs", lst_drug); // Remove all elements from 'qx::cache' qx::cache::clear(); // Create a dummy memory leak drug * pDummy = new drug(); return 0; } * ----------------------------------------------------------------------------------------------- * 6- QxORMQuickSample.pro file : 项目配置文件 * ----------------------------------------------------------------------------------------------- include(./QxOrm.pri) ############################### # QXORM Library Configuration # ############################### isEmpty(QXORM_INCLUDE_PATH) { QXORM_INCLUDE_PATH = $$quote(D:/QxORM/QxOrm/include) } isEmpty(QXORM_LIB_PATH) { QXORM_LIB_PATH = $$quote(D:/QxORM/QxOrm/lib) } QT += core gui TARGET = QxORMQuickSample TEMPLATE = app INCLUDEPATH += $${QXORM_INCLUDE_PATH} PRECOMPILED_HEADER = ./QxORM/precompiled.h LIBS += -L$${QXORM_LIB_PATH} CONFIG(debug, debug|release) { LIBS += -lQxOrmd } else { LIBS += -lQxOrm } # CONFIG(debug, debug|release) SOURCES += main.cpp\ mainwindow.cpp \ QxORM/drug.cpp HEADERS += mainwindow.h \ QxORM/precompiled.h \ QxORM/export.h \ QxORM/drug.h
最后把前面编译QxOrm时候改好的QxOrm.pri文件拷贝到QxORMQuickSample项目目录下。
编译运行后所得结果跟RxORM文档中的Quick Samples所得的结果相同