当前位置:   article > 正文

sqlsugar的增删改查及封装_sqlsugar 查询

sqlsugar 查询

 最基本的增删改查

           查询(Select)
  1. // 查询单个实体对象
  2. var result = db.Queryable<User>().Where(u => u.Id == 1).Single();
  3. // 查询多个实体对象
  4. var userList = db.Queryable<User>().Where(u => u.Age > 18).ToList();
  5. // 使用LINQ进行查询
  6. var query = from u in db.Queryable<User>()
  7. where u.Age > 18
  8. select u;
  9. var userList = query.ToList();
          插入(Insert)
  1. // 单条插入
  2. var user = new User
  3. {
  4. Name = "John",
  5. Age = 25,
  6. Gender = Gender.Male
  7. };
  8. var id = db.Insertable(user).ExecuteReturnIdentity();
  9. // 批量插入
  10. var userList = new List<User>
  11. {
  12. new User { Name = "Alice", Age = 20, Gender = Gender.Female },
  13. new User { Name = "Bob", Age = 22, Gender = Gender.Male }
  14. };
  15. var count = db.Insertable(userList).ExecuteCommand();
        更新(Update)
  1. // 更新单个实体对象
  2. var user = db.Queryable<User>().Where(u => u.Id == 1).Single();
  3. user.Age = 26;
  4. var count = db.Updateable(user).ExecuteCommand();
  5. // 批量更新
  6. var count = db.Updateable<User>().Where(u => u.Age < 18).SetColumns(u => new User { Age = u.Age + 1 }).ExecuteCommand();
         删除(Delete)
  1. // 删除单个实体对象
  2. var user = new User { Id = 1 };
  3. var count = db.Deleteable(user).ExecuteCommand();
  4. // 删除符合条件的数据
  5. var count = db.Deleteable<User>().Where(u => u.Age >= 30).ExecuteCommand();

针对增删改查进行一个封装进项调用

  1. public interface IBaseRespository<T> where T : class
  2. {
  3. Task<int> AddAsync(T obj); //添加
  4. Task<int> DeleteOneAsync(long id); //删除根据ID
  5. Task<long> DeleteAsync(Expression<Func<T, bool>> expression); //删除根据条件进行删除
  6. Task<long> UpdateAsync(T obj); //修改
  7. Task<long> UpdateWhereAsync(Action<T> update,Expression<Func<T, bool>> expression); //批量条件修改
  8. Task<T> GetByIdAsync(long id); //查询单个
  9. ISugarQueryable<T> GetAll(); //查询全部
  10. Task<PageResult<T>> PagingAsync(int pageIndex, int pageSize, Expression<Func<T, bool>> expression); //分页
  11. }
  12. public class PageResult<T>
  13. {
  14. public List<T> Items{ get; set; }
  15. public int TotalCount { get; set; }
  16. public int TotalPages { get; set; }
  17. }
  1. public class BaseRespository<T> : IBaseRespository<T> where T : class, new()
  2. {
  3. public BaseRespository(ISqlSugarClient db)
  4. {
  5. this.db = db;
  6. }
  7. private readonly ISqlSugarClient db;
  8. //添加
  9. public async Task<int> AddAsync(T obj)
  10. {
  11. return await db.Insertable<T>(obj).ExecuteCommandAsync();
  12. }
  13. public async Task<int> DeleteOneAsync(long id)
  14. {
  15. var result = await db.Deleteable<T>().In(id).ExecuteCommandAsync();
  16. return result;
  17. }
  18. //带条件删除
  19. public async Task<long> DeleteAsync(Expression<Func<T, bool>> expression)
  20. {
  21. var deltable = db.Deleteable<T>().Where(expression);
  22. return await deltable.ExecuteCommandAsync();
  23. }
  24. //修改
  25. public async Task<long> UpdateAsync(T obj)
  26. {
  27. return await db.Updateable<T>(obj).ExecuteCommandAsync();
  28. }
  29. //批量修改条件
  30. public async Task<long> UpdateWhereAsync(Action<T> update, Expression<Func<T, bool>> expression)
  31. {
  32. var data = await db.Queryable<T>().Where(expression).ToListAsync();
  33. foreach (var item in data)
  34. {
  35. update(item);
  36. }
  37. return await db.Updateable(data).ExecuteCommandAsync();
  38. }
  39. //查询全部
  40. public ISugarQueryable<T> GetAll()
  41. {
  42. return db.Queryable<T>();
  43. }
  44. //查询单个
  45. public async Task<T> GetByIdAsync(long id)
  46. {
  47. return await db.Queryable<T>().InSingleAsync(id);
  48. }
  49. //分页
  50. public async Task<PageResult<T>> PagingAsync(int pageIndex, int pageSize, Expression<Func<T, bool>> expression)
  51. {
  52. var query = db.Queryable<T>().Where(expression);
  53. var total = await query.CountAsync();
  54. var pageTotal = (int)Math.Ceiling(total * 1.0f / pageSize);
  55. return new PageResult<T>
  56. {
  57. Items = query.Skip(pageIndex * pageSize).Take(pageSize).ToList(),
  58. TotalCount = total,
  59. TotalPages = pageTotal,
  60. };
  61. }
  62. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/134867
推荐阅读
  

闽ICP备14008679号