仓储(Respository)是对数据库访问的一个封装
解决方案新建Respository文件夹,新建类库Web.Core.IRepository,Web.Core.Repository
解决方案新建Services文件夹,新建类库Web.Core.IServices,Web.Core.Services
在类库Web.Core.Model下面新建Entity文件夹
SqlSugar是国人开发者开发的一款高性能、轻量级 ORM框架,官网 SqlSugar ORM 5.X 官网 、文档、教程 - SqlSugar 5x - .NET果糖网
Respository层和Model层引入SqlSugarCore
Repository层新建suger文件夹,
新建BaseDBConfig.cs 数据库连接字符串
1 public class BaseDBConfig 2 { 3 /// <summary> 4 /// 数据库连接字符串 5 /// </summary> 6 public static string ConnectionString { get; set; } 7 8 }
新建DBConext.cs帮助类
1 public class DbContext<T> where T : class, new() 2 { 3 public DbContext() 4 { 5 Db = new SqlSugarClient(new ConnectionConfig() 6 { 7 ConnectionString = BaseDBConfig.ConnectionString, 8 DbType = DbType.SqlServer, 9 InitKeyType = InitKeyType.Attribute,//从特性读取主键和自增列信息 10 IsAutoCloseConnection = true,//开启自动释放模式和EF原理一样我就不多解释了 11 12 }); 13 //调式代码 用来打印SQL 14 Db.Aop.OnLogExecuting = (sql, pars) => 15 { 16 Console.WriteLine(sql + "\r\n" + 17 Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value))); 18 Console.WriteLine(); 19 }; 20 21 } 22 //注意:不能写成静态的 23 public SqlSugarClient Db;//用来处理事务多表查询和复杂的操作 24 public SimpleClient<T> CurrentDb { get { return new SimpleClient<T>(Db); } }//用来操作当前表的数据 25 26 27 28 29 }
在program.cs里面增加读取appsettings.json中保存的数据库连接字符串
1 //数据库配置
2 BaseDBConfig.ConnectionString = builder.Configuration.GetSection("AppSettings:ConnectionString").Value;
IRepository 层中添加Base文件夹,并添加基类接口 IBaseRepository.cs
1 /// <summary> 2 /// 基类接口,其他接口继承该接口 3 /// </summary> 4 /// <typeparam name="TEntity"></typeparam> 5 public interface IBaseRepository<TEntity> where TEntity : class 6 { 7 /// <summary> 8 /// 根据ID列表查询 9 /// </summary> 10 /// <param name="objId"></param> 11 /// <returns></returns> 12 Task<TEntity> QueryByID(object objId); 13 14 /// <summary> 15 /// 查询所有数据 16 /// </summary> 17 /// <param name="objId"></param> 18 /// <returns></returns> 19 Task<List<TEntity>> Query(); 20 21 /// <summary> 22 /// 添加 23 /// </summary> 24 /// <param name="model"></param> 25 /// <returns></returns> 26 Task<long> Add(TEntity model); 27 28 /// <summary> 29 /// 修改 30 /// </summary> 31 /// <param name="model"></param> 32 /// <returns></returns> 33 Task<bool> Update(TEntity model); 34 35 /// <summary> 36 /// 根据id列表删除 37 /// </summary> 38 /// <param name="ids"></param> 39 /// <returns></returns> 40 Task<bool> DeleteByIds(object[] ids); 41 42 /// <summary> 43 /// 根据ID删除 44 /// </summary> 45 /// <param name="id"></param> 46 /// <returns></returns> 47 Task<bool> DeleteById(object id); 48 49 }
Repository 层中,添加Base文件夹,并添加 BaseRepository.cs 基类
1 public class BaseRepository<TEntity> : DbContext<TEntity>, IBaseRepository<TEntity> where TEntity : class, new() 2 { 3 /// <summary> 4 /// 写入实体数据 5 /// </summary> 6 /// <param name="model"></param> 7 /// <returns></returns> 8 public async Task<long> Add(TEntity model) 9 { 10 var i = await Task.Run(() => Db.Insertable(model).ExecuteCommand()); 11 //返回的i是long类型,这里你可以根据你的业务需要进行处理 12 return i; 13 } 14 15 /// <summary> 16 /// 根据ID列表删除 17 /// </summary> 18 /// <param name="ids"></param> 19 /// <returns></returns> 20 public async Task<bool> DeleteByIds(object[] ids) 21 { 22 var i = await Task.Run(() => Db.Deleteable<TEntity>().In(ids).ExecuteCommand()); 23 return i > 0; 24 } 25 26 /// <summary> 27 /// 根据ID删除 28 /// </summary> 29 /// <param name="ids"></param> 30 /// <returns></returns> 31 public async Task<bool> DeleteById(object id) 32 { 33 var i = await Task.Run(() => Db.Deleteable<TEntity>().In(id).ExecuteCommand()); 34 return i > 0; 35 } 36 /// <summary> 37 /// 根据ID查询一条数据 38 /// </summary> 39 /// <param name="objId"></param> 40 /// <returns></returns> 41 public async Task<TEntity> QueryByID(object objId) 42 { 43 return await Task.Run(() => Db.Queryable<TEntity>().InSingle(objId)); 44 } 45 46 /// <summary> 47 /// 查询所有数据 48 /// </summary> 49 /// <param name="objId"></param> 50 /// <returns></returns> 51 public async Task<List<TEntity>> Query() 52 { 53 return await Task.Run(() => Db.Queryable<TEntity>().ToListAsync()); 54 } 55 /// <summary> 56 /// 更新实体数据 57 /// </summary> 58 /// <param name="model"></param> 59 /// <returns></returns> 60 public async Task<bool> Update(TEntity model) 61 { 62 //这种方式会以主键为条件 63 var i = await Task.Run(() => Db.Updateable(model).ExecuteCommand()); 64 return i > 0; 65 } 66 }
IService 层中添加Base文件夹,并添加接口 IBaseService.cs。
1 public interface IBaseServices<TEntity> where TEntity : class 2 { 3 /// <summary> 4 /// 根据ID列表删除 5 /// </summary> 6 /// <param name="ids"></param> 7 /// <returns></returns> 8 Task<bool> DeleteByIds(object[] ids); 9 10 /// <summary> 11 /// 根据ID删除 12 /// </summary> 13 /// <param name="id"></param> 14 /// <returns></returns> 15 Task<bool> DeleteById(string id); 16 /// <summary> 17 /// 根据ID查询 18 /// </summary> 19 /// <param name="objId"></param> 20 /// <returns></returns> 21 Task<TEntity> QueryByID(object objId); 22 23 /// <summary> 24 /// 查询所有数据 25 /// </summary> 26 /// <returns></returns> 27 Task<List<TEntity>> Query(); 28 29 /// <summary> 30 /// 添加实体 31 /// </summary> 32 /// <param name="model"></param> 33 /// <returns></returns> 34 Task<long> Add(TEntity model); 35 36 /// <summary> 37 /// 更新实体 38 /// </summary> 39 /// <param name="model"></param> 40 /// <returns></returns> 41 42 Task<bool> Update(TEntity model); 43 }
Service 层中添加Base文件夹,并添加接口 BaseService.cs。
1 public class BaseServices<TEntity> : IBaseServices<TEntity> where TEntity : class, new() 2 { 3 public IBaseRepository<TEntity> baseDal = new BaseRepository<TEntity>(); 4 5 /// <summary> 6 /// 写入实体 7 /// </summary> 8 /// <param name="model"></param> 9 /// <returns></returns> 10 public async Task<long> Add(TEntity model) 11 { 12 return await baseDal.Add(model); 13 } 14 15 /// <summary> 16 /// 根据ID删除 17 /// </summary> 18 /// <param name="ids"></param> 19 /// <returns></returns> 20 21 public async Task<bool> DeleteByIds(object[] ids) 22 { 23 return await baseDal.DeleteByIds(ids); 24 } 25 26 /// <summary> 27 /// 根据ID查询 28 /// </summary> 29 /// <param name="objId"></param> 30 /// <returns></returns> 31 public async Task<TEntity> QueryByID(object objId) 32 { 33 return await baseDal.QueryByID(objId); 34 } 35 36 /// <summary> 37 /// 更新实体 38 /// </summary> 39 /// <param name="model"></param> 40 /// <returns></returns> 41 public async Task<bool> Update(TEntity model) 42 { 43 return await baseDal.Update(model); 44 } 45 46 /// <summary> 47 /// 查询所有数据 48 /// </summary> 49 /// <returns></returns> 50 public async Task<List<TEntity>> Query() 51 { 52 return await baseDal.Query(); 53 } 54 55 /// <summary> 56 /// 根据ID删除 57 /// </summary> 58 /// <param name="ids"></param> 59 /// <returns></returns> 60 public async Task<bool> DeleteById(string id) 61 { 62 return await baseDal.DeleteById(id); 63 } 64 }
在Model层下的Entity文件,新建student.cs实体类
1 /// <summary> 2 /// 学生表 3 /// </summary> 4 public class Student 5 { 6 /// <summary> 7 /// id 8 /// </summary> 9 public int Id { get; set; } 10 /// <summary> 11 /// 姓名 12 /// </summary> 13 public string Name { get; set; } 14 /// <summary> 15 /// 年龄 16 /// </summary> 17 public int Age { get; set; } 18 }
在IRepository层中,新建IStudentRepository接口,继承自IBaseRepository<Student>
public interface IStudentRepository:IBaseRepository<Student>
{
}
在Repository层中,新建StudentRepository接口,继承自BaseRepository<Student>, IStudentRepository
public class StudentRepository : BaseRepository<Student>, IStudentRepository
{
}
在IServices层中,新建IStudentService接口,继承自IBaseServices<Student>
public interface IStudentService : IBaseServices<Student>
{
}
在Services层中,新建StudentService接口,继承自BaseService<Student>, IStudentService
public class StudentService : BaseService<Student>, IStudentService
{
}
新建控制器StudentController,新增接口
1 public class StudentController : BaseApiController 2 { 3 /// <summary> 4 /// 添加数据 5 /// </summary> 6 /// <returns></returns> 7 [HttpGet] 8 public async Task<IActionResult> Add(Student student) 9 { 10 IStudentService userService = new StudentService(); 11 var count = await userService.Add(student); 12 return Ok(count); 13 } 14 }
运行项目,插入数据成功。
标签:WebAPI,Task,return,await,class,导入,async,SqlSuag,public From: https://www.cnblogs.com/leon1128/p/18280611