添加项目文件。
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Configuration;
|
||||
|
||||
namespace JY.DAL.Repository
|
||||
{
|
||||
public static class DataContext
|
||||
{
|
||||
private static readonly Lazy<SqlSugarScope> _instance = new Lazy<SqlSugarScope>(() =>
|
||||
{
|
||||
string connectionString = ConfigurationManager.ConnectionStrings["CurDB"].ConnectionString;
|
||||
return new SqlSugarScope(new ConnectionConfig
|
||||
{
|
||||
ConnectionString = connectionString,
|
||||
DbType = DbType.SqlServer,
|
||||
IsAutoCloseConnection = true,
|
||||
InitKeyType = InitKeyType.Attribute
|
||||
}, db =>
|
||||
{
|
||||
db.Aop.OnLogExecuting = (sql, pars) =>
|
||||
{
|
||||
#if DEBUG
|
||||
Console.WriteLine($"SqlSugar SQL: {sql}");
|
||||
#endif
|
||||
};
|
||||
});
|
||||
});
|
||||
|
||||
public static SqlSugarScope Instance => _instance.Value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace JY.DAL.Repository
|
||||
{
|
||||
public interface IRepository<T> where T : class, new()
|
||||
{
|
||||
ISqlSugarClient Db { get; }
|
||||
|
||||
T GetById(object id);
|
||||
|
||||
T GetSingle(Expression<Func<T, bool>> predicate);
|
||||
|
||||
List<T> GetList();
|
||||
|
||||
List<T> GetList(Expression<Func<T, bool>> predicate);
|
||||
|
||||
List<T> GetListPaged(int pageIndex, int pageSize, out int totalCount, Expression<Func<T, bool>> predicate = null, string orderBy = null);
|
||||
|
||||
int Insert(T entity);
|
||||
|
||||
int Insert(List<T> entities);
|
||||
|
||||
int Update(T entity);
|
||||
|
||||
int Update(T entity, Expression<Func<T, bool>> whereExpression);
|
||||
|
||||
int Update(Expression<Func<T, T>> columns, Expression<Func<T, bool>> whereExpression);
|
||||
|
||||
int Delete(object id);
|
||||
|
||||
int Delete(Expression<Func<T, bool>> predicate);
|
||||
|
||||
int Delete(List<T> entities);
|
||||
|
||||
bool Any(Expression<Func<T, bool>> predicate);
|
||||
|
||||
int Count(Expression<Func<T, bool>> predicate = null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace JY.DAL.Repository
|
||||
{
|
||||
public class Repository<T> : IRepository<T> where T : class, new()
|
||||
{
|
||||
public ISqlSugarClient Db => DataContext.Instance;
|
||||
|
||||
public T GetById(object id)
|
||||
{
|
||||
return Db.Queryable<T>().InSingle(id);
|
||||
}
|
||||
|
||||
public T GetSingle(Expression<Func<T, bool>> predicate)
|
||||
{
|
||||
return Db.Queryable<T>().Where(predicate).Single();
|
||||
}
|
||||
|
||||
public List<T> GetList()
|
||||
{
|
||||
return Db.Queryable<T>().ToList();
|
||||
}
|
||||
|
||||
public List<T> GetList(Expression<Func<T, bool>> predicate)
|
||||
{
|
||||
return Db.Queryable<T>().Where(predicate).ToList();
|
||||
}
|
||||
|
||||
public List<T> GetListPaged(int pageIndex, int pageSize, out int totalCount, Expression<Func<T, bool>> predicate = null, string orderBy = null)
|
||||
{
|
||||
totalCount = 0;
|
||||
var query = Db.Queryable<T>();
|
||||
if (predicate != null)
|
||||
{
|
||||
query = query.Where(predicate);
|
||||
}
|
||||
if (!string.IsNullOrEmpty(orderBy))
|
||||
{
|
||||
query = query.OrderBy(orderBy);
|
||||
}
|
||||
return query.ToPageList(pageIndex, pageSize, ref totalCount);
|
||||
}
|
||||
|
||||
public int Insert(T entity)
|
||||
{
|
||||
return Db.Insertable(entity).ExecuteReturnIdentity();
|
||||
}
|
||||
|
||||
public int Insert(List<T> entities)
|
||||
{
|
||||
return Db.Insertable(entities).ExecuteCommand();
|
||||
}
|
||||
|
||||
public int Update(T entity)
|
||||
{
|
||||
return Db.Updateable(entity).ExecuteCommand();
|
||||
}
|
||||
|
||||
public int Update(T entity, Expression<Func<T, bool>> whereExpression)
|
||||
{
|
||||
return Db.Updateable(entity).Where(whereExpression).ExecuteCommand();
|
||||
}
|
||||
|
||||
public int Update(Expression<Func<T, T>> columns, Expression<Func<T, bool>> whereExpression)
|
||||
{
|
||||
return Db.Updateable<T>().SetColumns(columns).Where(whereExpression).ExecuteCommand();
|
||||
}
|
||||
|
||||
public int Delete(object id)
|
||||
{
|
||||
return Db.Deleteable<T>().In(id).ExecuteCommand();
|
||||
}
|
||||
|
||||
public int Delete(Expression<Func<T, bool>> predicate)
|
||||
{
|
||||
return Db.Deleteable<T>().Where(predicate).ExecuteCommand();
|
||||
}
|
||||
|
||||
public int Delete(List<T> entities)
|
||||
{
|
||||
return Db.Deleteable(entities).ExecuteCommand();
|
||||
}
|
||||
|
||||
public bool Any(Expression<Func<T, bool>> predicate)
|
||||
{
|
||||
return Db.Queryable<T>().Where(predicate).Any();
|
||||
}
|
||||
|
||||
public int Count(Expression<Func<T, bool>> predicate = null)
|
||||
{
|
||||
var query = Db.Queryable<T>();
|
||||
if (predicate != null)
|
||||
{
|
||||
query = query.Where(predicate);
|
||||
}
|
||||
return query.Count();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user