0


c++高性能web框架drogon入门教程四,orm使用,csp使用

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里面,避免重名

标签: 数据库 java orm

本文转载自: https://blog.csdn.net/zh7314/article/details/115653021
版权归原作者 zh7314 所有, 如有侵权,请联系我们删除。

“c++高性能web框架drogon入门教程四,orm使用,csp使用”的评论:

还没有评论