geboshi_V1/LeatherProject/Service/Repository.cs
2024-03-07 14:03:22 +08:00

44 lines
1.5 KiB
C#

using SqlSugar;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Service
{
public class Repository<T> : SimpleClient<T> where T : class, new()
{
public Repository(ISqlSugarClient context = null) : base(context)//注意这里要有默认值等于null
{
if (context == null)
{
base.Context = new SqlSugarClient(new ConnectionConfig()
{
DbType = SqlSugar.DbType.MySql,
//InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true,
ConnectionString = InitDB.ConnectionString
});
base.Context.Aop.OnLogExecuting = (s, p) =>
{
//Console.WriteLine(s);
string param = "";
p.ToList().ForEach(pp => param += $"{pp.ParameterName}={pp.Value};");
//File.AppendAllText(@"f:\sql.txt", s + " | " + param + "\r\n");
//InitDB.OutputDebugString(s+ " | "+ param);
};
}
}
/// <summary>
/// 扩展方法,自带方法不能满足的时候可以添加新方法
/// </summary>
/// <returns></returns>
public List<T> CommQuery(string json)
{
T t = Context.Utilities.DeserializeObject<T>(json);
var list = base.Context.Queryable<T>().WhereClass(t).ToList();
return list;
}
}
}