From 2e98682e7e1fb0e0fcf0d9c2d2afaab6501f3c96 Mon Sep 17 00:00:00 2001 From: tone <3341154833@qq.com> Date: Fri, 6 Sep 2024 11:57:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96MySQL=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=E6=B1=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Server/src/Plugs/MySQLConnection.ts | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/Server/src/Plugs/MySQLConnection.ts b/Server/src/Plugs/MySQLConnection.ts index ad04808..baa5631 100644 --- a/Server/src/Plugs/MySQLConnection.ts +++ b/Server/src/Plugs/MySQLConnection.ts @@ -1,5 +1,8 @@ -// MYSQL数据库连接池 -// 版本:v0.1 +/** + * @file MySQLConnection.ts + * @version 1.0.0 + * @description MySQL数据库连接池 + */ import mysql from "mysql2/promise"; import Logger from "./Logger"; import config from "../config"; @@ -20,8 +23,7 @@ class MySQLConnectPool { }, 10); } - // 内部函数,无需手动调用 - createConnectPool() { + private createConnectPool() { return mysql.createPool({ host: config.mysql.host, database: config.mysql.database, @@ -33,8 +35,7 @@ class MySQLConnectPool { }) } - // 内部函数,无需手动调用 - async testConnection() { + private async testConnection() { try { let res = await this.execute("SELECT 1 + 1 As result"); if (res[0].result == 2) @@ -48,8 +49,14 @@ class MySQLConnectPool { } - // 执行SQL语句 - async execute(sql: string, values?: any[], database?: string) { + /** + * 执行SQL查询 + * @param sql SQL语句 + * @param values 可选的查询参数列表 + * @param database 可选的数据库 + * @returns Promise 查询结果 + */ + public async execute(sql: string, values?: any[], database?: string): Promise { let connection: any; try { connection = await this.pool.getConnection(); @@ -73,5 +80,5 @@ class MySQLConnectPool { } } -let MySQLConnection = new MySQLConnectPool(); +const MySQLConnection = new MySQLConnectPool(); export default MySQLConnection; \ No newline at end of file