using SqlSugar; using System; using System.Threading.Tasks; using System.Collections.Generic; using SHJX.Service.Common.ReadXML; namespace SHJX.Service.Dao { public class DataManagerImp { /// /// 数据库连接 /// protected SqlSugarClient Db; /// /// 配置文件读取 /// private ReadConfigUtil _config; /// /// 初始化数据 /// protected void InitData(ReadConfigUtil config) { _config = config; GetSqlSugarClient(); } /// /// 创建SqlSugarClient /// /// SqlSugarClient连接对象 private void GetSqlSugarClient() { //创建数据库对象 Db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = _config.ConnStr, //连接符字串 DbType = DbType.MySql, //数据库类型 IsAutoCloseConnection = true, InitKeyType = InitKeyType.Attribute //从特性读取主键自增信息 }); /* //添加Sql打印事件(可删掉) Db.Aop.OnLogExecuting = (sql, pars) => logger.WarnFormat("{0} {1}", sql,Db.Utilities.SerializeObject(pars.ToDictionary(item => item.ParameterName,item => item.Value)));*/ } /// /// 查询全部数据 /// /// /// public IEnumerable Query() where T : class => Db.Queryable().ToList(); /// /// 根据条件表达式删除 /// /// /// /// public int Delete(Func func) where T : class, new() => Db.Deleteable().Where(func.Invoke()).ExecuteCommand(); /// /// 根据实体删除 /// /// /// /// public int Delete(T t) where T : class, new() => Db.Deleteable().Where(t).ExecuteCommand(); /// /// 批量删除 /// /// /// /// public int Delete(List lists) where T : class, new() => Db.Deleteable().Where(lists).ExecuteCommand(); /// /// 根据主键删除 /// /// /// /// public int Delete(int id) where T : class, new() => Db.Deleteable().In(id).ExecuteCommand(); /// /// 根据条件表达式删除 /// /// /// /// public Task DeleteAsync(Func func) where T : class, new() => Db.Deleteable().Where(func.Invoke()).ExecuteCommandAsync(); /// /// 根据实体删除 /// /// /// /// public Task DeleteAsync(T t) where T : class, new() => Db.Deleteable().Where(t).ExecuteCommandAsync(); /// /// 删除集合 /// /// /// /// public Task DeleteAsync(List lists) where T : class, new() => Db.Deleteable().Where(lists).ExecuteCommandAsync(); /// /// 根据主键删除 /// /// /// /// public Task DeleteAsync(int id) where T : class, new() => Db.Deleteable().In(id).ExecuteCommandAsync(); /// /// 根据实体插入 /// /// /// /// public int Insert(T t) where T : class, new() => Db.Insertable(t).ExecuteCommand(); /// /// 批量插入 /// /// /// /// public int Insert(List t) where T : class, new() => Db.Insertable(t).ExecuteCommand(); /// /// 异步插入 /// /// /// /// public Task InsertAsync(T t) where T : class, new() => Db.Insertable(t).ExecuteCommandAsync(); /// /// 单条数据更新 /// /// /// /// public int Update(T t) where T : class, new() => Db.Updateable(t).ExecuteCommand(); //IUpdateExpressions Update() where T : class; /// /// 多条数据同时更新 /// /// /// /// public int Update(List lists) where T : class, new() => Db.Updateable(lists).ExecuteCommand(); /// /// /// /// /// /// true:只更新指定字段 false:不更新指定字段 /// /// public int Update(T t, bool isUpdateParams, params string[] columns) where T : class, new() => isUpdateParams ? Db.Updateable(t).UpdateColumns(columns).ExecuteCommand() : Db.Updateable(t).IgnoreColumns(columns).ExecuteCommand(); /// /// 单条数据更新 /// /// /// /// public Task UpdateAsycn(T t) where T : class, new() => Db.Updateable(t).ExecuteCommandAsync(); /// /// 多条数据同时更新 /// /// /// /// public Task UpdateAsycn(List lists) where T : class, new() => Db.Updateable(lists).ExecuteCommandAsync(); /// /// /// /// /// /// true:只更新指定字段 false:不更新指定字段 /// /// public Task UpdateAsycn(T t, bool isUpdateParams, params string[] columns) where T : class, new() => isUpdateParams ? Db.Updateable(t).UpdateColumns(columns).ExecuteCommandAsync() : Db.Updateable(t).IgnoreColumns(columns).ExecuteCommandAsync(); } }