|
|
|
@ -1,5 +1,4 @@
|
|
|
|
|
|
|
|
|
|
using Dapper;
|
|
|
|
|
using Dapper;
|
|
|
|
|
using Dapper.Contrib.Extensions;
|
|
|
|
|
using Microsoft.Data.SqlClient;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
@ -9,16 +8,19 @@ using System.Threading.Tasks;
|
|
|
|
|
using Common;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Common.Interface;
|
|
|
|
|
using System.Linq.Expressions;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace Common
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Dapper帮助类
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class DapperDBBase : DbContext, IDapperDBBase
|
|
|
|
|
public class DapperDBBase : DbContext,IDapperDBBase
|
|
|
|
|
{
|
|
|
|
|
private string sqlConnection;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public DapperDBBase()
|
|
|
|
|
{
|
|
|
|
@ -33,9 +35,9 @@ namespace Common
|
|
|
|
|
if (string.IsNullOrEmpty(sqlConnection))
|
|
|
|
|
|
|
|
|
|
sqlConnection = sysOptionConfig.YsWebconfig.DapperDbString;
|
|
|
|
|
IDbConnection conn = new SqlConnection(sqlConnection);
|
|
|
|
|
conn.Open();
|
|
|
|
|
return conn;
|
|
|
|
|
IDbConnection conn = new SqlConnection(sqlConnection);
|
|
|
|
|
conn.Open();
|
|
|
|
|
return conn;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -410,7 +412,6 @@ namespace Common
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 根据查询SQL直接返回datatable
|
|
|
|
|
/// Author GHB 2021-6-15
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="entitys">实体模型</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
@ -426,7 +427,7 @@ namespace Common
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 执行SQL语句返回结果gjg
|
|
|
|
|
/// 执行SQL语句返回结果
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="insertSql"></param>
|
|
|
|
|
/// <param name="parms"></param>
|
|
|
|
@ -438,6 +439,11 @@ namespace Common
|
|
|
|
|
return await Conn.ExecuteScalarAsync<T>(Sql, parms);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 执行中包含事务sql
|
|
|
|
|
/// </summary>
|
|
|
|
@ -501,45 +507,18 @@ namespace Common
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// 数据库连接名
|
|
|
|
|
private static string _connection = string.Empty;
|
|
|
|
|
|
|
|
|
|
/// 获取连接名
|
|
|
|
|
private static string Connection
|
|
|
|
|
|
|
|
|
|
public long Insert<T>(T model) where T : class, new()
|
|
|
|
|
{
|
|
|
|
|
get { return _connection; }
|
|
|
|
|
set { _connection = value; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// 定义一个标识确保线程同步
|
|
|
|
|
private static readonly object locker = new object();
|
|
|
|
|
|
|
|
|
|
private static DapperDBBase uniqueInstance;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取实例,这里为单例模式,保证只存在一个实例
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static DapperDBBase GetInstance()
|
|
|
|
|
{
|
|
|
|
|
// 双重锁定实现单例模式,在外层加个判空条件主要是为了减少加锁、释放锁的不必要的损耗
|
|
|
|
|
if (uniqueInstance == null)
|
|
|
|
|
using (IDbConnection Conn = GetConn())
|
|
|
|
|
{
|
|
|
|
|
lock (locker)
|
|
|
|
|
{
|
|
|
|
|
if (uniqueInstance == null)
|
|
|
|
|
{
|
|
|
|
|
uniqueInstance = new DapperDBBase();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return Conn.Insert<T>(model);
|
|
|
|
|
}
|
|
|
|
|
return uniqueInstance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|