经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » ASP.net » 查看文章
net core 使用 SqlSugar
来源:cnblogs  作者:調調  时间:2018/11/8 9:55:36  对本文有异议
  1. 1 /// <summary>
  2. 2 /// SqlSugar 注入Service的扩展方法
  3. 3 /// </summary>
  4. 4 public static class SqlSugarServiceCollectionExtensions
  5. 5 {
  6. 6 /// <summary>
  7. 7 /// SqlSugar上下文注入
  8. 8 /// </summary>
  9. 9 /// <typeparam name="TSugarContext">要注册的上下文的类型</typeparam>
  10. 10 /// <param name="serviceCollection"></param>
  11. 11 /// <param name="configAction"></param>
  12. 12 /// <param name="lifetime">用于在容器中注册TSugarClient服务的生命周期</param>
  13. 13 /// <returns></returns>
  14. 14 public static IServiceCollection AddSqlSugarClient<TSugarContext>(this IServiceCollection serviceCollection, Action<IServiceProvider, ConnectionConfig> configAction, ServiceLifetime lifetime = ServiceLifetime.Singleton)
  15. 15 where TSugarContext : IDbFactory
  16. 16 {
  17. 17 serviceCollection.AddMemoryCache().AddLogging();
  18. 18 serviceCollection.TryAdd(new ServiceDescriptor(typeof(ConnectionConfig), p => ConnectionConfigFactory(p, configAction), lifetime));
  19. 19 serviceCollection.Add(new ServiceDescriptor(typeof(ConnectionConfig), p => ConnectionConfigFactory(p, configAction), lifetime));
  20. 20 serviceCollection.TryAdd(new ServiceDescriptor(typeof(TSugarContext), typeof(TSugarContext), lifetime));
  21. 21 return serviceCollection;
  22. 22 }
  23. 23
  24. 24 private static ConnectionConfig ConnectionConfigFactory(IServiceProvider applicationServiceProvider, Action<IServiceProvider, ConnectionConfig> configAction)
  25. 25 {
  26. 26 var config = new ConnectionConfig();
  27. 27 configAction.Invoke(applicationServiceProvider, config);
  28. 28 return config;
  29. 29 }
  30. 30 }
注入扩展
  1. 1 public interface IDbFactory
  2. 2 {
  3. 3 SqlSugarClient GetDbContext(Action<Exception> onErrorEvent);
  4. 4 SqlSugarClient GetDbContext(Action<string, SugarParameter[]> onExecutedEvent);
  5. 5 SqlSugarClient GetDbContext(Func<string, SugarParameter[], KeyValuePair<string, SugarParameter[]>> onExecutingChangeSqlEvent);
  6. 6 SqlSugarClient GetDbContext(Action<string, SugarParameter[]> onExecutedEvent = null, Func<string, SugarParameter[], KeyValuePair<string, SugarParameter[]>> onExecutingChangeSqlEvent = null, Action<Exception> onErrorEvent = null);
  7. 7 }
IDbFactory
  1. 1 public class DbFactory : IDbFactory
  2. 2 {
  3. 3 private readonly ILogger _logger;
  4. 4 private readonly ConnectionConfig _config;
  5. 5
  6. 6 public DbFactory(ConnectionConfig config, ILogger<DbFactory> logger)
  7. 7 {
  8. 8 this._logger = logger;
  9. 9 this._config = config;
  10. 10 }
  11. 11
  12. 12 public SqlSugarClient GetDbContext(Action<Exception> onErrorEvent) => GetDbContext(null, null, onErrorEvent);
  13. 13 public SqlSugarClient GetDbContext(Action<string, SugarParameter[]> onExecutedEvent) => GetDbContext(onExecutedEvent);
  14. 14 public SqlSugarClient GetDbContext(Func<string, SugarParameter[], KeyValuePair<string, SugarParameter[]>> onExecutingChangeSqlEvent) => GetDbContext(null, onExecutingChangeSqlEvent);
  15. 15 public SqlSugarClient GetDbContext(Action<string, SugarParameter[]> onExecutedEvent = null, Func<string, SugarParameter[], KeyValuePair<string, SugarParameter[]>> onExecutingChangeSqlEvent = null, Action<Exception> onErrorEvent = null)
  16. 16 {
  17. 17 SqlSugarClient db = new SqlSugarClient(_config)
  18. 18 {
  19. 19 Aop =
  20. 20 {
  21. 21 OnExecutingChangeSql = onExecutingChangeSqlEvent,
  22. 22 OnError = onErrorEvent ?? ((Exception ex) => { this._logger.LogError(ex, "ExecuteSql Error"); }),
  23. 23 OnLogExecuted =onExecutedEvent?? ((string sql, SugarParameter[] pars) =>
  24. 24 {
  25. 25 var keyDic = new KeyValuePair<string, SugarParameter[]>(sql, pars);
  26. 26 this._logger.LogInformation($"ExecuteSql:【{keyDic.ToJson()}】");
  27. 27 })
  28. 28 }
  29. 29 };
  30. 30 return db;
  31. 31 }
  32. 32 }
DbFactory
  1. public interface IRepository
  2. {
  3. }
