SqlSugar中封装的一些泛型方法

#我采用的是接口的形式#

#IDE工具VS2022#

#框架.net6#

#Nuget包-SqlSugarCode#

#废话不多说,直接上代码#

1.接口

using SqlSugar;
using System.Linq.Expressions;

namespace YourNameSpace
{
    public interface ISqlSugarCode where T : class,new()
    {
        public SqlSugarClient Db();

        public List QueryableAll();

        public int QueryableCountAll();

        public List QueryableCondition(Expression> Expable);

        public List QueryableCondition(string whereString, object parameters = null);

        public List QueryableCondition(IFuncModel funcModel);

        public List QueryableCondition(List conditionalModels);

        public List QueryableCondition(List conditionalModels, bool isWrap);

        public List QueryableCondition(string fieldName, string conditionalType, object fieldValue);

        public List QueryableTake(int take);

        public List QueryableAllWithLock();

        public List QueryableOR(List>> expList);

        public T QueryableFirst(Expression> expression);

        public T QueryableInSingle(object pkValue);

        public T QueryableSingle(Expression> expression);

        public int QueryableSum(Expression> expression);

        public bool IsAny(Expression> expression);

        public List QueryableWhereClassByPrimaryKey(T t);

        public List QueryableWhereClassByPrimaryKey(List t);

        public T QueryableFirstOrDefault(Expression> expression,Expression> expFirst);

        public List QueryableToPageList(int pageNumber,int pageSize);

        public Task> QueryableToPageListAsync(int pageNumber, int pageSize,RefAsync refAsync);

        public List QueryableToPageList(Expression> tRexp,Expression> Tcond,int pageIndex,int pageSize);

        public int AddEntity(T entity);

        public int AddEntityReturnKey(T entity);

        public long ReturnExecuteReturnSnowflakeId(T entity);

        public DateTime ReturnDateTime();

        public int Updatable(T entity);

        public int Updatable(List entity);

        public int Deleteble(T entity);

        public int Deleteble(List entity);

        public int Deleteble(List entity,Expression> expression);

        public int DeletebleIn(int primaryKey);

        public int DeletebleIn(Expression> expression,int primaryKey);

        public int DeletebleIn(int[] primaryKey);

        public int DeletebleIn(Expression> expression, int[] primaryKey);

        public int Deleteble(Expression> expression);

        public int InsertableOrupdata(T entity);

        public int InsertableOrupdata(List entity);
    }
}

2.实现

using SqlSugar;
using System.Linq.Expressions;

namespace YourNameSpace
{
    public class SqlSugarCodeSyntacticSugar : ISqlSugarCode where T : class, new()
    {
        private readonly string ConnectDataBaseStrings = @"YourDbConnStrings";
        private readonly DbType DataBaseType = DbType.SqlServer;

        public SqlSugarClient Db()
        {
            var Dbconfig = new ConnectionConfig()
            {
                ConnectionString = ConnectDataBaseStrings,
                DbType = DataBaseType,
                IsAutoCloseConnection = true,
            };
            /* Action DataBaseSql = dbs =>
             {
                 dbs.Aop.OnLogExecuted =(Sql, pars) =>
                 {

                 };
             };*/
            return new SqlSugarClient(Dbconfig);
        }

        public List QueryableAll()
        {
            return Db().Queryable().ToList();
        }

        public List QueryableAllWithLock()
        {
            return Db().Queryable().With(SqlWith.NoLock).ToList();
        }

        public int QueryableCountAll()
        {
            return Db().Queryable().Count();
        }

        public List QueryableCondition(Expression> Exp)
        {
            return Db().Queryable().Where(Exp).ToList();
        }

        public List QueryableCondition(string whereString, object parameters = null)
        {
            return Db().Queryable().Where(whereString,parameters).ToList();
        }

        public List QueryableCondition(IFuncModel funcModel)
        {
            return Db().Queryable().Where(funcModel).ToList();
        }

        public List QueryableCondition(List conditionalModels)
        {
            return Db().Queryable().Where(conditionalModels).ToList();
        }

        public List QueryableCondition(List conditionalModels, bool isWrap)
        {
            return Db().Queryable().Where(conditionalModels,isWrap).ToList();
        }

        public List QueryableCondition(string fieldName, string conditionalType, object fieldValue)
        {
            return Db().Queryable().Where(fieldName,conditionalType,fieldValue).ToList();
        }

        public List QueryableTake(int take)
        {
            return Db().Queryable().Take(take).ToList();
        }

