Qt笔记-多线程检索数据库(单例多重锁,QtConcurrent::blockingMapped)

这里主要是个事例,并不涉及业务逻辑处理。在此记录下。

 

程序运行截图如下:

这里有下面这个类BigDataTest对数据进行的类。

MySQLInstance采用单例双重锁模式对数据库进行操作。

 

源码如下:

BigDataTest.h

#ifndef CONCURRENT_H
#define CONCURRENT_H

#include <QObject>
#include <QList>


class BigDataTest : public QObject
{
    Q_OBJECT
public:
    BigDataTest(QObject *parent = 0);
    void run();

protected:
    bool connectMySQL();
    friend QString sqlResult(const QString &cmd);

private:
    QList<QString> m_str;
};

#endif // CONCURRENT_H

MySQLInstance.h

#ifndef MYSQLINSTANCE_H
#define MYSQLINSTANCE_H

#include <QSqlDatabase>

class MySQLInstance
{
public:
    static MySQLInstance *getInstance();
    QString execSql(const QString &cmd);

protected:
    MySQLInstance();
    void connectMySQL();

private:
    static MySQLInstance *m_instance;
    QSqlDatabase m_db;
};

#endif // MYSQLINSTANCE_H

BigDataTest.cpp

#include "BigDataTest.h"
#include "MySQLInstance.h"
#include <QDebug>
#include <QtConcurrent>
#include <QSqlQuery>

BigDataTest::BigDataTest(QObject *parent) : QObject(parent)
{

    m_str << "SELECT 1 + 1" << "SELECT 100 * 100 * 100 / 10" << "SELECT 100 + 99" << "SELECT 1 + 1"
          << "SELECT 1 + 1 + 500" << "SELECT 1 + 1 + 1000" << "SELECT 1999 - 199" << "SELECT 1999 + 10"
          << "SELECT 1 + 1 + 8888" << "SELECT 1 + 1 + 788";
}

QString sqlResult(const QString &cmd)
{
    qDebug() << "处理SQL语句进程 : " << QThread::currentThread();
    MySQLInstance *instance = MySQLInstance::getInstance();
    return instance->execSql(cmd);
}


void BigDataTest::run()
{
    QList<QString> retList = QtConcurrent::blockingMapped(m_str, sqlResult);
    qDebug() << "返回结果";
    qDebug() << retList;
}

main.cpp

#include <QCoreApplication>
#include "BigDataTest.h"

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

    BigDataTest bigDataTest;
    bigDataTest.run();

    return a.exec();
}

MySQLInstance.cpp

#include "MySQLInstance.h"
#include <QSqlQuery>
#include <QSqlError>
#include <QDebug>
#include <QMutex>
#include <QVariant>

MySQLInstance *MySQLInstance::m_instance = nullptr;
QMutex mt;

MySQLInstance *MySQLInstance::getInstance()
{
    if(m_instance == nullptr){

        mt.lock();
        if(m_instance == nullptr){

            m_instance = new MySQLInstance();
            mt.unlock();
            return m_instance;
        }
        mt.unlock();
    }

    return m_instance;
}

QString MySQLInstance::execSql(const QString &cmd)
{
    if(m_instance == nullptr){

        Q_ASSERT(!"m_instance == nullptr");
    }


    //简单点,返回值只有一个
    mt.lock();
    QSqlQuery query(m_db);
    if(!query.exec(cmd)){

        mt.unlock();
        qDebug() << "error:" << query.lastError().text();
        return "error";
    }
    mt.unlock();

    QString ret = "none";
    while(query.next()){

        ret = query.value(0).toString();
    }


    return ret;
}

MySQLInstance::MySQLInstance()
{
    connectMySQL();
}

void MySQLInstance::connectMySQL()
{
    m_db = QSqlDatabase::addDatabase("QMYSQL");
    m_db.setHostName("127.0.0.1");
    m_db.setPort(3306);
    m_db.setDatabaseName("mysql");
    m_db.setUserName("root");
    m_db.setPassword("root");
    m_db.open();
}

程序打包下载地址:

https://github.com/fengfanchen/Qt/tree/master/DealWithBigData

已标记关键词 清除标记
相关推荐
<p> <b><span style="font-size:14px;"></span><span style="font-size:14px;background-color:#FFE500;">【Java面试宝典】</span></b><br /> <span style="font-size:14px;">1、68讲视频课,500道大厂Java常见面试题+100个Java面试技巧与答题公式+10万字核心知识解析+授课老师1对1面试指导+无限次回放</span><br /> <span style="font-size:14px;">2、这门课程基于胡书敏老师8年Java面试经验,调研近百家互联网公司及面试官的问题打造而成,从筛选简历和面试官角度,给出能帮助候选人能面试成功的面试技巧。</span><br /> <span style="font-size:14px;">3、通过学习这门课程,你能系统掌握Java核心、数据库、Java框架、分布式组件、Java简历准备、面试实战技巧等面试必考知识点。</span><br /> <span style="font-size:14px;">4、知识点+项目经验案,每一个都能做为面试的作品展现。</span><br /> <span style="font-size:14px;">5、本课程已经在线下的培训课程中经过实际检验,老师每次培训结束后,都能帮助同学们运用面试技巧,成功找到更好的工作。</span><br /> <br /> <span style="font-size:14px;background-color:#FFE500;"><b>【超人气讲师】</b></span><br /> <span style="font-size:14px;">胡书敏 | 10年大厂工作经验,8年Java面试官经验,5年线下Java职业培训经验,5年架构师经验</span><br /> <br /> <span style="font-size:14px;background-color:#FFE500;"><b>【报名须知】</b></span><br /> <span style="font-size:14px;">上课模式是什么?</span><br /> <span style="font-size:14px;">课程采取录播模式,课程永久有效,可无限次观看</span><br /> <span style="font-size:14px;">课件、课程案代码完全开放给你,你可以根据所学知识,自行修改、优化</span><br /> <br /> <br /> <span style="font-size:14px;background-color:#FFE500;"><strong>如何开始学习?</strong></span><br /> <span style="font-size:14px;">PC端:报名成功后可以直接进入课程学习</span><br /> <span style="font-size:14px;">移动端:<span style="font-family:Helvetica;font-size:14px;background-color:#FFFFFF;">CSDN 学院APP(注意不是CSDN APP哦)</span></span> </p>
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页