IRepository
  1. 1 public class Repository<TFactory, TIRepository> : IRepository where TFactory : IDbFactory where TIRepository : IRepository
  2. 2 {
  3. 3 protected readonly ILogger Log;
  4. 4 protected readonly TFactory Factory;
  5. 5 protected readonly TIRepository DbRepository;
  6. 6 protected SqlSugarClient DbContext => this.Factory.GetDbContext();
  7. 7
  8. 8 public Repository(TFactory factory) => Factory = factory;
  9. 9 public Repository(TFactory factory, ILogger logger) : this(factory) => Log = logger;
  10. 10 public Repository(TFactory factory, TIRepository repository) : this(factory) => DbRepository = repository;
  11. 11 public Repository(TFactory factory, TIRepository repository, ILogger logger) : this(factory, repository) => Log = logger;
  12. 12 }
  13. 13
  14. 14 public class Repository<TFactory> : IRepository where TFactory : IDbFactory
  15. 15 {
  16. 16 protected readonly ILogger Log;
  17. 17 protected readonly TFactory Factory;
  18. 18 protected SqlSugarClient DbContext => this.Factory.GetDbContext();
  19. 19
  20. 20 public Repository(TFactory factory) => Factory = factory;
  21. 21 public Repository(TFactory factory, ILogger logger) : this(factory) => Log = logger;
  22. 22 }
