首先简单封装了个DbContext
代码语言:javascript复制 1 public class DbContext
2 {
3 #region 属性字段
4 private static string _connectionString;
5
6 /// <summary>
7 /// 连接字符串 by beck.huang 2018-05-08 09:56:05
8 /// </summary>
9 public static string ConnectionString
10 {
11 get { return _connectionString; }
12 set { _connectionString = value; }
13 }
14
15 private static DbType _dbType;
16
17 /// <summary>
18 /// 数据库类型 by beck.huang 2018-05-08 09:58:03
19 /// </summary>
20 public static DbType DbType
21 {
22 get { return _dbType; }
23 set { _dbType = value; }
24 }
25
26
27 private SqlSugarClient _db;
28
29 /// <summary>
30 /// 数据连接对象 by beck.huang 2018-05-08 10:00:14
31 /// </summary>
32 public SqlSugarClient Db
33 {
34 get { return _db; }
35 private set { _db = value; }
36 }
37
38 /// <summary>
39 /// 数据库上下文实例(自动关闭连接) by beck.huang 2018-05-08 09:47:30
40 /// </summary>
41 public static DbContext Context
42 {
43 get
44 {
45 return new DbContext();
46 }
47
48 }
49 #endregion
50
51 #region 构造函数
52
53 /// <summary>
54 /// 功能描述:构造函数
55 /// 作 者:beck.huang
56 /// 创建日期:2018-05-08 09:47:24
57 /// 任务编号:中餐
58 /// </summary>
59 private DbContext()
60 {
61 if (string.IsNullOrEmpty(_connectionString))
62 throw new ArgumentNullException("数据库连接字符串为空");
63 _db = new SqlSugarClient(new ConnectionConfig()
64 {
65 ConnectionString = _connectionString,
66 DbType = _dbType,
67 IsAutoCloseConnection = true,
68 IsShardSameThread = true,
69 ConfigureExternalServices = new ConfigureExternalServices()
70 {
71 DataInfoCacheService = new HttpRuntimeCache()
72 },
73 MoreSettings = new ConnMoreSettings()
74 {
75 //IsWithNoLockQuery = true,
76 IsAutoRemoveDataCache = true
77 }
78 });
79 }
80
81 /// <summary>
82 /// 功能描述:构造函数
83 /// 作 者:beck.huang
84 /// 创建日期:2018-05-28 17:23:19
85 /// 任务编号:中餐
86 /// </summary>
87 /// <param name="blnIsAutoCloseConnection">是否自动关闭连接</param>
88 private DbContext(bool blnIsAutoCloseConnection)
89 {
90 if (string.IsNullOrEmpty(_connectionString))
91 throw new ArgumentNullException("数据库连接字符串为空");
92 _db = new SqlSugarClient(new ConnectionConfig()
93 {
94 ConnectionString = _connectionString,
95 DbType = _dbType,
96 IsAutoCloseConnection = blnIsAutoCloseConnection,
97 IsShardSameThread = true,
98 ConfigureExternalServices = new ConfigureExternalServices()
99 {
100 DataInfoCacheService = new HttpRuntimeCache()
101 },
102 MoreSettings = new ConnMoreSettings()
103 {
104 //IsWithNoLockQuery = true,
105 IsAutoRemoveDataCache = true
106 }
107 });
108 }
109 #endregion
110
111 #region 实例方法
112 /// <summary>
113 /// 功能描述:获取数据库处理对象
114 /// 作 者:beck.huang
115 /// 创建日期:2018-05-08 09:46:06
116 /// 任务编号:中餐
117 /// </summary>
118 /// <returns>返回值</returns>
119 public SimpleClient<T> GetEntityDB<T>() where T : class,new()
120 {
121 return new SimpleClient<T>(_db);
122 }
123 /// <summary>
124 /// 功能描述:获取数据库处理对象
125 /// 作 者:beck.huang
126 /// 创建日期:2018-05-09 09:17:43
127 /// 任务编号:中餐
128 /// </summary>
129 /// <param name="db">db</param>
130 /// <returns>返回值</returns>
131 public SimpleClient<T> GetEntityDB<T>(SqlSugarClient db) where T : class,new()
132 {
133 return new SimpleClient<T>(db);
134 }
135
136 #region 根据数据库表生产实体类
137 /// <summary>
138 /// 功能描述:根据数据库表生产实体类
139 /// 作 者:beck.huang
140 /// 创建日期:2018-05-08 10:14:37
141 /// 任务编号:中餐
142 /// </summary>
143 /// <param name="strPath">实体类存放路径</param>
144 public void CreateClassFileByDBTalbe(string strPath)
145 {
146 CreateClassFileByDBTalbe(strPath, "Km.PosZC");
147 }
148 /// <summary>
149 /// 功能描述:根据数据库表生产实体类
150 /// 作 者:beck.huang
151 /// 创建日期:2018-05-08 10:10:59
152 /// 任务编号:中餐
153 /// </summary>
154 /// <param name="strPath">实体类存放路径</param>
155 /// <param name="strNameSpace">命名空间</param>
156 public void CreateClassFileByDBTalbe(string strPath, string strNameSpace)
157 {
158 CreateClassFileByDBTalbe(strPath, strNameSpace, null);
159 }
160
161 /// <summary>
162 /// 功能描述:根据数据库表生产实体类
163 /// 作 者:beck.huang
164 /// 创建日期:2018-05-08 10:18:18
165 /// 任务编号:中餐
166 /// </summary>
167 /// <param name="strPath">实体类存放路径</param>
168 /// <param name="strNameSpace">命名空间</param>
169 /// <param name="lstTableNames">生产指定的表</param>
170 public void CreateClassFileByDBTalbe(
171 string strPath,
172 string strNameSpace,
173 string[] lstTableNames)
174 {
175 CreateClassFileByDBTalbe(strPath, strNameSpace, lstTableNames, string.Empty);
176 }
177
178 /// <summary>
179 /// 功能描述:根据数据库表生产实体类
180 /// 作 者:beck.huang
181 /// 创建日期:2018-05-09 15:38:22
182 /// 任务编号:中餐
183 /// </summary>
184 /// <param name="strPath">实体类存放路径</param>
185 /// <param name="strNameSpace">命名空间</param>
186 /// <param name="lstTableNames">生产指定的表</param>
187 /// <param name="strInterface">实现接口</param>
188 public void CreateClassFileByDBTalbe(
189 string strPath,
190 string strNameSpace,
191 string[] lstTableNames,
192 string strInterface,
193 bool blnSerializable = false)
194 {
195 if (lstTableNames != null && lstTableNames.Length > 0)
196 {
197 _db.DbFirst.Where(lstTableNames).IsCreateDefaultValue().IsCreateAttribute()
198 .SettingClassTemplate(p => p = @"
199 {using}
200
201 namespace {Namespace}
202 {
203 {ClassDescription}{SugarTable}" (blnSerializable ? "[Serializable]" : "") @"
204 public partial class {ClassName}" (string.IsNullOrEmpty(strInterface) ? "" : (" : " strInterface)) @"
205 {
206 public {ClassName}()
207 {
208 {Constructor}
209 }
210 {PropertyName}
211 }
212 }
213 ")
214 .SettingPropertyTemplate(p => p = @"
215 {SugarColumn}
216 public {PropertyType} {PropertyName}
217 {
218 get
219 {
220 return _{PropertyName};
221 }
222 set
223 {
224 if(_{PropertyName}!=value)
225 {
226 base.SetValueCall(" ""{PropertyName}",_{PropertyName}" @");
227 }
228 _{PropertyName}=value;
229 }
230 }")
231 .SettingPropertyDescriptionTemplate(p => p = " private {PropertyType} _{PropertyName};rn" p)
232 .SettingConstructorTemplate(p => p = " this._{PropertyName} ={DefaultValue};")
233 .CreateClassFile(strPath, strNameSpace);
234 }
235 else
236 {
237 _db.DbFirst.IsCreateAttribute().IsCreateDefaultValue()
238 .SettingClassTemplate(p => p = @"
239 {using}
240
241 namespace {Namespace}
242 {
243 {ClassDescription}{SugarTable}" (blnSerializable ? "[Serializable]" : "") @"
244 public partial class {ClassName}" (string.IsNullOrEmpty(strInterface) ? "" : (" : " strInterface)) @"
245 {
246 public {ClassName}()
247 {
248 {Constructor}
249 }
250 {PropertyName}
251 }
252 }
253 ")
254 .SettingPropertyTemplate(p => p = @"
255 {SugarColumn}
256 public {PropertyType} {PropertyName}
257 {
258 get
259 {
260 return _{PropertyName};
261 }
262 set
263 {
264 if(_{PropertyName}!=value)
265 {
266 base.SetValueCall(" ""{PropertyName}",_{PropertyName}" @");
267 }
268 _{PropertyName}=value;
269 }
270 }")
271 .SettingPropertyDescriptionTemplate(p => p = " private {PropertyType} _{PropertyName};rn" p)
272 .SettingConstructorTemplate(p => p = " this._{PropertyName} ={DefaultValue};")
273 .CreateClassFile(strPath, strNameSpace);
274 }
275 }
276 #endregion
277
278 #region 根据实体类生成数据库表
279 /// <summary>
280 /// 功能描述:根据实体类生成数据库表
281 /// 作 者:beck.huang
282 /// 创建日期:2018-05-08 10:31:02
283 /// 任务编号:中餐
284 /// </summary>
285 /// <param name="blnBackupTable">是否备份表</param>
286 /// <param name="lstEntitys">指定的实体</param>
287 public void CreateTableByEntity<T>(bool blnBackupTable, params T[] lstEntitys) where T : class,new()
288 {
289 Type[] lstTypes = null;
290 if (lstEntitys != null)
291 {
292 lstTypes = new Type[lstEntitys.Length];
293 for (int i = 0; i < lstEntitys.Length; i )
294 {
295 T t = lstEntitys[i];
296 lstTypes[i] = typeof(T);
297 }
298 }
299 CreateTableByEntity(blnBackupTable, lstTypes);
300 }
301
302 /// <summary>
303 /// 功能描述:根据实体类生成数据库表
304 /// 作 者:beck.huang
305 /// 创建日期:2018-05-08 10:31:14
306 /// 任务编号:中餐
307 /// </summary>
308 /// <param name="blnBackupTable">是否备份表</param>
309 /// <param name="lstEntitys">指定的实体</param>
310 public void CreateTableByEntity(bool blnBackupTable, params Type[] lstEntitys)
311 {
312 if (blnBackupTable)
313 {
314 _db.CodeFirst.BackupTable().InitTables(lstEntitys); //change entity backupTable
315 }
316 else
317 {
318 _db.CodeFirst.InitTables(lstEntitys);
319 }
320 }
321 #endregion
322
323 #endregion
324
325 #region 静态方法
326
327 /// <summary>
328 /// 功能描述:获得一个DbContext
329 /// 作 者:beck.huang
330 /// 创建日期:2018-05-28 17:24:12
331 /// 任务编号:中餐
332 /// </summary>
333 /// <param name="blnIsAutoCloseConnection">是否自动关闭连接(如果为false,则使用接受时需要手动关闭Db)</param>
334 /// <returns>返回值</returns>
335 public static DbContext GetDbContext(bool blnIsAutoCloseConnection)
336 {
337 return new DbContext(blnIsAutoCloseConnection);
338 }
339
340 /// <summary>
341 /// 功能描述:设置初始化参数
342 /// 作 者:beck.huang
343 /// 创建日期:2018-05-08 10:02:32
344 /// 任务编号:中餐
345 /// </summary>
346 /// <param name="strConnectionString">连接字符串</param>
347 /// <param name="enmDbType">数据库类型</param>
348 public static void Init(string strConnectionString, DbType enmDbType = SqlSugar.DbType.MySql)
349 {
350 _connectionString = strConnectionString;
351 _dbType = enmDbType;
352 }
353
354 /// <summary>
355 /// 功能描述:创建一个链接配置
356 /// 作 者:beck.huang
357 /// 创建日期:2018-05-09 09:03:33
358 /// 任务编号:中餐
359 /// </summary>
360 /// <param name="blnIsAutoCloseConnection">是否自动关闭连接</param>
361 /// <param name="blnIsShardSameThread">是否夸类事务</param>
362 /// <returns>ConnectionConfig</returns>
363 public static ConnectionConfig GetConnectionConfig(bool blnIsAutoCloseConnection = true, bool blnIsShardSameThread = false)
364 {
365 ConnectionConfig config = new ConnectionConfig()
366 {
367 ConnectionString = _connectionString,
368 DbType = _dbType,
369 IsAutoCloseConnection = blnIsAutoCloseConnection,
370 ConfigureExternalServices = new ConfigureExternalServices()
371 {
372 DataInfoCacheService = new HttpRuntimeCache()
373 },
374 IsShardSameThread = blnIsShardSameThread
375 };
376 return config;
377 }
378
379 /// <summary>
380 /// 功能描述:获取一个自定义的DB
381 /// 作 者:beck.huang
382 /// 创建日期:2018-05-08 09:49:36
383 /// 任务编号:中餐
384 /// </summary>
385 /// <param name="config">config</param>
386 /// <returns>返回值</returns>
387 public static SqlSugarClient GetCustomDB(ConnectionConfig config)
388 {
389 return new SqlSugarClient(config);
390 }
391 /// <summary>
392 /// 功能描述:获取一个自定义的数据库处理对象
393 /// 作 者:beck.huang
394 /// 创建日期:2018-05-09 08:56:20
395 /// 任务编号:中餐
396 /// </summary>
397 /// <param name="sugarClient">sugarClient</param>
398 /// <returns>返回值</returns>
399 public static SimpleClient<T> GetCustomEntityDB<T>(SqlSugarClient sugarClient) where T : class,new()
400 {
401 return new SimpleClient<T>(sugarClient);
402 }
403 /// <summary>
404 /// 功能描述:获取一个自定义的数据库处理对象
405 /// 作 者:beck.huang
406 /// 创建日期:2018-05-08 09:50:32
407 /// 任务编号:中餐
408 /// </summary>
409 /// <param name="config">config</param>
410 /// <returns>返回值</returns>
411 public static SimpleClient<T> GetCustomEntityDB<T>(ConnectionConfig config) where T : class,new()
412 {
413 SqlSugarClient sugarClient = GetCustomDB(config);
414 return GetCustomEntityDB<T>(sugarClient);
415 }
416 #endregion
417 }
然后是一个BaseDal,基本上覆盖了大部分的增删改查功能
代码语言:javascript复制 public class BaseDal<T> where T : class ,new()
{
#region 属性字段
private DbContext _context;
public DbContext Context
{
get { return _context; }
set { _context = value; }
}
private SqlSugarClient _db;
/// <summary>
/// 数据处理对象 by beck.huang 2018-05-09 09:31:24
/// </summary>
internal SqlSugarClient Db
{
get { return _db; }
private set { _db = value; }
}
private SimpleClient<T> _entityDB;
/// <summary>
/// 实体数据处理对象 by beck.huang 2018-05-09 09:31:15
/// </summary>
internal SimpleClient<T> EntityDB
{
get { return _entityDB; }
private set { _entityDB = value; }
}
public Action<Exception> OnDalError { get; set; }
public Action<string, SugarParameter[]> OnDalLogExecuting { get; set; }
public Action<string, SugarParameter[]> OnDalLogExecuted { get; set; }
public Func<string, SugarParameter[], KeyValuePair<string, SugarParameter[]>> OnDalExecutingChangeSql { get; set; }
#endregion
#region 构造函数
/// <summary>
/// 功能描述:构造函数
/// 作 者:beck.huang
/// 创建日期:2018-05-09 09:30:54
/// 任务编号:中餐
/// </summary>
/// <param name="blnIsAutoCloseConnection">是否自动关闭连接</param>
public BaseDal(bool blnIsAutoCloseConnection = true)
{
DbContext context = DbContext.GetDbContext(blnIsAutoCloseConnection);
_context = context;
_db = context.Db;
//_db.Aop.OnLogExecuted = new Action<string, SugarParameter[]>(OnLogExecuted);
//_db.Aop.OnLogExecuting = OnDalLogExecuting;
//_db.Aop.OnError = OnDalError;
//_db.Aop.OnExecutingChangeSql = new Func<string, SugarParameter[], KeyValuePair<string, SugarParameter[]>>(OnExecutingChangeSql);
_entityDB = context.GetEntityDB<T>(_db);
}
/// <summary>
/// 功能描述:使用指定的DbContext初始化一个对象
/// 作 者:beck.huang
/// 创建日期:2018-05-28 17:31:57
/// 任务编号:中餐
/// </summary>
/// <param name="context">DbContext</param>
public BaseDal(DbContext context)
{
_context = context;
_db = context.Db;
_entityDB = context.GetEntityDB<T>(_db);
}
#endregion
#region 增
/// <summary>
/// 功能描述:插入数据
/// 作 者:beck.huang
/// 创建日期:2018-05-16 17:57:26
/// 任务编号:中餐
/// </summary>
/// <param name="strSql">strSql</param>
/// <param name="parameters">parameters</param>
/// <returns>是否成功</returns>
public bool Insert(string strSql, SugarParameter[] parameters = null)
{
return _db.Ado.ExecuteCommand(strSql, parameters) > 0;
}
/// <summary>
/// 功能描述:插入数据
/// 作 者:beck.huang
/// 创建日期:2018-05-09 09:43:06
/// 任务编号:中餐
/// </summary>
/// <param name="entitys">实体列表</param>
/// <returns>是否成功</returns>
public bool Insert(params T[] entitys)
{
if (entitys != null && entitys.Length > 0)
{
return _entityDB.InsertRange(entitys);
}
return true;
}
/// <summary>
/// 功能描述:插入数据,返回自增列
/// 作 者:beck.huang
/// 创建日期:2018-05-09 09:44:52
/// 任务编号:中餐
/// </summary>
/// <param name="entity">实体</param>
/// <returns>自增ID</returns>
public int InsertReturnIdentity(T entity)
{
return _entityDB.InsertReturnIdentity(entity);
}
#endregion
#region 删
/// <summary>
/// 功能描述:删除指定实体
/// 作 者:beck.huang
/// 创建日期:2018-05-09 09:47:38
/// 任务编号:中餐
/// </summary>
/// <param name="entity">实体(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件</param>
/// <returns>是否成功</returns>
public bool Delete(T entity)
{
return _entityDB.Delete(entity);
}
/// <summary>
/// 功能描述:删除数据
/// 作 者:beck.huang
/// 创建日期:2018-05-09 10:06:10
/// 任务编号:中餐
/// </summary>
/// <param name="entitys">实体列表(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件</param>
/// <returns>受影响行数</returns>
public int Delete(T[] entitys)
{
if (entitys != null)
return _db.Deleteable<T>(entitys.ToList()).ExecuteCommand();
else
return 0;
}
/// <summary>
/// 功能描述:删除数据
/// 作 者:beck.huang
/// 创建日期:2018-05-09 09:52:35
/// 任务编号:中餐
/// </summary>
/// <param name="whereExpression">条件表达式</param>
/// <returns>是否成功</returns>
public bool Delete(Expression<Func<T, bool>> whereExpression)
{
return _entityDB.Delete(whereExpression);
}
/// <summary>
/// 功能描述:删除数据
/// 作 者:beck.huang
/// 创建日期:2018-05-09 10:02:37
/// 任务编号:中餐
/// </summary>
/// <param name="strWhere">条件</param>
/// <returns>是否成功</returns>
public bool Delete(string strWhere)
{
return _db.Deleteable<T>().Where(strWhere).ExecuteCommand() > 0;
}
/// <summary>
/// 功能描述:删除数据
/// 作 者:beck.huang
/// 创建日期:2018-05-09 10:02:37
/// 任务编号:中餐
/// </summary>
/// <param name="strWhere">条件</param>
/// <param name="lstParameters">参数</param>
/// <returns>是否成功</returns>
public bool Delete(string strWhere, List<SugarParameter> lstParameters)
{
return _db.Deleteable<T>().Where(strWhere, lstParameters).ExecuteCommand() > 0;
}
/// <summary>
/// 功能描述:根据ID删除
/// 作 者:beck.huang
/// 创建日期:2018-05-09 10:08:18
/// 任务编号:中餐
/// </summary>
/// <param name="ids">主键列表(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件</param>
/// <returns>受影响行数</returns>
public int DeleteByID(params object[] ids)
{
return _db.Deleteable<T>().In(ids).ExecuteCommand();
}
#endregion
#region 改
/// <summary>
/// 功能描述:修改数据
/// 作 者:beck.huang
/// 创建日期:2018-05-16 17:57:26
/// 任务编号:中餐
/// </summary>
/// <param name="strSql">strSql</param>
/// <param name="parameters">parameters</param>
/// <returns>是否成功</returns>
public bool Update(string strSql, SugarParameter[] parameters = null)
{
return _db.Ado.ExecuteCommand(strSql, parameters) > 0;
}
/// <summary>
/// 功能描述:修改数据
/// 作 者:beck.huang
/// 创建日期:2018-05-09 10:28:27
/// 任务编号:中餐
/// </summary>
/// <param name="entity">实体(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件</param>
/// <returns>是否成功</returns>
public bool Update(T entity)
{
return _entityDB.Update(entity);
}
/// <summary>
/// 功能描述:修改数据
/// 作 者:beck.huang
/// 创建日期:2018-05-09 10:28:27
/// 任务编号:中餐
/// </summary>
/// <param name="entity">实体</param>
/// <param name="entity">条件</param>
/// <returns>是否成功</returns>
public bool Update(T entity, string strWhere)
{
return _db.Updateable(entity).Where(strWhere).ExecuteCommand() > 0;
}
/// <summary>
/// 功能描述:修改数据
/// 作 者:beck.huang
/// 创建日期:2018-05-09 10:28:27
/// 任务编号:中餐
/// </summary>
/// <param name="entity">实体</param>
/// <param name="entity">条件</param>
/// <param name="lstParameters">参数</param>
/// <returns>是否成功</returns>
public bool Update(
T entity,
string strWhere,
List<SugarParameter> lstParameters)
{
return _db.Updateable(entity).Where(strWhere, lstParameters).ExecuteCommand() > 0;
}
/// <summary>
/// 功能描述:修改数据
/// 作 者:beck.huang
/// 创建日期:2018-05-09 10:31:15
/// 任务编号:中餐
/// </summary>
/// <param name="entitys">实体列表(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件</param>
/// <returns>受影响行数</returns>
public int Update(T[] entitys)
{
return Update(entitys, null, null);
}
/// <summary>
/// 功能描述:修改数据
/// 作 者:beck.huang
/// 创建日期:2018-05-16 15:31:09
/// 任务编号:中餐
/// </summary>
/// <param name="entitys">实体列表(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件</param>
/// <param name="lstColumns">更新的列,如果为空则不做限制(lstColumns与lstIgnoreColumns互斥)</param>
/// <param name="lstIgnoreColumns">不更新的列,如果为空则不做限制(lstColumns与lstIgnoreColumns互斥)</param>
/// <returns>受影响行数</returns>
public int Update(
T[] entitys,
List<string> lstColumns,
List<string> lstIgnoreColumns)
{
return Update(entitys, lstColumns, lstIgnoreColumns, "");
}
/// <summary>
/// 功能描述:修改数据
/// 作 者:beck.huang
/// 创建日期:2018-05-16 15:31:47
/// 任务编号:中餐
/// </summary>
/// <param name="entitys">实体列表(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件</param>
/// <param name="lstColumns">更新的列,如果为空则不做限制(lstColumns与lstIgnoreColumns互斥)</param>
/// <param name="lstIgnoreColumns">不更新的列,如果为空则不做限制(lstColumns与lstIgnoreColumns互斥)</param>
/// <param name="strWhere">条件</param>
/// <returns>受影响行数</returns>
public int Update(
T[] entitys,
List<string> lstColumns,
List<string> lstIgnoreColumns,
string strWhere)
{
return Update(entitys, lstColumns, lstIgnoreColumns, strWhere, null);
}
/// <summary>
/// 功能描述:修改数据
/// 作 者:beck.huang
/// 创建日期:2018-05-16 15:33:06
/// 任务编号:中餐
/// </summary>
/// <param name="entitys">实体列表(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件</param>
/// <param name="lstColumns">更新的列,如果为空则不做限制(lstColumns与lstIgnoreColumns互斥)</param>
/// <param name="lstIgnoreColumns">不更新的列,如果为空则不做限制(lstColumns与lstIgnoreColumns互斥)</param>
/// <param name="strWhere">条件</param>
/// <param name="lstParameters">条件参数</param>
/// <returns>受影响行数</returns>
public int Update(
T[] entitys,
List<string> lstColumns,
List<string> lstIgnoreColumns,
string strWhere,
List<SugarParameter> lstParameters)
{
IUpdateable<T> up = _db.Updateable(entitys);
if (lstIgnoreColumns != null && lstIgnoreColumns.Count > 0)
{
up = up.IgnoreColumns(it => lstIgnoreColumns.Contains(it));
}
if (lstColumns != null && lstColumns.Count > 0)
{
up = up.UpdateColumns(it => lstColumns.Contains(it));
}
if (!string.IsNullOrEmpty(strWhere))
{
up = up.Where(strWhere, lstParameters);
}
return up.ExecuteCommand();
}
/// <summary>
/// 功能描述:修改数据
/// 作 者:beck.huang
/// 创建日期:2018-05-16 15:33:36
/// 任务编号:中餐
/// </summary>
/// <param name="entitys">实体列表(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件</param>
/// <param name="lstColumns">更新的列,如果为空则不做限制(lstColumns与lstIgnoreColumns互斥)</param>
/// <param name="lstIgnoreColumns">不更新的列,如果为空则不做限制(lstColumns与lstIgnoreColumns互斥)</param>
/// <param name="whereExpression">条件</param>
/// <returns>受影响行数</returns>
public int Update(
T[] entitys,
List<string> lstColumns,
List<string> lstIgnoreColumns,
Expression<Func<T, bool>> whereExpression)
{
IUpdateable<T> up = _db.Updateable(entitys);
if (lstIgnoreColumns != null && lstIgnoreColumns.Count > 0)
{
up = up.IgnoreColumns(it => lstIgnoreColumns.Contains(it));
}
if (lstColumns != null && lstColumns.Count > 0)
{
up = up.UpdateColumns(it => lstColumns.Contains(it));
}
if (whereExpression != null)
{
up = up.Where(whereExpression);
}
return up.ExecuteCommand();
}
/// <summary>
/// 功能描述:修改数据
/// 作 者:beck.huang
/// 创建日期:2018-05-09 10:40:18
/// 任务编号:中餐
/// </summary>
/// <param name="columns">修改的列</param>
/// <param name="whereExpression">条件表达式</param>
/// <returns>是否成功</returns>
public bool Update(Expression<Func<T, T>> columns, Expression<Func<T, bool>> whereExpression)
{
return _entityDB.Update(columns, whereExpression);
}
/// <summary>
/// 功能描述:修改数据
/// 作 者:beck.huang
/// 创建日期:2018-05-09 10:43:35
/// 任务编号:中餐
/// </summary>
/// <param name="entity">实体(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件</param>
/// <param name="lstColumns">更新的列,如果为空则不做限制(lstColumns与lstIgnoreColumns互斥)</param>
/// <param name="lstIgnoreColumns">不更新的列,如果为空则不做限制(lstColumns与lstIgnoreColumns互斥)</param>
/// <returns>是否成功</returns>
public bool Update(
T entity,
List<string> lstColumns,
List<string> lstIgnoreColumns)
{
return Update(entity, lstColumns, lstIgnoreColumns, string.Empty);
}
/// <summary>
/// 功能描述:修改指定的列和值
/// 作 者:beck.huang
/// 创建日期:2018-05-16 19:33:46
/// 任务编号:中餐
/// </summary>
/// <param name="strWhere">条件</param>
/// <param name="lstSetValueExpression">列和值列表(如:it => it.Name == (it.Name 1))</param>
/// <returns>是否成功</returns>
public bool Update(string strWhere, params Expression<Func<T, bool>>[] lstSetValueExpression)
{
return Update(strWhere, null, lstSetValueExpression);
}
/// <summary>
/// 功能描述:修改指定的列和值
/// 作 者:beck.huang
/// 创建日期:2018-05-16 19:34:01
/// 任务编号:中餐
/// </summary>
/// <param name="strWhere">条件</param>
/// <param name="lstParameters">参数</param>
/// <param name="lstSetValueExpression">列和值列表(如:it => it.Name == (it.Name 1))</param>
/// <returns>是否成功</returns>
public bool Update(
string strWhere,
List<SugarParameter> lstParameters,
params Expression<Func<T, bool>>[] lstSetValueExpression
)
{
IUpdateable<T> up = _db.Updateable<T>();
if (lstSetValueExpression != null)
{
foreach (var item in lstSetValueExpression)
{
up = up.ReSetValue(item);
}
}
if (!string.IsNullOrEmpty(strWhere))
{
up = up.Where(strWhere, lstParameters);
}
return up.ExecuteCommand() > 0;
}
/// <summary>
/// 功能描述:修改数据
/// 作 者:beck.huang
/// 创建日期:2018-05-09 10:43:35
/// 任务编号:中餐
/// </summary>
/// <param name="entity">实体</param>
/// <param name="lstColumns">更新的列,如果为空则不做限制(lstColumns与lstIgnoreColumns互斥)</param>
/// <param name="lstIgnoreColumns">不更新的列,如果为空则不做限制(lstColumns与lstIgnoreColumns互斥)</param>
/// <param name="strWhere">条件</param>
/// <returns>是否成功</returns>
public bool Update(
T entity,
List<string> lstColumns,
List<string> lstIgnoreColumns,
string strWhere)
{
IUpdateable<T> up = _db.Updateable(entity);
if (lstIgnoreColumns != null && lstIgnoreColumns.Count > 0)
{
up = up.IgnoreColumns(it => lstIgnoreColumns.Contains(it));
}
if (lstColumns != null && lstColumns.Count > 0)
{
up = up.UpdateColumns(it => lstColumns.Contains(it));
}
if (!string.IsNullOrEmpty(strWhere))
{
up = up.Where(strWhere);
}
return up.ExecuteCommand() > 0;
}
/// <summary>
/// 功能描述:修改数据
/// 作 者:beck.huang
/// 创建日期:2018-05-09 10:43:35
/// 任务编号:中餐
/// </summary>
/// <param name="entity">实体</param>
/// <param name="lstColumns">更新的列,如果为空则不做限制(lstColumns与lstIgnoreColumns互斥)</param>
/// <param name="lstIgnoreColumns">不更新的列,如果为空则不做限制(lstColumns与lstIgnoreColumns互斥)</param>
/// <param name="strWhere">条件</param>
/// <param name="lstParameters">参数</param>
/// <returns>是否成功</returns>
public bool Update(
T entity,
List<string> lstColumns,
List<string> lstIgnoreColumns,
string strWhere,
List<SugarParameter> lstParameters)
{
IUpdateable<T> up = _db.Updateable(entity);
if (lstIgnoreColumns != null && lstIgnoreColumns.Count > 0)
{
up = up.IgnoreColumns(it => lstIgnoreColumns.Contains(it));
}
if (lstColumns != null && lstColumns.Count > 0)
{
up = up.UpdateColumns(it => lstColumns.Contains(it));
}
if (!string.IsNullOrEmpty(strWhere))
{
up = up.Where(strWhere, lstParameters);
}
return up.ExecuteCommand() > 0;
}
/// <summary>
/// 功能描述:修改数据
/// 作 者:beck.huang
/// 创建日期:2018-05-15 10:10:53
/// 任务编号:中餐
/// </summary>
/// <param name="实体">entity</param>
/// <param name="lstColumns">更新的列,如果为空则不做限制(lstColumns与lstIgnoreColumns互斥)</param>
/// <param name="lstIgnoreColumns">不更新的列,如果为空则不做限制(lstColumns与lstIgnoreColumns互斥)</param>
/// <param name="whereExpression">条件表达式</param>
/// <returns>是否成功</returns>
public bool Update(
T entity,
List<string> lstColumns,
List<string> lstIgnoreColumns,
Expression<Func<T, bool>> whereExpression)
{
IUpdateable<T> up = _db.Updateable(entity);
if (lstIgnoreColumns != null && lstIgnoreColumns.Count > 0)
{
up = up.IgnoreColumns(it => lstIgnoreColumns.Contains(it));
}
if (lstColumns != null && lstColumns.Count > 0)
{
up = up.UpdateColumns(it => lstColumns.Contains(it));
}
if (whereExpression != null)
{
up = up.Where(whereExpression);
}
return up.ExecuteCommand() > 0;
}
/// <summary>
/// 功能描述:修改数据
/// 作 者:beck.huang
/// 创建日期:2018-05-14 15:40:53
/// 任务编号:中餐
/// </summary>
/// <param name="lstColumnValues">列,值</param>
/// <param name="strWhere">条件</param>
/// <param name="lstParameters">参数</param>
/// <returns>是否成功</returns>
public bool Update(
Dictionary<string, object> lstColumnValues,
string strWhere,
List<SugarParameter> lstParameters)
{
IUpdateable<T> up = _db.Updateable<T>(lstColumnValues);
if (!string.IsNullOrEmpty(strWhere))
{
up = up.Where(strWhere, lstParameters);
}
return up.ExecuteCommand() > 0;
}
/// <summary>
/// 功能描述:修改数据
/// 作 者:beck.huang
/// 创建日期:2018-05-14 15:42:27
/// 任务编号:中餐
/// </summary>
/// <param name="lstColumnValues">列,值</param>
/// <param name="whereExpression">条件</param>
/// <returns>是否成功</returns>
public bool Update(Dictionary<string, object> lstColumnValues, Expression<Func<T, bool>> whereExpression)
{
IUpdateable<T> up = _db.Updateable<T>(lstColumnValues);
if (whereExpression != null)
{
up = up.Where(whereExpression);
}
return up.ExecuteCommand() > 0;
}
#endregion
#region 查
/// <summary>
/// 功能描述:数据条数
/// 作 者:beck.huang
/// 创建日期:2018-05-25 18:07:00
/// 任务编号:中餐
/// </summary>
/// <param name="strWhere">strWhere</param>
/// <returns>返回值</returns>
public int SelectCount(string strWhere)
{
return _db.Queryable<T>()
.WhereIF(!string.IsNullOrEmpty(strWhere), strWhere)
.Count();
}
/// <summary>
/// 功能描述:查询所有数据
/// 作 者:beck.huang
/// 创建日期:2018-05-09 14:02:34
/// 任务编号:中餐
/// </summary>
/// <returns>数据列表</returns>
public List<T> Query()
{
return _entityDB.GetList();
}
/// <summary>
/// 功能描述:查询数据列表
/// 作 者:beck.huang
/// 创建日期:2018-05-10 10:39:11
/// 任务编号:中餐
/// </summary>
/// <param name="strWhere">条件</param>
/// <returns>数据列表</returns>
public List<T> Query(string strWhere)
{
return _db.Queryable<T>().WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToList();
}
/// <summary>
/// 功能描述:查询数据列表
/// 作 者:beck.huang
/// 创建日期:2018-05-10 10:40:32
/// 任务编号:中餐
/// </summary>
/// <param name="whereExpression">whereExpression</param>
/// <returns>数据列表</returns>
public List<T> Query(Expression<Func<T, bool>> whereExpression)
{
return _entityDB.GetList(whereExpression);
}
/// <summary>
/// 功能描述:查询一个列表
/// 作 者:beck.huang
/// 创建日期:2018-05-09 11:13:55
/// 任务编号:中餐
/// </summary>
/// <param name="whereExpression">条件表达式</param>
/// <param name="strOrderByFileds">排序字段,如name asc,age desc</param>
/// <returns>数据列表</returns>
public List<T> Query(Expression<Func<T, bool>> whereExpression, string strOrderByFileds)
{
return _db.Queryable<T>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).ToList();
}
/// <summary>
/// 功能描述:查询一个列表
/// 作 者:beck.huang
/// 创建日期:2018-05-09 11:14:54
/// 任务编号:中餐
/// </summary>
/// <param name="strWhere">条件</param>
/// <param name="strOrderByFileds">排序字段,如name asc,age desc</param>
/// <returns>数据列表</returns>
public List<T> Query(string strWhere, string strOrderByFileds)
{
return _db.Queryable<T>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToList();
}
/// <summary>
/// 功能描述:查询一个列表
/// 作 者:beck.huang
/// 创建日期:2018-05-09 11:14:54
/// 任务编号:中餐
/// </summary>
/// <param name="strWhere">条件</param>
/// <param name="lstParameters">参数</param>
/// <param name="strOrderByFileds">排序字段,如name asc,age desc</param>
/// <returns>数据列表</returns>
public List<T> Query(
string strWhere,
List<SugarParameter> lstParameters,
string strOrderByFileds)
{
return _db.Queryable<T>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere, lstParameters).ToList();
}
/// <summary>
/// 功能描述:查询前N条数据
/// 作 者:beck.huang
/// 创建日期:2018-05-09 11:16:09
/// 任务编号:中餐
/// </summary>
/// <param name="whereExpression">条件表达式</param>
/// <param name="intTop">前N条</param>
/// <param name="strOrderByFileds">排序字段,如name asc,age desc</param>
/// <returns>数据列表</returns>
public List<T> Query(
Expression<Func<T, bool>> whereExpression,
int intTop,
string strOrderByFileds)
{
return _db.Queryable<T>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).Take(intTop).ToList();
}
/// <summary>
/// 功能描述:查询前N条数据
/// 作 者:beck.huang
/// 创建日期:2018-05-09 11:17:14
/// 任务编号:中餐
/// </summary>
/// <param name="strWhere">条件</param>
/// <param name="intTop">前N条</param>
/// <param name="strOrderByFileds">排序字段,如name asc,age desc</param>
/// <returns>数据列表</returns>
public List<T> Query(
string strWhere,
int intTop,
string strOrderByFileds)
{
return _db.Queryable<T>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).Take(intTop).ToList();
}
/// <summary>
/// 功能描述:查询前N条数据
/// 作 者:beck.huang
/// 创建日期:2018-05-09 11:17:14
/// 任务编号:中餐
/// </summary>
/// <param name="strWhere">条件</param>
/// <param name="lstParameters">参数</param>
/// <param name="intTop">前N条</param>
/// <param name="strOrderByFileds">排序字段,如name asc,age desc</param>
/// <returns>数据列表</returns>
public List<T> Query(
string strWhere,
List<SugarParameter> lstParameters,
int intTop,
string strOrderByFileds)
{
return _db.Queryable<T>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere, lstParameters).Take(intTop).ToList();
}
/// <summary>
/// 功能描述:分页查询
/// 作 者:beck.huang
/// 创建日期:2018-05-09 11:27:17
/// 任务编号:中餐
/// </summary>
/// <param name="whereExpression">条件表达式</param>
/// <param name="intPageIndex">页码(下标0)</param>
/// <param name="intPageSize">页大小</param>
/// <param name="intTotalCount">数据总量</param>
/// <param name="strOrderByFileds">排序字段,如name asc,age desc</param>
/// <returns>数据列表</returns>
public List<T> Query(
Expression<Func<T, bool>> whereExpression,
int intPageIndex,
int intPageSize,
ref int intTotalCount,
string strOrderByFileds)
{
return _db.Queryable<T>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).ToPageList(intPageIndex, intPageSize, ref intTotalCount);
}
/// <summary>
/// 功能描述:分页查询
/// 作 者:beck.huang
/// 创建日期:2018-05-09 11:29:07
/// 任务编号:中餐
/// </summary>
/// <param name="strWhere">条件</param>
/// <param name="intPageIndex">页码(下标0)</param>
/// <param name="intPageSize">页大小</param>
/// <param name="intTotalCount">数据总量</param>
/// <param name="strOrderByFileds">排序字段,如name asc,age desc</param>
/// <returns>数据列表</returns>
public List<T> Query(
string strWhere,
int intPageIndex,
int intPageSize,
ref int intTotalCount,
string strOrderByFileds)
{
return _db.Queryable<T>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToPageList(intPageIndex, intPageSize, ref intTotalCount);
}
/// <summary>
/// 功能描述:分页查询
/// 作 者:beck.huang
/// 创建日期:2018-05-09 11:29:07
/// 任务编号:中餐
/// </summary>
/// <param name="strWhere">条件</param>
/// <param name="lstParameters">参数</param>
/// <param name="intPageIndex">页码(下标0)</param>
/// <param name="intPageSize">页大小</param>
/// <param name="intTotalCount">数据总量</param>
/// <param name="strOrderByFileds">排序字段,如name asc,age desc</param>
/// <returns>数据列表</returns>
public List<T> Query(
string strWhere,
List<SugarParameter> lstParameters,
int intPageIndex,
int intPageSize,
ref int intTotalCount,
string strOrderByFileds)
{
return _db.Queryable<T>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere, lstParameters).ToPageList(intPageIndex, intPageSize, ref intTotalCount);
}
/// <summary>
/// 功能描述:根据ID查询一条数据
/// 作 者:beck.huang
/// 创建日期:2018-05-09 11:23:21
/// 任务编号:中餐
/// </summary>
/// <param name="objId">id(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件</param>
/// <returns>数据实体</returns>
public T QueryByID(object objId)
{
return _db.Queryable<T>().InSingle(objId);
}
/// <summary>
/// 功能描述:根据ID查询一条数据
/// 作 者:beck.huang
/// 创建日期:2018-05-14 16:58:09
/// 任务编号:中餐
/// </summary>
/// <param name="objId">id(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件</param>
/// <param name="blnUseCache">是否使用缓存</param>
/// <param name="cacheDurationInSeconds">缓存超时时间(秒)</param>
/// <returns>数据实体</returns>
public T QueryByID(
object objId,
bool blnUseCache = false,
int cacheDurationInSeconds = 2147483647)
{
return _db.Queryable<T>().WithCacheIF(blnUseCache).InSingle(objId);
}
/// <summary>
/// 功能描述:根据ID查询数据
/// 作 者:beck.huang
/// 创建日期:2018-05-09 11:23:34
/// 任务编号:中餐
/// </summary>
/// <param name="lstIds">id列表(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件</param>
/// <returns>数据实体列表</returns>
public List<T> QueryByIDs(object[] lstIds)
{
return _db.Queryable<T>().In(lstIds).ToList();
}
/// <summary>
/// 功能描述:根据ID查询一条数据
/// 作 者:beck.huang
/// 创建日期:2018-05-14 16:58:47
/// 任务编号:中餐
/// </summary>
/// <param name="lstIds">id列表(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件</param>
/// <param name="blnUseCache">是否使用缓存</param>
/// <param name="cacheDurationInSeconds">缓存超时时间(秒)</param>
/// <returns>数据实体列表</returns>
public List<T> QueryByIDs(
object[] lstIds,
bool blnUseCache = false,
int cacheDurationInSeconds = 2147483647)
{
return _db.Queryable<T>().WithCacheIF(blnUseCache).In(lstIds).ToList();
}
#region QueryEntity
/// <summary>
/// 功能描述:查询一个实体
/// 作 者:beck.huang
/// 创建日期:2018-05-10 10:45:37
/// 任务编号:中餐
/// </summary>
/// <param name="whereExpression">条件表达式</param>
/// <param name="blnUseCache">是否使用缓存机制</param>
/// <param name="lstParameters">缓存过期时长</param>
/// <returns>实体</returns>
public T QueryEntity(
Expression<Func<T, bool>> whereExpression,
bool blnUseCache = false,
int cacheDurationInSeconds = 2147483647)
{
return _db.Queryable<T>().WithCacheIF(blnUseCache, cacheDurationInSeconds).WhereIF(whereExpression != null, whereExpression).First();
}
/// <summary>
/// 功能描述:查询一个实体
/// 作 者:beck.huang
/// 创建日期:2018-05-10 10:45:44
/// 任务编号:中餐
/// </summary>
/// <param name="strWhere">条件</param>
/// <param name="lstParameters">参数</param>
/// <param name="blnUseCache">是否使用缓存机制</param>
/// <param name="lstParameters">缓存过期时长</param>
/// <returns>实体</returns>
public T QueryEntity(
string strWhere,
List<SugarParameter> lstParameters = null,
bool blnUseCache = false,
int cacheDurationInSeconds = 2147483647)
{
return _db.Queryable<T>().WithCacheIF(blnUseCache, cacheDurationInSeconds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere, lstParameters).First();
}
#endregion
#region QueryList
/// <summary>
/// 功能描述:查询一个列表
/// 作 者:beck.huang
/// 创建日期:2018-05-10 10:47:14
/// 任务编号:中餐
/// </summary>
/// <param name="blnUseCache">是否使用缓存机制</param>
/// <param name="lstParameters">缓存过期时长</param>
/// <returns>返回值</returns>
public List<T> QueryList(bool blnUseCache = false, int cacheDurationInSeconds = 2147483647)
{
return _db.Queryable<T>().WithCacheIF(blnUseCache, cacheDurationInSeconds).ToList();
}
/// <summary>
/// 功能描述:查询一个列表
/// 作 者:beck.huang
/// 创建日期:2018-05-10 10:50:32
/// 任务编号:中餐
/// </summary>
/// <param name="whereExpression">条件表达式</param>
/// <param name="intTop">前N条数据</param>
/// <param name="strOrderByFileds">排序字段,如name asc,age desc</param>
/// <param name="blnUseCache">是否使用缓存机制</param>
/// <param name="lstParameters">缓存过期时长</param>
/// <returns>数据列表</returns>
public List<T> QueryList(
Expression<Func<T, bool>> whereExpression,
int? intTop = null,
string strOrderByFileds = null,
bool blnUseCache = false,
int cacheDurationInSeconds = 2147483647)
{
ISugarQueryable<T> q = _db.Queryable<T>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression);
if (intTop.HasValue)
{
q = q.Take(intTop.Value);
}
return q.WithCacheIF(blnUseCache, cacheDurationInSeconds).ToList();
}
/// <summary>
/// 功能描述:查询一个列表
/// 作 者:beck.huang
/// 创建日期:2018-05-10 10:52:17
/// 任务编号:中餐
/// </summary>
/// <param name="strWhere">条件</param>
/// <param name="lstParameters">参数</param>
/// <param name="intTop">前N条数据</param>
/// <param name="strOrderByFileds">排序字段,如name asc,age desc</param>
/// <param name="blnUseCache">是否使用缓存机制</param>
/// <param name="lstParameters">缓存过期时长</param>
/// <returns>数据列表</returns>
public List<T> QueryList(
string strWhere,
List<SugarParameter> lstParameters = null,
int? intTop = null,
string strOrderByFileds = null,
bool blnUseCache = false,
int cacheDurationInSeconds = 2147483647)
{
ISugarQueryable<T> q = _db.Queryable<T>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere, lstParameters);
if (intTop.HasValue)
{
q = q.Take(intTop.Value);
}
return q.WithCacheIF(blnUseCache, cacheDurationInSeconds).ToList();
}
#endregion
#region QueryPage
/// <summary>
/// 功能描述:分页查询
/// 作 者:beck.huang
/// 创建日期:2018-05-10 10:55:06
/// 任务编号:中餐
/// </summary>
/// <param name="strWhere">条件</param>
/// <param name="intTotalCount">数据总数</param>
/// <param name="intPageIndex">当前页</param>
/// <param name="intPageSize">页大小</param>
/// <param name="lstParameters">参数</param>
/// <param name="strOrderByFileds">排序字段,如name asc,age desc</param>
/// <returns>数据列表</returns>
public List<T> QueryPage(
string strWhere,
ref int intTotalCount,
int intPageIndex = 0,
int intPageSize = 20,
List<SugarParameter> lstParameters = null,
string strOrderByFileds = null)
{
return _db.Queryable<T>()
.OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds)
.WhereIF(!string.IsNullOrEmpty(strWhere), strWhere, lstParameters)
.ToPageList(intPageIndex, intPageSize, ref intTotalCount);
}
/// <summary>
/// 功能描述:分页查询
/// 作 者:beck.huang
/// 创建日期:2018-05-10 10:55:06
/// 任务编号:中餐
/// </summary>
/// <param name="strWhere">条件</param>
/// <param name="intTotalCount">数据总数</param>
/// <param name="intPageIndex">当前页</param>
/// <param name="intPageSize">页大小</param>
/// <param name="lstParameters">参数</param>
/// <param name="strOrderByFileds">排序字段,如name asc,age desc</param>
/// <returns>数据列表</returns>
public List<T> QueryPage(
Expression<Func<T, bool>> whereExpression,
ref int intTotalCount,
int intPageIndex = 0,
int intPageSize = 20,
string strOrderByFileds = null)
{
return _db.Queryable<T>()
.OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds)
.WhereIF(whereExpression != null, whereExpression)
.ToPageList(intPageIndex, intPageSize, ref intTotalCount);
}
#endregion
/// <summary>
/// 功能描述:查询Table
/// 作 者:beck.huang
/// 创建日期:2018-05-16 18:03:14
/// 任务编号:中餐
/// </summary>
/// <param name="strSql">strSql</param>
/// <param name="lstParameters">参数</param>
/// <returns>DataTable</returns>
public DataTable QueryTable(string strSql, SugarParameter[] lstParameters = null)
{
return _db.Ado.GetDataTable(strSql, lstParameters);
}
/// <summary>
/// 功能描述:查询DataSet
/// 作 者:beck.huang
/// 创建日期:2018-05-16 18:06:05
/// 任务编号:中餐
/// </summary>
/// <param name="strSql">strSql</param>
/// <param name="lstParameters">参数</param>
/// <returns>DataSet</returns>
public DataSet QueryDataSet(string strSql, SugarParameter[] lstParameters = null)
{
return _db.Ado.GetDataSetAll(strSql, lstParameters);
}
#endregion
#region 事务
/// <summary>
/// 功能描述:开始事务
/// 作 者:beck.huang
/// 创建日期:2018-05-09 09:49:49
/// 任务编号:中餐
/// </summary>
public void BeginTran()
{
_db.Ado.BeginTran();
}
/// <summary>
/// 功能描述:提交事务
/// 作 者:beck.huang
/// 创建日期:2018-05-09 09:49:57
/// 任务编号:中餐
/// </summary>
public void CommitTran()
{
_db.Ado.CommitTran();
}
/// <summary>
/// 功能描述:回滚事务
/// 作 者:beck.huang
/// 创建日期:2018-05-09 09:50:01
/// 任务编号:中餐
/// </summary>
public void RollbackTran()
{
_db.Ado.RollbackTran();
}
#endregion
#region 其他
/// <summary>
/// 功能描述:获取数据库时间
/// 作 者:beck.huang
/// 创建日期:2018-05-09 19:41:31
/// 任务编号:中餐
/// </summary>
/// <returns>返回值</returns>
public DateTime GetDBTime()
{
return _db.GetDate();
}
/// <summary>
/// 功能描述:清除表缓存
/// 作 者:beck.huang
/// 创建日期:2018-05-11 10:15:51
/// 任务编号:中餐
/// </summary>
public void RemoveCache()
{
var cacheService = _db.CurrentConnectionConfig.ConfigureExternalServices.DataInfoCacheService;
string tableName = _db.EntityMaintenance.GetTableName<T>();
var keys = cacheService.GetAllKey<string>();
if (keys != null && keys.Count() > 0)
{
foreach (var item in keys)
{
if (item.ToLower().Contains("." tableName.ToLower() "."))
{
cacheService.Remove<string>(item);
}
}
}
}
/// <summary>
/// 功能描述:关闭连接
/// 作 者:beck.huang
/// 创建日期:2018-05-28 17:37:16
/// 任务编号:中餐
/// </summary>
public void CloseDB()
{
if (_context != null && _context.Db != null)
{
_context.Db.Close();
_context.Db.Dispose();
}
}
#endregion
#region 事件
///// <summary>
///// 功能描述:Sql执行完发生
///// 作 者:beck.huang
///// 创建日期:2018-05-11 10:40:25
///// 任务编号:中餐
///// </summary>
///// <param name="sql">sql</param>
///// <param name="pars">pars</param>
//public virtual void OnLogExecuted(string sql, SugarParameter[] pars)
//{
//}
///// <summary>
///// 功能描述:Sql执行前发生
///// 作 者:beck.huang
///// 创建日期:2018-05-11 10:40:25
///// 任务编号:中餐
///// </summary>
///// <param name="sql">sql</param>
///// <param name="pars">pars</param>
//public virtual void OnLogExecuting(string sql, SugarParameter[] pars)
//{
//}
///// <summary>
///// 功能描述:执行SQL 错误时发生
///// 作 者:beck.huang
///// 创建日期:2018-05-11 10:40:25
///// 任务编号:中餐
///// </summary>
///// <param name="ex">错误</param>
//public virtual void OnError(Exception ex)
//{
//}
///// <summary>
///// 功能描述:SQL执行前 可以修改SQL
///// 作 者:beck.huang
///// 创建日期:2018-05-11 10:47:37
///// 任务编号:中餐
///// </summary>
///// <param name="sql">sql</param>
///// <param name="pars">pars</param>
///// <returns>返回值</returns>
//public virtual KeyValuePair<string, SugarParameter[]> OnExecutingChangeSql(string sql, SugarParameter[] pars)
//{
// return new KeyValuePair<string, SugarParameter[]>(sql, pars);
//}
#endregion
}
HttpRuntimeCache
代码语言:javascript复制 1 using SqlSugar;
2 using System;
3 using System.Collections;
4 using System.Collections.Generic;
5 using System.Linq;
6 using System.Text;
7 using System.Web;
8 using System.Web.Caching;
9
10 namespace Km.DB
11 {
12 public class HttpRuntimeCache : ICacheService
13 {
14 public void Add<V>(string key, V value)
15 {
16 HttpRuntimeCacheHelper<V>.GetInstance().Add(key, value);
17 }
18
19 public void Add<V>(string key, V value, int cacheDurationInSeconds)
20 {
21 HttpRuntimeCacheHelper<V>.GetInstance().Add(key, value, cacheDurationInSeconds);
22 }
23
24 public bool ContainsKey<V>(string key)
25 {
26 return HttpRuntimeCacheHelper<V>.GetInstance().ContainsKey(key);
27 }
28
29 public V Get<V>(string key)
30 {
31 return HttpRuntimeCacheHelper<V>.GetInstance().Get(key);
32 }
33
34 public IEnumerable<string> GetAllKey<V>()
35 {
36 return HttpRuntimeCacheHelper<V>.GetInstance().GetAllKey();
37 }
38
39 public V GetOrCreate<V>(string cacheKey, Func<V> create, int cacheDurationInSeconds = int.MaxValue)
40 {
41 var cacheManager = HttpRuntimeCacheHelper<V>.GetInstance();
42 if (cacheManager.ContainsKey(cacheKey))
43 {
44 return cacheManager[cacheKey];
45 }
46 else
47 {
48 var result = create();
49 cacheManager.Add(cacheKey, result, cacheDurationInSeconds);
50 return result;
51 }
52 }
53
54 public void Remove<V>(string key)
55 {
56 HttpRuntimeCacheHelper<V>.GetInstance().Remove(key);
57 }
58 }
59
60 internal class HttpRuntimeCacheHelper<V>
61 {
62
63 #region 全局变量
64 private static HttpRuntimeCacheHelper<V> _instance = null;
65 private static readonly object _instanceLock = new object();
66 #endregion
67
68 #region 构造函数
69
70 private HttpRuntimeCacheHelper() { }
71 #endregion
72
73 #region 属性
74 /// <summary>
75 ///根据key获取value
76 /// </summary>
77 /// <value></value>
78 public V this[string key]
79 {
80 get { return (V)HttpRuntime.Cache[CreateKey(key)]; }
81 }
82 #endregion
83
84 #region 公共函数
85
86 /// <summary>
87 /// key是否存在
88 /// </summary>
89 /// <param name="key">key</param>
90 /// <returns> /// 存在<c>true</c> 不存在<c>false</c>. /// /// </returns>
91 public bool ContainsKey(string key)
92 {
93 return HttpRuntime.Cache[CreateKey(key)] != null;
94 }
95
96 /// <summary>
97 /// 获取缓存值
98 /// </summary>
99 /// <param name="key">key</param>
100 /// <returns></returns>
101 public V Get(string key)
102 {
103 return (V)HttpRuntime.Cache.Get(CreateKey(key));
104 }
105
106 /// <summary>
107 /// 获取实例 (单例模式)
108 /// </summary>
109 /// <returns></returns>
110 public static HttpRuntimeCacheHelper<V> GetInstance()
111 {
112 if (_instance == null)
113 lock (_instanceLock)
114 if (_instance == null)
115 _instance = new HttpRuntimeCacheHelper<V>();
116 return _instance;
117 }
118
119 /// <summary>
120 /// 插入缓存(默认20分钟)
121 /// </summary>
122 /// <param name="key"> key</param>
123 /// <param name="value">value</param>
124 public void Add(string key, V value)
125 {
126 Add(key, value, 60 * 20);
127 }
128
129 /// <summary>
130 /// 插入缓存
131 /// </summary>
132 /// <param name="key"> key</param>
133 /// <param name="value">value</param>
134 /// <param name="cacheDurationInSeconds">过期时间单位秒</param>
135 public void Add(string key, V value, int cacheDurationInSeconds)
136 {
137 Add(key, value, cacheDurationInSeconds, CacheItemPriority.Default);
138 }
139
140 /// <summary>
141 /// 插入缓存.
142 /// </summary>
143 /// <param name="key">key</param>
144 /// <param name="value">value</param>
145 /// <param name="cacheDurationInSeconds">过期时间单位秒</param>
146 /// <param name="priority">缓存项属性</param>
147 public void Add(string key, V value, int cacheDurationInSeconds, CacheItemPriority priority)
148 {
149 string keyString = CreateKey(key);
150 HttpRuntime.Cache.Insert(keyString, value, null,
151 DateTime.Now.AddSeconds(cacheDurationInSeconds), Cache.NoSlidingExpiration, priority, null);
152 }
153
154 /// <summary>
155 /// 插入缓存.
156 /// </summary>
157 /// <param name="key">key</param>
158 /// <param name="value">value</param>
159 /// <param name="cacheDurationInSeconds">过期时间单位秒</param>
160 /// <param name="priority">缓存项属性</param>
161 public void Add(string key, V value, int
162 cacheDurationInSeconds, CacheDependency dependency, CacheItemPriority priority)
163 {
164 string keyString = CreateKey(key);
165 HttpRuntime.Cache.Insert(keyString, value,
166 dependency, DateTime.Now.AddSeconds(cacheDurationInSeconds), Cache.NoSlidingExpiration, priority, null);
167 }
168
169 /// <summary>
170 /// 删除缓存
171 /// </summary>
172 /// <param name="key">key</param>
173 public void Remove(string key)
174 {
175 HttpRuntime.Cache.Remove(CreateKey(key));
176 }
177
178 /// <summary>
179 /// 清除所有缓存
180 /// </summary>
181 public void RemoveAll()
182 {
183 System.Web.Caching.Cache cache = HttpRuntime.Cache;
184 IDictionaryEnumerator CacheEnum = cache.GetEnumerator();
185 ArrayList al = new ArrayList();
186 while (CacheEnum.MoveNext())
187 {
188 al.Add(CacheEnum.Key);
189 }
190 foreach (string key in al)
191 {
192 cache.Remove(key);
193 }
194 }
195
196 /// <summary>
197 /// 清除所有包含关键字的缓存
198 /// </summary>
199 /// <param name="removeKey">关键字</param>
200 public void RemoveAll(Func<string, bool> removeExpression)
201 {
202 System.Web.Caching.Cache _cache = System.Web.HttpRuntime.Cache;
203 var allKeyList = GetAllKey();
204 var delKeyList = allKeyList.Where(removeExpression).ToList();
205 foreach (var key in delKeyList)
206 {
207 HttpRuntime.Cache.Remove(key); ;
208 }
209 }
210
211 /// <summary>
212 /// 获取所有缓存key
213 /// </summary>
214 /// <returns></returns>
215 public IEnumerable<string> GetAllKey()
216 {
217 IDictionaryEnumerator CacheEnum = HttpRuntime.Cache.GetEnumerator();
218 while (CacheEnum.MoveNext())
219 {
220 yield return CacheEnum.Key.ToString();
221 }
222 }
223 #endregion
224
225 #region 私有函数
226
227 /// <summary>
228 ///创建KEY
229 /// </summary>
230 /// <param name="key">Key</param>
231 /// <returns></returns>
232 private string CreateKey(string key)
233 {
234 return key;
235 }
236 #endregion
237 }
238 }
至于为什么要再封装一层,是为了学习成本,如果不封装,整个项目组大家都要学习Sqlsugar,封装了之后,只需要我自己学习就可以了,别人只用我封装的类就可以