#我采用的是接口的形式#
#IDE工具VS2022#
#框架.net6#
#Nuget包-SqlSugarCode#
#废话不多说,直接上代码#
1.接口
using SqlSugar;
using System.Linq.Expressions;
namespace YourNameSpace
{
public interface ISqlSugarCode<T> where T : class,new()
{
public SqlSugarClient Db();
public List<T> QueryableAll();
public int QueryableCountAll();
public List<T> QueryableCondition(Expression<Func<T,bool>> Expable);
public List<T> QueryableCondition(string whereString, object parameters = null);
public List<T> QueryableCondition(IFuncModel funcModel);
public List<T> QueryableCondition(List<IConditionalModel> conditionalModels);
public List<T> QueryableCondition(List<IConditionalModel> conditionalModels, bool isWrap);
public List<T> QueryableCondition(string fieldName, string conditionalType, object fieldValue);
public List<T> QueryableTake(int take);
public List<T> QueryableAllWithLock();
public List<T> QueryableOR(List<Expression<Func<T, bool>>> expList);
public T QueryableFirst(Expression<Func<T, bool>> expression);
public T QueryableInSingle(object pkValue);
public T QueryableSingle(Expression<Func<T, bool>> expression);
public int QueryableSum(Expression<Func<T,int>> expression);
public bool IsAny(Expression<Func<T,bool>> expression);
public List<T> QueryableWhereClassByPrimaryKey(T t);
public List<T> QueryableWhereClassByPrimaryKey(List<T> t);
public T QueryableFirstOrDefault(Expression<Func<T, object>> expression,Expression<Func<T,bool>> expFirst);
public List<T> QueryableToPageList(int pageNumber,int pageSize);
public Task<List<T>> QueryableToPageListAsync(int pageNumber, int pageSize,RefAsync<int> refAsync);
public List<T> QueryableToPageList<TResult>(Expression<Func<T,TResult,bool>> tRexp,Expression<Func<T,TResult,T>> 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<T> entity);
public int Deleteble(T entity);
public int Deleteble(List<T> entity);
public int Deleteble(List<T> entity,Expression<Func<T,object>> expression);
public int DeletebleIn(int primaryKey);
public int DeletebleIn(Expression<Func<T,object>> expression,int primaryKey);
public int DeletebleIn(int[] primaryKey);
public int DeletebleIn(Expression<Func<T, object>> expression, int[] primaryKey);
public int Deleteble(Expression<Func<T, bool>> expression);
public int InsertableOrupdata(T entity);
public int InsertableOrupdata(List<T> entity);
}
}
2.实现
using SqlSugar;
using System.Linq.Expressions;
namespace YourNameSpace
{
public class SqlSugarCodeSyntacticSugar<T> : ISqlSugarCode<T> 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<SqlSugarClient> DataBaseSql = dbs =>
{
dbs.Aop.OnLogExecuted =(Sql, pars) =>
{
};
};*/
return new SqlSugarClient(Dbconfig);
}
public List<T> QueryableAll()
{
return Db().Queryable<T>().ToList();
}
public List<T> QueryableAllWithLock()
{
return Db().Queryable<T>().With(SqlWith.NoLock).ToList();
}
public int QueryableCountAll()
{
return Db().Queryable<T>().Count();
}
public List<T> QueryableCondition(Expression<Func<T,bool>> Exp)
{
return Db().Queryable<T>().Where(Exp).ToList();
}
public List<T> QueryableCondition(string whereString, object parameters = null)
{
return Db().Queryable<T>().Where(whereString,parameters).ToList();
}
public List<T> QueryableCondition(IFuncModel funcModel)
{
return Db().Queryable<T>().Where(funcModel).ToList();
}
public List<T> QueryableCondition(List<IConditionalModel> conditionalModels)
{
return Db().Queryable<T>().Where(conditionalModels).ToList();
}
public List<T> QueryableCondition(List<IConditionalModel> conditionalModels, bool isWrap)
{
return Db().Queryable<T>().Where(conditionalModels,isWrap).ToList();
}
public List<T> QueryableCondition(string fieldName, string conditionalType, object fieldValue)
{
return Db().Queryable<T>().Where(fieldName,conditionalType,fieldValue).ToList();
}
public List<T> QueryableTake(int take)
{
return Db().Queryable<T>().Take(take).ToList();
}
public T QueryableFirst(Expression<Func<T, bool>> expression)
{
return Db().Queryable<T>().First(expression);
}
public T QueryableInSingle(object pkValue)
{
return Db().Queryable<T>().InSingle(pkValue);
}
public int QueryableSum(Expression<Func<T, int>> expression)
{
return Db().Queryable<T>().Sum(expression);
}
public bool IsAny(Expression<Func<T, bool>> expression)
{
return Db().Queryable<T>().Any(expression);
}
public List<T> QueryableOR(List<Expression<Func<T, bool>>> expList)
{
var exp = Expressionable.Create<T>();
foreach (var item in expList)
{
exp.Or(item);
}
return Db().Queryable<T>().Where(exp.ToExpression()).ToList();
}
public T QueryableSingle(Expression<Func<T, bool>> expression)
{
return Db().Queryable<T>().Single(expression);
}
public List<T> QueryableWhereClassByPrimaryKey(T t)
{
return Db().Queryable<T>().WhereClassByPrimaryKey(t).ToList();
}
public List<T> QueryableWhereClassByPrimaryKey(List<T> t)
{
return Db().Queryable<T>().WhereClassByPrimaryKey(t).ToList();
}
public T QueryableFirstOrDefault(Expression<Func<T, object>> expression, Expression<Func<T, bool>> expFirst)
{
return Db().Queryable<T>().OrderBy(expression,OrderByType.Desc).First(expFirst);
}
public List<T> QueryableToPageList(int pageNumber, int pageSize)
{
return Db().Queryable<T>().ToPageList(pageNumber, pageSize);
}
public Task<List<T>> QueryableToPageListAsync(int pageNumber, int pageSize, RefAsync<int> refAsync)
{
return Db().Queryable<T>().ToPageListAsync(pageNumber,pageSize,refAsync);
}
public List<T> QueryableToPageList<TResult>(Expression<Func<T, TResult,bool>> tRexp, Expression<Func<T, TResult, T>> Tcond, int pageIndex, int pageSize)
{
return Db().Queryable<T>().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<T> entity)
{
return Db().Updateable(entity).ExecuteCommand();
}
public int Deleteble(T entity)
{
return Db().Deleteable(entity).ExecuteCommand();
}
public int Deleteble(List<T> entity)
{
return Db().Deleteable(entity).ExecuteCommand();
}
public int Deleteble(List<T> entity, Expression<Func<T, object>> expression)
{
return Db().Deleteable<T>().WhereColumns(entity, expression).ExecuteCommand();
}
public int DeletebleIn(int primaryKey)
{
return Db().Deleteable<T>().In(primaryKey).ExecuteCommand();
}
public int DeletebleIn(Expression<Func<T, object>> expression, int primaryKey)
{
return Db().Deleteable<T>().In(expression, primaryKey).ExecuteCommand();
}
public int DeletebleIn(int[] primaryKey)
{
return Db().Deleteable<T>().In(primaryKey).ExecuteCommand();
}
public int DeletebleIn(Expression<Func<T, object>> expression, int[] primaryKey)
{
return Db().Deleteable<T>().In(expression,primaryKey).ExecuteCommand();
}
public int Deleteble(Expression<Func<T, bool>> expression)
{
return Db().Deleteable<T>().Where(expression).ExecuteCommand();
}
public int InsertableOrupdata(T entity)
{
return Db().Storageable(entity).ExecuteCommand();
}
public int InsertableOrupdata(List<T> entity)
{
return Db().Storageable(entity).ExecuteCommand();
}
}
}
3.引用
namespace Program
{
public class Program
{
public class YourClassNames
{
private readonly ISqlSugarCode<Studnet> _ISqlSugarCode;
public YourClassNames(ISqlSugarCode<Studnet> ISqlSugarCode)
{
_ISqlSugarCode= ISqlSugarCode;
}
public List<Studnet> GetStu()
{
return _ISqlSugarCode.QueryableAll();
}
}
}
public class Studnet
{
public int Id { get; set; }
public string? Name { get; set; }
}
}