Repository
  1. 1 public static class SugarFactoryExtensions
  2. 2 {
  3. 3
  4. 4 #region 根据主键获取实体对象
  5. 5
  6. 6 /// <summary>
  7. 7 /// 根据主键获取实体对象
  8. 8 /// </summary>
  9. 9 /// <typeparam name="TSource">数据源类型</typeparam>
  10. 10 /// <param name="db"></param>
  11. 11 /// <param name="id"></param>
  12. 12 /// <returns></returns>
  13. 13 public static TSource GetById<TSource>(this SqlSugarClient db, dynamic id) where TSource : EntityBase, new()
  14. 14 {
  15. 15 return db.Queryable<TSource>().InSingle(id);
  16. 16 }
  17. 17
  18. 18 /// <summary>
  19. 19 /// 根据主键获取实体对象
  20. 20 /// </summary>
  21. 21 /// <typeparam name="TSource">数据源类型</typeparam>
  22. 22 /// <typeparam name="TMap">数据源映射类型</typeparam>
  23. 23 /// <param name="db"></param>
  24. 24 /// <param name="id"></param>
  25. 25 /// <returns></returns>
  26. 26 public static TMap GetById<TSource, TMap>(this SqlSugarClient db, dynamic id) where TSource : EntityBase, new()
  27. 27 {
  28. 28 TSource model = db.Queryable<TSource>().InSingle(id);
  29. 29 return model.Map<TSource, TMap>();
  30. 30 }
  31. 31
  32. 32 #endregion
  33. 33
  34. 34 #region 根据Linq表达式条件获取单个实体对象
  35. 35
  36. 36 /// <summary>
  37. 37 /// 根据条件获取单个实体对象
  38. 38 /// </summary>
  39. 39 /// <typeparam name="TSource">数据源类型</typeparam>
  40. 40 /// <param name="db"></param>
  41. 41 /// <param name="whereExp"></param>
  42. 42 /// <returns></returns>
  43. 43 public static TSource Get<TSource>(this SqlSugarClient db, Expression<Func<TSource, bool>> whereExp) where TSource : EntityBase, new()
  44. 44 {
  45. 45 return db.Queryable<TSource>().Where(whereExp).Single();
  46. 46 }
  47. 47
  48. 48 /// <summary>
  49. 49 /// 根据条件获取单个实体对象
  50. 50 /// </summary>
  51. 51 /// <typeparam name="TSource">数据源类型</typeparam>
  52. 52 /// <typeparam name="TMap">数据源映射类型</typeparam>
  53. 53 /// <param name="db"></param>
  54. 54 /// <param name="whereExp">条件表达式</param>
  55. 55 /// <returns></returns>
  56. 56 public static TMap Get<TSource, TMap>(this SqlSugarClient db, Expression<Func<TSource, bool>> whereExp) where TSource : EntityBase, new()
  57. 57 {
  58. 58 TSource model = db.Queryable<TSource>().Where(whereExp).Single();
  59. 59 return model.Map<TSource, TMap>();
  60. 60 }
  61. 61
  62. 62 #endregion
  63. 63
  64. 64 #region 获取所有实体列表
  65. 65
  66. 66 /// <summary>
  67. 67 /// 获取所有实体列表
  68. 68 /// </summary>
  69. 69 /// <typeparam name="TSource">数据源类型</typeparam>
  70. 70 /// <param name="db"></param>
  71. 71 /// <returns></returns>
  72. 72 public static List<TSource> GetList<TSource>(this SqlSugarClient db) where TSource : EntityBase, new()
  73. 73 {
  74. 74 return db.Queryable<TSource>().ToList();
  75. 75 }
  76. 76
  77. 77 /// <summary>
  78. 78 /// 获取实体列表
  79. 79 /// </summary>
  80. 80 /// <typeparam name="TSource">数据源类型</typeparam>
  81. 81 /// <typeparam name="TMap">数据源映射类型</typeparam>
  82. 82 /// <param name="db"></param>
  83. 83 /// <returns></returns>
  84. 84 public static List<TMap> GetList<TSource, TMap>(this SqlSugarClient db) where TSource : EntityBase, new()
  85. 85 {
  86. 86 var result = db.Queryable<TSource>().ToList();
  87. 87 return result.Map<List<TSource>, List<TMap>>();
  88. 88 }
  89. 89
  90. 90 #endregion
  91. 91
  92. 92 #region 根据Linq表达式条件获取列表
  93. 93
  94. 94 /// <summary>
  95. 95 /// 根据条件获取实体列表
  96. 96 /// </summary>
  97. 97 /// <typeparam name="TSource">数据源类型</typeparam>
  98. 98 /// <param name="db"></param>
  99. 99 /// <param name="whereExp">条件表达式</param>
  100. 100 /// <returns></returns>
  101. 101 public static List<TSource> GetList<TSource>(this SqlSugarClient db, Expression<Func<TSource, bool>> whereExp) where TSource : EntityBase, new()
  102. 102 {
  103. 103 return db.Queryable<TSource>().Where(whereExp).ToList();
  104. 104 }
  105. 105
  106. 106 /// <summary>
  107. 107 /// 根据条件获取实体列表
  108. 108 /// </summary>
  109. 109 /// <typeparam name="TSource">数据源类型</typeparam>
  110. 110 /// <typeparam name="TMap">数据源映射类型</typeparam>
  111. 111 /// <param name="db"></param>
  112. 112 /// <param name="whereExp">条件表达式</param>
  113. 113 /// <returns></returns>
  114. 114 public static List<TMap> GetList<TSource, TMap>(this SqlSugarClient db, Expression<Func<TSource, bool>> whereExp) where TSource : EntityBase, new()
  115. 115 {
  116. 116 var result = db.Queryable<TSource>().Where(whereExp).ToList();
  117. 117 return result.Map<List<TSource>, List<TMap>>();
  118. 118 }
  119. 119
  120. 120 #endregion
  121. 121
  122. 122 #region 根据Sugar条件获取列表
  123. 123
  124. 124 /// <summary>
  125. 125 /// 根据条件获取实体列表
  126. 126 /// </summary>
  127. 127 /// <typeparam name="TSource"></typeparam>
  128. 128 /// <param name="db"></param>
  129. 129 /// <param name="conditionals">Sugar调价表达式集合</param>
  130. 130 /// <returns></returns>
  131. 131 public static List<TSource> GetList<TSource>(this SqlSugarClient db, List<IConditionalModel> conditionals) where TSource : EntityBase, new()
  132. 132 {
  133. 133 return db.Queryable<TSource>().Where(conditionals).ToList();
  134. 134 }
  135. 135
  136. 136 /// <summary>
  137. 137 /// 根据条件获取实体列表
  138. 138 /// </summary>
  139. 139 /// <typeparam name="TSource">数据源类型</typeparam>
  140. 140 /// <typeparam name="TMap">数据源映射类型</typeparam>
  141. 141 /// <param name="db"></param>
  142. 142 /// <param name="conditionals">Sugar调价表达式集合</param>
  143. 143 /// <returns></returns>
  144. 144 public static List<TMap> GetList<TSource, TMap>(this SqlSugarClient db, List<IConditionalModel> conditionals) where TSource : EntityBase, new()
  145. 145 {
  146. 146 var result = db.Queryable<TSource>().Where(conditionals).ToList();
  147. 147 return result.Map<List<TSource>, List<TMap>>();
  148. 148 }
  149. 149
  150. 150 #endregion
  151. 151
  152. 152 #region 是否包含某个元素
  153. 153 /// <summary>
  154. 154 /// 是否包含某个元素
  155. 155 /// </summary>
  156. 156 /// <typeparam name="TSource"></typeparam>
  157. 157 /// <param name="db"></param>
  158. 158 /// <param name="whereExp">条件表达式</param>
  159. 159 /// <returns></returns>
  160. 160 public static bool Exist<TSource>(this SqlSugarClient db, Expression<Func<TSource, bool>> whereExp) where TSource : EntityBase, new()
  161. 161 {
  162. 162 return db.Queryable<TSource>().Where(whereExp).Any();
  163. 163 }
  164. 164 #endregion
  165. 165
  166. 166 #region 新增实体对象
  167. 167 /// <summary>
  168. 168 /// 新增实体对象
  169. 169 /// </summary>
  170. 170 /// <typeparam name="TSource"></typeparam>
  171. 171 /// <param name="db"></param>
  172. 172 /// <param name="insertObj"></param>
  173. 173 /// <returns></returns>
  174. 174 public static bool Insert<TSource>(this SqlSugarClient db, TSource insertObj) where TSource : EntityBase, new()
  175. 175 {
  176. 176 return db.Insertable(insertObj).ExecuteCommand() > 0;
  177. 177 }
  178. 178
  179. 179 /// <summary>
  180. 180 /// 新增实体对象
  181. 181 /// </summary>
  182. 182 /// <typeparam name="TSource"></typeparam>
  183. 183 /// <typeparam name="TMap"></typeparam>
  184. 184 /// <param name="db"></param>
  185. 185 /// <param name="insertDto"></param>
  186. 186 /// <returns></returns>
  187. 187 public static bool Insert<TSource, TMap>(this SqlSugarClient db, TSource insertDto) where TMap : EntityBase, new()
  188. 188 {
  189. 189 var entity = insertDto.Map<TSource, TMap>();
  190. 190 return db.Insertable(entity).ExecuteCommand() > 0;
  191. 191 }
  192. 192 #endregion
  193. 193
  194. 194 #region 批量新增实体对象
  195. 195 /// <summary>
  196. 196 /// 批量新增实体对象
  197. 197 /// </summary>
  198. 198 /// <typeparam name="TSource"></typeparam>
  199. 199 /// <param name="db"></param>
  200. 200 /// <param name="insertObjs"></param>
  201. 201 /// <returns></returns>
  202. 202 public static bool InsertRange<TSource>(this SqlSugarClient db, List<TSource> insertObjs) where TSource : EntityBase, new()
  203. 203 {
  204. 204 return db.Insertable(insertObjs).ExecuteCommand() > 0;
  205. 205 }
  206. 206
  207. 207 /// <summary>
  208. 208 /// 批量新增实体对象
  209. 209 /// </summary>
  210. 210 /// <typeparam name="TSource"></typeparam>
  211. 211 /// <typeparam name="TMap"></typeparam>
  212. 212 /// <param name="db"></param>
  213. 213 /// <param name="insertObjs"></param>
  214. 214 /// <returns></returns>
  215. 215 public static bool InsertRange<TSource, TMap>(this SqlSugarClient db, List<TSource> insertObjs) where TMap : EntityBase, new()
  216. 216 {
  217. 217 var entitys = insertObjs.Map<List<TSource>, List<TMap>>();
  218. 218 return db.Insertable(entitys).ExecuteCommand() > 0;
  219. 219 }
  220. 220 #endregion
  221. 221
  222. 222 #region 更新单个实体对象
  223. 223 /// <summary>
  224. 224 /// 更新单个实体对象
  225. 225 /// </summary>
  226. 226 /// <typeparam name="TSource"></typeparam>
  227. 227 /// <param name="db"></param>
  228. 228 /// <param name="updateObj"></param>
  229. 229 /// <returns></returns>
  230. 230 public static bool Update<TSource>(this SqlSugarClient db, TSource updateObj) where TSource : EntityBase, new()
  231. 231 {
  232. 232 return db.Updateable(updateObj).ExecuteCommand() > 0;
  233. 233 }
  234. 234 #endregion
  235. 235
  236. 236 #region 根据条件批量更新实体指定列
  237. 237 /// <summary>
  238. 238 /// 根据条件批量更新实体指定列
  239. 239 /// </summary>
  240. 240 /// <typeparam name="TSource"></typeparam>
  241. 241 /// <param name="db"></param>
  242. 242 /// <param name="columns">需要更新的列</param>
  243. 243 /// <param name="whereExp">条件表达式</param>
  244. 244 /// <returns></returns>
  245. 245 public static bool Update<TSource>(this SqlSugarClient db, Expression<Func<TSource, TSource>> columns, Expression<Func<TSource, bool>> whereExp) where TSource : EntityBase, new()
  246. 246 {
  247. 247 return db.Updateable<TSource>().UpdateColumns(columns).Where(whereExp).ExecuteCommand() > 0;
  248. 248 }
  249. 249 #endregion
  250. 250
  251. 251 #region 物理删除实体对象
  252. 252
  253. 253 /// <summary>
  254. 254 /// 物理删除实体对象
  255. 255 /// </summary>
  256. 256 /// <typeparam name="TSource"></typeparam>
  257. 257 /// <param name="db"></param>
  258. 258 /// <param name="deleteObj"></param>
  259. 259 /// <returns></returns>
  260. 260 public static bool Delete<TSource>(this SqlSugarClient db, TSource deleteObj) where TSource : EntityBase, new()
  261. 261 {
  262. 262 return db.Deleteable<TSource>().Where(deleteObj).ExecuteCommand() > 0;
  263. 263 }
  264. 264
  265. 265 /// <summary>
  266. 266 /// 物理删除实体对象
  267. 267 /// </summary>
  268. 268 /// <typeparam name="TSource"></typeparam>
  269. 269 /// <param name="db"></param>
  270. 270 /// <param name="whereExp">条件表达式</param>
  271. 271 /// <returns></returns>
  272. 272 public static bool Delete<TSource>(this SqlSugarClient db, Expression<Func<TSource, bool>> whereExp) where TSource : EntityBase, new()
  273. 273 {
  274. 274 return db.Deleteable<TSource>().Where(whereExp).ExecuteCommand() > 0;
  275. 275 }
  276. 276
  277. 277 /// <summary>
  278. 278 /// 根据主键物理删除实体对象
  279. 279 /// </summary>
  280. 280 /// <typeparam name="TSource"></typeparam>
  281. 281 /// <param name="db"></param>
  282. 282 /// <param name="id"></param>
  283. 283 /// <returns></returns>
  284. 284 public static bool DeleteById<TSource>(this SqlSugarClient db, dynamic id) where TSource : EntityBase, new()
  285. 285 {
  286. 286 return db.Deleteable<TSource>().In(id).ExecuteCommand() > 0;
  287. 287 }
  288. 288
  289. 289 /// <summary>
  290. 290 /// 根据主键批量物理删除实体集合
  291. 291 /// </summary>
  292. 292 /// <typeparam name="TSource"></typeparam>
  293. 293 /// <param name="db"></param>
  294. 294 /// <param name="ids"></param>
  295. 295 /// <returns></returns>
  296. 296 public static bool DeleteByIds<TSource>(this SqlSugarClient db, dynamic[] ids) where TSource : EntityBase, new()
  297. 297 {
  298. 298 return db.Deleteable<TSource>().In(ids).ExecuteCommand() > 0;
  299. 299 }
  300. 300
  301. 301 #endregion
  302. 302
  303. 303 #region 分页查询
  304. 304
  305. 305 /// <summary>
  306. 306 /// 获取分页列表【页码,每页条数】
  307. 307 /// </summary>
  308. 308 /// <typeparam name="TSource">数据源类型</typeparam>
  309. 309 /// <param name="db"></param>
  310. 310 /// <param name="pageIndex">页码(从0开始)</param>
  311. 311 /// <param name="pageSize">每页条数</param>
  312. 312 /// <returns></returns>
  313. 313 public static IPagedList<TSource> GetPageList<TSource>(this SqlSugarClient db, int pageIndex, int pageSize) where TSource : EntityBase, new()
  314. 314 {
  315. 315 int count = 0;
  316. 316 var result = db.Queryable<TSource>().ToPageList(pageIndex, pageSize, ref count);
  317. 317 return new PagedList<TSource>(result, pageIndex, pageSize, count);
  318. 318 }
  319. 319
  320. 320 /// <summary>
  321. 321 /// 获取分页列表【页码,每页条数】
  322. 322 /// </summary>
  323. 323 /// <typeparam name="TSource">数据源类型</typeparam>
  324. 324 /// <typeparam name="TMap">数据源映射类型</typeparam>
  325. 325 /// <param name="db"></param>
  326. 326 /// <param name="pageIndex">页码(从0开始)</param>
  327. 327 /// <param name="pageSize">每页条数</param>
  328. 328 /// <returns></returns>
  329. 329 public static IPagedList<TMap> GetPageList<TSource, TMap>(this SqlSugarClient db, int pageIndex, int pageSize) where TSource : EntityBase, new()
  330. 330 {
  331. 331 int count = 0;
  332. 332 var result = db.Queryable<TSource>().ToPageList(pageIndex, pageSize, ref count);
  333. 333 var pageResult = new PagedList<TSource>(result, pageIndex, pageSize, count);
  334. 334 return pageResult.Map<TSource, TMap>();
  335. 335 }
  336. 336
  337. 337 #endregion
  338. 338
  339. 339 #region 分页查询(排序)
  340. 340
  341. 341 /// <summary>
  342. 342 /// 获取分页列表【排序,页码,每页条数】
  343. 343 /// </summary>
  344. 344 /// <typeparam name="TSource">数据源类型</typeparam>
  345. 345 /// <param name="db"></param>
  346. 346 /// <param name="orderExp">排序表达式</param>
  347. 347 /// <param name="orderType">排序类型</param>
  348. 348 /// <param name="pageIndex">页码(从0开始)</param>
  349. 349 /// <param name="pageSize">每页条数</param>
  350. 350 /// <returns></returns>
  351. 351 public static IPagedList<TSource> GetPageList<TSource>(this SqlSugarClient db, Expression<Func<TSource, object>> orderExp, OrderByType orderType, int pageIndex, int pageSize) where TSource : EntityBase, new()
  352. 352 {
  353. 353 int count = 0;
  354. 354 var result = db.Queryable<TSource>().OrderBy(orderExp, orderType).ToPageList(pageIndex, pageSize, ref count);
  355. 355 return new PagedList<TSource>(result, pageIndex, pageSize, count);
  356. 356 }
  357. 357
  358. 358 /// <summary>
  359. 359 /// 获取分页列表【排序,页码,每页条数】
  360. 360 /// </summary>
  361. 361 /// <typeparam name="TSource">数据源类型</typeparam>
  362. 362 /// <typeparam name="TMap">数据源映射类型</typeparam>
  363. 363 /// <param name="db"></param>
  364. 364 /// <param name="orderExp">排序表达式</param>
  365. 365 /// <param name="orderType">排序类型</param>
  366. 366 /// <param name="pageIndex">页码(从0开始)</param>
  367. 367 /// <param name="pageSize">每页条数</param>
  368. 368 /// <returns></returns>
  369. 369 public static IPagedList<TMap> GetPageList<TSource, TMap>(this SqlSugarClient db, Expression<Func<TSource, object>> orderExp, OrderByType orderType, int pageIndex, int pageSize) where TSource : EntityBase, new()
  370. 370 {
  371. 371 int count = 0;
  372. 372 var result = db.Queryable<TSource>().OrderBy(orderExp, orderType).ToPageList(pageIndex, pageSize, ref count);
  373. 373 var pageResult = new PagedList<TSource>(result, pageIndex, pageSize, count);
  374. 374 return pageResult.Map<TSource, TMap>();
  375. 375 }
  376. 376
  377. 377 #endregion
  378. 378
  379. 379 #region 分页查询(Linq表达式条件)
  380. 380
  381. 381 /// <summary>
  382. 382 /// 获取分页列表【Linq表达式条件,页码,每页条数】
  383. 383 /// </summary>
  384. 384 /// <typeparam name="TSource">数据源类型</typeparam>
  385. 385 /// <param name="db"></param>
  386. 386 /// <param name="whereExp">Linq表达式条件</param>
  387. 387 /// <param name="pageIndex">页码(从0开始)</param>
  388. 388 /// <param name="pageSize">每页条数</param>
  389. 389 /// <returns></returns>
  390. 390 public static IPagedList<TSource> GetPageList<TSource>(this SqlSugarClient db, Expression<Func<TSource, bool>> whereExp, int pageIndex, int pageSize) where TSource : EntityBase, new()
  391. 391 {
  392. 392 int count = 0;
  393. 393 var result = db.Queryable<TSource>().Where(whereExp).ToPageList(pageIndex, pageSize, ref count);
  394. 394 return new PagedList<TSource>(result, pageIndex, pageSize, count);
  395. 395 }
  396. 396
  397. 397 /// <summary>
  398. 398 /// 获取分页列表【Linq表达式条件,页码,每页条数】
  399. 399 /// </summary>
  400. 400 /// <typeparam name="TSource">数据源类型</typeparam>
  401. 401 /// <typeparam name="TMap">数据源映射类型</typeparam>
  402. 402 /// <param name="db"></param>
  403. 403 /// <param name="whereExp">Linq表达式条件</param>
  404. 404 /// <param name="pageIndex">页码(从0开始)</param>
  405. 405 /// <param name="pageSize">每页条数</param>
  406. 406 /// <returns></returns>
  407. 407 public static IPagedList<TMap> GetPageList<TSource, TMap>(this SqlSugarClient db, Expression<Func<TSource, bool>> whereExp, int pageIndex, int pageSize) where TSource : EntityBase, new()
  408. 408 {
  409. 409 int count = 0;
  410. 410 var result = db.Queryable<TSource>().Where(whereExp).ToPageList(pageIndex, pageSize, ref count);
  411. 411 var pageResult = new PagedList<TSource>(result, pageIndex, pageSize, count);
  412. 412 return pageResult.Map<TSource, TMap>();
  413. 413 }
  414. 414
  415. 415 #endregion
  416. 416
  417. 417 #region 分页查询(Linq表达式条件,排序)
  418. 418
  419. 419 /// <summary>
  420. 420 /// 获取分页列表【Linq表达式条件,排序,页码,每页条数】
  421. 421 /// </summary>
  422. 422 /// <typeparam name="TSource">数据源类型</typeparam>
  423. 423 /// <param name="db"></param>
  424. 424 /// <param name="whereExp">Linq表达式条件</param>
  425. 425 /// <param name="orderExp">排序表达式</param>
  426. 426 /// <param name="orderType">排序类型</param>
  427. 427 /// <param name="pageIndex">页码(从0开始)</param>
  428. 428 /// <param name="pageSize">每页条数</param>
  429. 429 /// <returns></returns>
  430. 430 public static IPagedList<TSource> GetPageList<TSource>(this SqlSugarClient db, Expression<Func<TSource, bool>> whereExp, Expression<Func<TSource, object>> orderExp, OrderByType orderType, int pageIndex, int pageSize) where TSource : EntityBase, new()
  431. 431 {
  432. 432 int count = 0;
  433. 433 var result = db.Queryable<TSource>().Where(whereExp).OrderBy(orderExp, orderType).ToPageList(pageIndex, pageSize, ref count);
  434. 434 return new PagedList<TSource>(result, pageIndex, pageSize, count);
  435. 435 }
  436. 436
  437. 437 /// <summary>
  438. 438 /// 获取分页列表【Linq表达式条件,排序,页码,每页条数】
  439. 439 /// </summary>
  440. 440 /// <typeparam name="TSource">数据源类型</typeparam>
  441. 441 /// <typeparam name="TMap">数据源映射类型</typeparam>
  442. 442 /// <param name="db"></param>
  443. 443 /// <param name="whereExp">Linq表达式条件</param>
  444. 444 /// <param name="orderExp">排序表达式</param>
  445. 445 /// <param name="orderType">排序类型</param>
  446. 446 /// <param name="pageIndex">页码(从0开始)</param>
  447. 447 /// <param name="pageSize">每页条数</param>
  448. 448 /// <returns></returns>
  449. 449 public static IPagedList<TMap> GetPageList<TSource, TMap>(this SqlSugarClient db, Expression<Func<TSource, bool>> whereExp, Expression<Func<TSource, object>> orderExp, OrderByType orderType, int pageIndex, int pageSize) where TSource : EntityBase, new()
  450. 450 {
  451. 451 int count = 0;
  452. 452 var result = db.Queryable<TSource>().Where(whereExp).OrderBy(orderExp, orderType).ToPageList(pageIndex, pageSize, ref count);
  453. 453 var pageResult = new PagedList<TSource>(result, pageIndex, pageSize, count);
  454. 454 return pageResult.Map<TSource, TMap>();
  455. 455 }
  456. 456
  457. 457 #endregion
  458. 458
  459. 459 #region 分页查询(Sugar条件)
  460. 460
  461. 461 /// <summary>
  462. 462 /// 获取分页列表【Sugar表达式条件,页码,每页条数】
  463. 463 /// </summary>
  464. 464 /// <typeparam name="TSource">数据源类型</typeparam>
  465. 465 /// <param name="db"></param>
  466. 466 /// <param name="conditionals">Sugar条件表达式集合</param>
  467. 467 /// <param name="pageIndex">页码(从0开始)</param>
  468. 468 /// <param name="pageSize">每页条数</param>
  469. 469 /// <returns></returns>
  470. 470 public static IPagedList<TSource> GetPageList<TSource>(this SqlSugarClient db, List<IConditionalModel> conditionals, int pageIndex, int pageSize) where TSource : EntityBase, new()
  471. 471 {
  472. 472 int count = 0;
  473. 473 var result = db.Queryable<TSource>().Where(conditionals).ToPageList(pageIndex, pageSize, ref count);
  474. 474 return new PagedList<TSource>(result, pageIndex, pageSize, count);
  475. 475 }
  476. 476
  477. 477 /// <summary>
  478. 478 /// 获取分页列表【Sugar表达式条件,页码,每页条数】
  479. 479 /// </summary>
  480. 480 /// <typeparam name="TSource">数据源类型</typeparam>
  481. 481 /// <typeparam name="TMap">数据源映射类型</typeparam>
  482. 482 /// <param name="db"></param>
  483. 483 /// <param name="conditionals">Sugar条件表达式集合</param>
  484. 484 /// <param name="pageIndex">页码(从0开始)</param>
  485. 485 /// <param name="pageSize">每页条数</param>
  486. 486 /// <returns></returns>
  487. 487 public static IPagedList<TMap> GetPageList<TSource, TMap>(this SqlSugarClient db, List<IConditionalModel> conditionals, int pageIndex, int pageSize) where TSource : EntityBase, new()
  488. 488 {
  489. 489 int count = 0;
  490. 490 var result = db.Queryable<TSource>().Where(conditionals).ToPageList(pageIndex, pageSize, ref count);
  491. 491 var pageResult = new PagedList<TSource>(result, pageIndex, pageSize, count);
  492. 492 return pageResult.Map<TSource, TMap>();
  493. 493 }
  494. 494
  495. 495 #endregion
  496. 496
  497. 497 #region 分页查询(Sugar条件,排序)
  498. 498
  499. 499 /// <summary>
  500. 500 /// 获取分页列表【Sugar表达式条件,排序,页码,每页条数】
  501. 501 /// </summary>
  502. 502 /// <typeparam name="TSource"></typeparam>
  503. 503 /// <param name="db"></param>
  504. 504 /// <param name="conditionals">Sugar条件表达式集合</param>
  505. 505 /// <param name="orderExp">排序表达式</param>
  506. 506 /// <param name="orderType">排序类型</param>
  507. 507 /// <param name="pageIndex">页码(从0开始)</param>
  508. 508 /// <param name="pageSize">每页条数</param>
  509. 509 /// <returns></returns>
  510. 510 public static IPagedList<TSource> GetPageList<TSource>(this SqlSugarClient db, List<IConditionalModel> conditionals, Expression<Func<TSource, object>> orderExp, OrderByType orderType, int pageIndex, int pageSize) where TSource : EntityBase, new()
  511. 511 {
  512. 512 int count = 0;
  513. 513 var result = db.Queryable<TSource>().Where(conditionals).OrderBy(orderExp, orderType).ToPageList(pageIndex, pageSize, ref count);
  514. 514 return new PagedList<TSource>(result, pageIndex, pageSize, count);
  515. 515 }
  516. 516
  517. 517 /// <summary>
  518. 518 /// 获取分页列表【Sugar表达式条件,排序,页码,每页条数】
  519. 519 /// </summary>
  520. 520 /// <typeparam name="TSource"></typeparam>
  521. 521 /// <param name="db"></param>
  522. 522 /// <param name="conditionals">Sugar条件表达式集合</param>
  523. 523 /// <param name="orderExp">排序表达式</param>
  524. 524 /// <param name="orderType">排序类型</param>
  525. 525 /// <param name="pageIndex">页码(从0开始)</param>
  526. 526 /// <param name="pageSize">每页条数</param>
  527. 527 /// <returns></returns>
  528. 528 public static IPagedList<TMap> GetPageList<TSource, TMap>(this SqlSugarClient db, List<IConditionalModel> conditionals, Expression<Func<TSource, object>> orderExp, OrderByType orderType, int pageIndex, int pageSize) where TSource : EntityBase, new()
  529. 529 {
  530. 530 int count = 0;
  531. 531 var result = db.Queryable<TSource>().Where(conditionals).OrderBy(orderExp, orderType).ToPageList(pageIndex, pageSize, ref count);
  532. 532 var pageResult = new PagedList<TSource>(result, pageIndex, pageSize, count);
  533. 533 return pageResult.Map<TSource, TMap>();
  534. 534 }
  535. 535
  536. 536 #endregion
  537. 537
  538. 538 #region 分页查询 (扩展条件构造实体,默认排序列,默认排序方式)
  539. 539 /// <summary>
  540. 540 /// 分页查询 (扩展条件构造实体,默认排序列,默认排序方式)
  541. 541 /// </summary>
  542. 542 /// <typeparam name="TSource"></typeparam>
  543. 543 /// <typeparam name="TMap"></typeparam>
  544. 544 /// <param name="db"></param>
  545. 545 /// <param name="query"></param>
  546. 546 /// <param name="defaultSort"></param>
  547. 547 /// <param name="defaultSortType"></param>
  548. 548 /// <returns></returns>
  549. 549 public static IPagedList<TMap> GetPageList<TSource, TMap>(this SqlSugarClient db, QueryCollection query, Expression<Func<TSource, object>> defaultSort, OrderByType defaultSortType) where TSource : EntityBase, new()
  550. 550 {
  551. 551 int count = 0;
  552. 552 List<IConditionalModel> conditionals = query.ConditionItems.ExamineConditional<TSource>();
  553. 553 Expression<Func<TSource, object>> sort = query.SortLambda<TSource, object>(defaultSort, defaultSortType, out var sortType);
  554. 554 var result = db.Queryable<TSource>().Where(conditionals).OrderBy(sort, sortType).ToPageList(query.PageIndex, query.PageSize, ref count);
  555. 555 var pageResult = new PagedList<TSource>(result, query.PageIndex, query.PageSize, count);
  556. 556 return pageResult.Map<TSource, TMap>();
  557. 557 }
  558. 558 #endregion
  559. 559
  560. 560 #region 分页查询 (扩展条件构造实体,默认排序列,默认排序方式,Linq表达式条件)
  561. 561 /// <summary>
  562. 562 /// 分页查询 (扩展条件构造实体,默认排序列,默认排序方式,Linq表达式条件)
  563. 563 /// </summary>
  564. 564 /// <typeparam name="TSource"></typeparam>
  565. 565 /// <typeparam name="TMap"></typeparam>
  566. 566 /// <param name="db"></param>
  567. 567 /// <param name="query"></param>
  568. 568 /// <param name="defaultSort"></param>
  569. 569 /// <param name="defaultSortType"></param>
  570. 570 /// <param name="whereExp"></param>
  571. 571 /// <returns></returns>
  572. 572 public static IPagedList<TMap> GetPageList<TSource, TMap>(this SqlSugarClient db, QueryCollection query, Expression<Func<TSource, object>> defaultSort, OrderByType defaultSortType, Expression<Func<TSource, bool>> whereExp) where TSource : EntityBase, new()
  573. 573 {
  574. 574 int count = 0;
  575. 575 List<IConditionalModel> conditionals = query.ConditionItems.ExamineConditional<TSource>();
  576. 576 Expression<Func<TSource, object>> sort = query.SortLambda<TSource, object>(defaultSort, defaultSortType, out var sortType);
  577. 577 var result = db.Queryable<TSource>().Where(whereExp).Where(conditionals).OrderBy(sort, sortType).ToPageList(query.PageIndex, query.PageSize, ref count);
  578. 578 var pageResult = new PagedList<TSource>(result, query.PageIndex, query.PageSize, count);
  579. 579 return pageResult.Map<TSource, TMap>();
  580. 580 }
  581. 581 #endregion
  582. 582 }