        public T QueryableFirst(Expression> expression)
        {
            return Db().Queryable().First(expression);
        }

        public T QueryableInSingle(object pkValue)
        {
            return Db().Queryable().InSingle(pkValue);
        }

        public int QueryableSum(Expression> expression)
        {
            return Db().Queryable().Sum(expression);
        }

        public bool IsAny(Expression> expression)
        {
            return Db().Queryable().Any(expression);
        }

        public List QueryableOR(List>> expList)
        {
            var exp = Expressionable.Create();
            foreach (var item in expList)
            {
                exp.Or(item);
            }
            return Db().Queryable().Where(exp.ToExpression()).ToList();
        }

        public T QueryableSingle(Expression> expression)
        {
            return Db().Queryable().Single(expression);
        }

        public List QueryableWhereClassByPrimaryKey(T t)
        {
            return Db().Queryable().WhereClassByPrimaryKey(t).ToList();
        }

        public List QueryableWhereClassByPrimaryKey(List t)
        {
            return Db().Queryable().WhereClassByPrimaryKey(t).ToList();
        }

        public T QueryableFirstOrDefault(Expression> expression, Expression> expFirst)
        {
            return Db().Queryable().OrderBy(expression,OrderByType.Desc).First(expFirst);
        }

        public List QueryableToPageList(int pageNumber, int pageSize)
        {
            return Db().Queryable().ToPageList(pageNumber, pageSize);
        }

        public Task> QueryableToPageListAsync(int pageNumber, int pageSize, RefAsync refAsync)
        {
            return Db().Queryable().ToPageListAsync(pageNumber,pageSize,refAsync);
        }

        public List QueryableToPageList(Expression> tRexp, Expression> Tcond, int pageIndex, int pageSize)
        {
            return Db().Queryable().LeftJoin(tRexp).Select(Tcond).ToPageList(pageIndex,pageSize);
        }

        public int AddEntity(T entity)
        {
            return Db().Insertable(entity).ExecuteCommand();
        }

        public int AddEntityReturnKey(T entity)
        {
            return Db().Insertable(entity).ExecuteReturnIdentity();
        }

        public long ReturnExecuteReturnSnowflakeId(T entity)
        {
            return Db().Insertable(entity).ExecuteReturnSnowflakeId();
        }

        public DateTime ReturnDateTime()
        {
            return Db().GetDate();
        }

        public int Updatable(T entity)
        {
            return Db().Updateable(entity).ExecuteCommand();
        }

        public int Updatable(List entity)
        {
            return Db().Updateable(entity).ExecuteCommand();
        }

        public int Deleteble(T entity)
        {
            return Db().Deleteable(entity).ExecuteCommand();
        }

        public int Deleteble(List entity)
        {
            return Db().Deleteable(entity).ExecuteCommand();
        }

        public int Deleteble(List entity, Expression> expression)
        {
            return Db().Deleteable().WhereColumns(entity, expression).ExecuteCommand();
        }

        public int DeletebleIn(int primaryKey)
        {
            return Db().Deleteable().In(primaryKey).ExecuteCommand();
        }

        public int DeletebleIn(Expression> expression, int primaryKey)
        {
            return Db().Deleteable().In(expression, primaryKey).ExecuteCommand();
        }

        public int DeletebleIn(int[] primaryKey)
        {
            return Db().Deleteable().In(primaryKey).ExecuteCommand();
        }

        public int DeletebleIn(Expression> expression, int[] primaryKey)
        {
           return Db().Deleteable().In(expression,primaryKey).ExecuteCommand();
        }

        public int Deleteble(Expression> expression)
        {
            return Db().Deleteable().Where(expression).ExecuteCommand();   
        }

        public int InsertableOrupdata(T entity)
        {
            return Db().Storageable(entity).ExecuteCommand();
        }

        public int InsertableOrupdata(List entity)
        {
            return Db().Storageable(entity).ExecuteCommand();
        }
    }
}

 3.引用

namespace Program
{
    public class Program
    {
        public class YourClassNames
        {
            private readonly ISqlSugarCode _ISqlSugarCode;
            public YourClassNames(ISqlSugarCode ISqlSugarCode)
            {
                _ISqlSugarCode= ISqlSugarCode;
            }
        
            public List GetStu()
            {
                return _ISqlSugarCode.QueryableAll();
            }
        }
    }

    public class Studnet
    {
        public int Id { get; set; }
        public string? Name { get; set; }
    }
}

你可能感兴趣的:(c#,sql)