2020年11月26日13:57:48
c++高性能web框架drogon入門教程一 linux環境搭建和demo運行
c++高性能web框架drogon入門教程二 windows10下安裝drogon,配合vscoede搭建開發環境
c++高性能web框架drogon入門教程三 控制器和數據庫客戶端使用
c++高性能web框架drogon入門教程四 orm使用,csp使用
c++高性能web框架drogon入門教程五 實例小項目,web和api實例代碼
首先這個 drogon的orm和一般框架的orm是不一樣的,不要用你使用其他框架的orm來使用
orm是同步接口,dbclient基本都是異步接口,性能更好,建議
1,配置model.json
上篇博客已經寫好了,翻一下
2,生成對應模型
drogon_ctl create model models
直接yes覆蓋
3,一個實例代碼
#include "TestCtrl.h" #include "../models/Admin.h" #include <exception> using namespace web; using namespace drogon_model::v2; using namespace drogon::orm; void TestCtrl::name(const HttpRequestPtr &req, std::function<void (const HttpResponsePtr &)> &&callback) const { try{ auto clientPtr = drogon::app().getDbClient(); Mapper<Admin> mp(clientPtr); // std::vector<Admin> uu = mp.orderBy(Admin::Cols::_id).limit(25).offset(0).findAll(); auto iii = mp.count(); std::cout << iii << " rows 111111111111111!" << std::endl; auto uu = mp.orderBy(Admin::Cols::_id).limit(5).offset(5).findAll(); std::cout << uu.size() << " rows 2222222222222222!" << std::endl; }catch (const DrogonDbException &e){ std::cout << "error:" << e.base().what() << std::endl; } Json::Value ret; ret["result"]="33"; ret["user_name"]="Jack"; ret["gender"]=1; auto resp=HttpResponse::newHttpJsonResponse(ret); callback(resp); }
很奇怪有一個問題,admin表背后有30條記錄,但是
mp.orderBy(Admin::Cols::_id).limit(5).offset(5).findAll();就崩潰 mp.orderBy(Admin::Cols::_id).limit(1).offset(1).findAll();就OK
看是windows下的bug吧,而且orm生成的語句和一般我們認為的生產的SQL語句不一樣
比如我們一般認為.limit(5).offset(5)
注意:應該生成的是limit 5,5 但是實際生成的limit 5 offset 5
目前我使用的版本有個小bug就是如果你使用orm,你的datetime類型的數據,如果值是"0000-00-00 00:00:00"的通過Mapper包裝的Admin模型就會導致程序崩潰,異常也無法捕獲
所以建議在數據里不要存在datetime類型的數據為"0000-00-00 00:00:00"的數據,不然就會崩潰,此問題已經反饋給框架作者
中文亂碼問題,數據庫編碼設置在配置json里,兩個json都要改
"client_encoding": "utf8",
全局sql打印,打印一些debug信息
"log_level": "TRACE"
error LNK2005: 已經在 TestCtrl.obj 中定義 正在創建庫 D:/cpp/web/build/Debug/web.lib 和對象 D:/cpp/web/build/Debug/web.exp [build] LINK : warning LNK4098: 默認庫“MSVCRT”與其他庫的使用沖突;請使用 /NODEFAULTLIB:library [D:\cpp\web\build\web.vcxproj] warning C4819: 該文件包含不能在當前代碼頁(936)中表示的字符
4,csp使用,注意這個和我們一般所謂的模板引擎不是同一個概念,也類似,這里通過c++語法吧html,拼接出來
drogon_ctl create view Index.csp create view:Index.csp create HttpView Class file by Index.csp can't open file Index.csp
你需要先去view手動建立一個空的 Index.csp,注意保存為utf-8
然后執行項目根目錄
drogon_ctl create view Index.csp
<%inc #include "../models/News.h" #include <exception> #include <trantor/utils/Date.h> using namespace drogon_model::v2; using namespace drogon::orm; %> <!DOCTYPE html> <html lang="zh-CN"> <%c++ auto t1=@@.get<std::vector<News>>("news1"); auto t2=@@.get<Result>("news2"); for (auto row : t2) { std::cout << row["id"].as<std::string>() << std::endl; std::cout << row["title"].as<std::string>() << std::endl; std::cout << row["create_time"].as<std::string>() << std::endl; } %> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- 上述3個meta標簽*必須*放在最前面,任何其他內容都*必須*跟隨其后! --> <title>[[ title ]]</title> <!-- Bootstrap --> <link href="./static/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <!-- Fixed navbar --> <nav class="navbar navbar-inverse navbar-fixed-top"> <div class="container"> <div class="navbar-header"> <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="#">Bootstrap 主題</a> </div> <div id="navbar" class="navbar-collapse collapse"> <ul class="nav navbar-nav"> <li class="active"><a href="/">首頁</a></li> <li><a href="#about">關於我們</a></li> <li><a href="#contact">聯系我們</a></li> <li class="下拉菜單"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a> <ul class="dropdown-menu"> <li><a href="#">Action</a></li> <li><a href="#">Another action</a></li> <li><a href="#">Something else here</a></li> <li role="separator" class="divider"></li> <li class="dropdown-header">Nav header</li> <li><a href="#">Separated link</a></li> <li><a href="#">One more separated link</a></li> </ul> </li> </ul> </div><!--/.nav-collapse --> </div> </nav> <div class="container theme-showcase" role="main"> <!-- Main jumbotron for a primary marketing message or call to action --> <div class="jumbotron"> <h1>主題示例</h1> <p>This is a template showcasing the optional theme stylesheet included in Bootstrap. Use it as a starting point to create something more unique by building on or modifying it.</p> </div> <div class="page-header"> <h1>新聞列表</h1> </div> <div class="row"> <div class="col-md-6"> <table class="table"> <thead> <tr> <th>ID</th> <th>標題</th> <th>作者ID</th> <th>時間</th> </tr> </thead> <tbody> <%c++ for(auto iter:t1){%> <tr> <td>{%*iter.getId()%}</td> <td><a href="/news_detail?news_id={%*iter.getId()%}" target="__blank">{%*iter.getTitle()%}</a></td> <td>{%*iter.getAdminId()%}</td> <%c++ trantor::Date now = *iter.getCreateTime();%> <td>{%now.toDbStringLocal()%}</td> </tr> <%c++}%> </tbody> </table> </div> <div class="row"> <div class="col-md-6"> <table class="table table-condensed"> <thead> <tr> <th>ID</th> <th>標題</th> <th>作者ID</th> <th>時間</th> </tr> </thead> <tbody> <%c++ for(auto row:t2){%> <tr> <td>{%row["id"].as<std::string>()%}</td> <td>{%row["title"].as<std::string>()%}</td> <td>{%row["admin_id"].as<std::string>()%}</td> <td>{%row["create_time"].as<std::string>()%}</td> </tr> <%c++}%> </tbody> </table> </div> </div> </div> <!-- /container --> <!-- jQuery (Bootstrap 的所有 JavaScript 插件都依賴 jQuery,所以必須放在前邊) --> <!-- 加載 Bootstrap 的所有 JavaScript 插件。你也可以根據需要只加載單個插件。 --> <script src="./static/js/jquery.min.js"></script> <script src="./static/js/bootstrap.min.js"></script> </body> </html>
注意view重名問題,因為編譯的之后模板都是放到build里面,避免重名