SugarFactoryExtensions,自行斟酌,喜欢使用sugar原生的,可跳过

使用:

1             services.AddSqlSugarClient<DbFactory>((sp, op) =>
2             {
3                 op.ConnectionString = sp.GetService<IConfiguration>().GetConnectionString("lucy");
4                 op.DbType = DbType.MySql;
5                 op.IsAutoCloseConnection = true;
6                 op.InitKeyType = InitKeyType.Attribute;
7                 op.IsShardSameThread = true;
8             });
注入
 1     //如果数据操作简单,直接在业务层使用
 2     public class UsersService : Repository<DbFactory, IUsersRepository>, IUsersService
 3     {
 4         public UsersService(DbFactory factory, IUsersRepository) : base(factory)
 5         {
 6 
 7         }
 8 
 9 
10         public async Task TestMethod()
11         {
12             //获取数据库上下文
13             //第一种  
14             DbContext.Insert<Users>(new Users());
15             //第二种
16             using (var db = Factory.GetDbContext())
17             {
18                 db.Insert<Users>(new Users());
19                 db.Update<Users>(new Users());
20             }
21             //数据操作繁琐的放到自定义的IUsersRepository中
22             await DbRepository.TestAddAsync();
23         }
24 
25     }
业务层使用示例
 1  public class UsersRepository : Repository<DbFactory>, IUsersRepository
 2     {
 3         public UsersRepository(DbFactory factory) : base(factory)
 4         {
 5 
 6         }
 7 
 8         public async Task<bool> TestAddAsync()
 9         {
10             //这里获取数据库上下文,与业务层一致
11 
12             DbContext.Insert<Users>(new Users());
13 
14             using (var db = Factory.GetDbContext())
15             {
16                 db.Insert<Users>(new Users());
17                 db.Update<Users>(new Users());
18             }
19             return await Task.FromResult(true);
20         }
21     }
仓储层(可省略,数据操作繁琐可以放这一层,简单的可以直接在业务层使用)

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号