|
@@ -1,22 +1,24 @@
|
|
|
-using SqlSugar;
|
|
|
+using HiTeachCE.Models;
|
|
|
+using SqlSugar;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
+using System.Linq.Expressions;
|
|
|
using System.Threading.Tasks;
|
|
|
using TEAMModelOS.SDK.Context.Configuration;
|
|
|
|
|
|
namespace HiTeachCE.Context
|
|
|
{
|
|
|
- public class DBContext<T> where T : class, new()
|
|
|
+ public class DBContext<Entity> where Entity : class, new()
|
|
|
{
|
|
|
public SqlSugarClient Db;
|
|
|
/// <summary>
|
|
|
/// 修改后的代码
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
- public static DBContext<T> OpDB()
|
|
|
+ public static DBContext<Entity> OpDB()
|
|
|
{
|
|
|
- DBContext<T> dbcontext_t = new DBContext<T>();
|
|
|
+ DBContext<Entity> dbcontext_t = new DBContext<Entity>();
|
|
|
dbcontext_t.Db = new SqlSugarClient(new ConnectionConfig()
|
|
|
{
|
|
|
ConnectionString = BaseConfigModel.Configuration["DbConnection:MySqlConnectionString"],
|
|
@@ -26,5 +28,191 @@ namespace HiTeachCE.Context
|
|
|
});
|
|
|
return dbcontext_t;
|
|
|
}
|
|
|
+ protected DBContext()
|
|
|
+ {
|
|
|
+ Db = new SqlSugarClient(new ConnectionConfig()
|
|
|
+ {
|
|
|
+ ConnectionString = BaseConfigModel.Configuration["DbConnection:MySqlConnectionString"],
|
|
|
+ DbType = SqlSugar.DbType.MySql,
|
|
|
+ IsAutoCloseConnection = true,
|
|
|
+ InitKeyType = InitKeyType.Attribute
|
|
|
+ });
|
|
|
+ //调式代码 用来打印SQL
|
|
|
+ Db.Aop.OnLogExecuting = (sql, pars) =>
|
|
|
+ {
|
|
|
+ Console.WriteLine(sql + "\r\n" +
|
|
|
+ Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
|
|
|
+ };
|
|
|
+ }
|
|
|
+ public void Dispose()
|
|
|
+ {
|
|
|
+ if (Db != null)
|
|
|
+ {
|
|
|
+ Db.Dispose();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ public SimpleClient<Entity> CurrentDb { get { return new SimpleClient<Entity>(Db); } }
|
|
|
+ /// <summary>
|
|
|
+ /// 获取所有
|
|
|
+ /// </summary>
|
|
|
+ /// <returns></returns>
|
|
|
+ public virtual List<Entity> GetList()
|
|
|
+ {
|
|
|
+ return CurrentDb.GetList();
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 根据表达式查询
|
|
|
+ /// </summary>
|
|
|
+ /// <returns></returns>
|
|
|
+ public virtual List<Entity> GetList(Expression<Func<Entity, bool>> whereExpression)
|
|
|
+ {
|
|
|
+ return CurrentDb.GetList(whereExpression);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 根据表达式查询分页
|
|
|
+ /// </summary>
|
|
|
+ /// <returns></returns>
|
|
|
+ public virtual List<Entity> GetPageList(Expression<Func<Entity, bool>> whereExpression, PageModel pageModel)
|
|
|
+ {
|
|
|
+ return CurrentDb.GetPageList(whereExpression, pageModel);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 根据表达式查询分页并排序
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="whereExpression">it</param>
|
|
|
+ /// <param name="pageModel"></param>
|
|
|
+ /// <param name="orderByExpression">it=>it.id或者it=>new{it.id,it.name}</param>
|
|
|
+ /// <param name="orderByType">OrderByType.Desc</param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public virtual List<Entity> GetPageList(Expression<Func<Entity, bool>> whereExpression, PageModel pageModel, Expression<Func<Entity, object>> orderByExpression = null, OrderByType orderByType = OrderByType.Asc)
|
|
|
+ {
|
|
|
+ return CurrentDb.GetPageList(whereExpression, pageModel, orderByExpression, orderByType);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 根据主键查询
|
|
|
+ /// </summary>
|
|
|
+ /// <returns></returns>
|
|
|
+ public virtual List<Entity> GetById(dynamic id)
|
|
|
+ {
|
|
|
+ return CurrentDb.GetById(id);
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// 根据主键查询
|
|
|
+ /// </summary>
|
|
|
+ /// <returns></returns>
|
|
|
+ public virtual List<Entity> GetByIds(dynamic[] ids)
|
|
|
+ {
|
|
|
+ return Db.Queryable<Entity>().In("id", ids).ToList();
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 根据主键删除
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="id"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public virtual bool Delete(dynamic id)
|
|
|
+ {
|
|
|
+ if (string.IsNullOrEmpty(id.ObjToString))
|
|
|
+ {
|
|
|
+ Console.WriteLine(string.Format("要删除的主键id不能为空值!"));
|
|
|
+ }
|
|
|
+ return CurrentDb.Delete(id);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 根据实体删除
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="id"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public virtual bool Delete(Entity data)
|
|
|
+ {
|
|
|
+ if (data == null)
|
|
|
+ {
|
|
|
+ Console.WriteLine(string.Format("要删除的实体对象不能为空值!"));
|
|
|
+ }
|
|
|
+ return CurrentDb.Delete(data);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 根据主键删除
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="id"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public virtual bool Delete(dynamic[] ids)
|
|
|
+ {
|
|
|
+ if (ids.Count() <= 0)
|
|
|
+ {
|
|
|
+ Console.WriteLine(string.Format("要删除的主键ids不能为空值!"));
|
|
|
+ }
|
|
|
+ return CurrentDb.AsDeleteable().In(ids).ExecuteCommand() > 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 根据表达式删除
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="id"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public virtual bool Delete(Expression<Func<Entity, bool>> whereExpression)
|
|
|
+ {
|
|
|
+ return CurrentDb.Delete(whereExpression);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 根据实体更新,实体需要有主键
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="id"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public virtual bool Update(Entity obj)
|
|
|
+ {
|
|
|
+ if (obj == null)
|
|
|
+ {
|
|
|
+ Console.WriteLine(string.Format("要更新的实体不能为空,必须带上主键!"));
|
|
|
+ }
|
|
|
+ return CurrentDb.Update(obj);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ ///批量更新
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="id"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public virtual bool Update(List<Entity> objs)
|
|
|
+ {
|
|
|
+ if (objs.Count <= 0)
|
|
|
+ {
|
|
|
+ Console.WriteLine(string.Format("要批量更新的实体不能为空,必须带上主键!"));
|
|
|
+ }
|
|
|
+ return CurrentDb.UpdateRange(objs);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 插入
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="id"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public virtual bool Insert(Entity obj)
|
|
|
+ {
|
|
|
+ return CurrentDb.Insert(obj);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 批量
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="id"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public virtual bool Insert(List<Entity> objs)
|
|
|
+ {
|
|
|
+ return CurrentDb.InsertRange(objs);
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
}
|