完整框架项目源码地址:https://download.csdn.net/download/yigu4011/87788956?spm=1001.2014.3001.5503
仓储(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 数据库连接字符串
public class BaseDBConfig
{
///
/// 数据库连接字符串
///
public static string ConnectionString { get; set; }
}
新建DBConext.cs帮助类
public class DbContext where T : class, new()
{
public DbContext()
{
Db = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = BaseDBConfig.ConnectionString,
DbType = DbType.SqlServer,
InitKeyType = InitKeyType.Attribute,//从特性读取主键和自增列信息
IsAutoCloseConnection = true,//开启自动释放模式和EF原理一样我就不多解释了
});
//调式代码 用来打印SQL
Db.Aop.OnLogExecuting = (sql, pars) =>
{
Console.WriteLine(sql + "\r\n" +
Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
Console.WriteLine();
};
}
//注意:不能写成静态的
public SqlSugarClient Db;//用来处理事务多表查询和复杂的操作
public SimpleClient CurrentDb { get { return new SimpleClient(Db); } }//用来操作当前表的数据
}
在program.cs里面增加读取appsettings.json中保存的数据库连接字符串
//数据库配置
BaseDBConfig.ConnectionString = builder.Configuration.GetSection("AppSettings:ConnectionString").Value;
IRepository 层中添加Base文件夹,并添加基类接口 IBaseRepository.cs
///
/// 基类接口,其他接口继承该接口
///
///
public interface IBaseRepository where TEntity : class
{
///
/// 根据ID列表查询
///
///
///
Task QueryByID(object objId);
///
/// 查询所有数据
///
///
///
Task> Query();
///
/// 添加
///
///
///
Task Add(TEntity model);
///
/// 修改
///
///
///
Task Update(TEntity model);
///
/// 根据id列表删除
///
///
///
Task DeleteByIds(object[] ids);
///
/// 根据ID删除
///
///
///
Task DeleteById(object id);
}
Repository 层中,添加Base文件夹,并添加 BaseRepository.cs 基类
public class BaseRepository : DbContext, IBaseRepository where TEntity : class, new()
{
///
/// 写入实体数据
///
///
///
public async Task Add(TEntity model)
{
var i = await Task.Run(() => Db.Insertable(model).ExecuteCommand());
//返回的i是long类型,这里你可以根据你的业务需要进行处理
return i;
}
///
/// 根据ID列表删除
///
///
///
public async Task DeleteByIds(object[] ids)
{
var i = await Task.Run(() => Db.Deleteable().In(ids).ExecuteCommand());
return i > 0;
}
///
/// 根据ID删除
///
///
///
public async Task DeleteById(object id)
{
var i = await Task.Run(() => Db.Deleteable().In(id).ExecuteCommand());
return i > 0;
}
///
/// 根据ID查询一条数据
///
///
///
public async Task QueryByID(object objId)
{
return await Task.Run(() => Db.Queryable().InSingle(objId));
}
///
/// 查询所有数据
///
///
///
public async Task> Query()
{
return await Task.Run(() => Db.Queryable().ToListAsync());
}
///
/// 更新实体数据
///
///
///
public async Task Update(TEntity model)
{
//这种方式会以主键为条件
var i = await Task.Run(() => Db.Updateable(model).ExecuteCommand());
return i > 0;
}
}
IService 层中添加Base文件夹,并添加接口 IBaseService.cs。
public interface IBaseServices where TEntity : class
{
///
/// 根据ID列表删除
///
///
///
Task DeleteByIds(object[] ids);
///
/// 根据ID删除
///
///
///
Task DeleteById(string id);
///
/// 根据ID查询
///
///
///
Task QueryByID(object objId);
///
/// 查询所有数据
///
///
Task> Query();
///
/// 添加实体
///
///
///
Task Add(TEntity model);
///
/// 更新实体
///
///
///
Task Update(TEntity model);
}
Service 层中添加Base文件夹,并添加接口 BaseService.cs。
public class BaseServices : IBaseServices where TEntity : class, new()
{
public IBaseRepository baseDal = new BaseRepository();
///
/// 写入实体
///
///
///
public async Task Add(TEntity model)
{
return await baseDal.Add(model);
}
///
/// 根据ID删除
///
///
///
public async Task DeleteByIds(object[] ids)
{
return await baseDal.DeleteByIds(ids);
}
///
/// 根据ID查询
///
///
///
public async Task QueryByID(object objId)
{
return await baseDal.QueryByID(objId);
}
///
/// 更新实体
///
///
///
public async Task Update(TEntity model)
{
return await baseDal.Update(model);
}
///
/// 查询所有数据
///
///
public async Task> Query()
{
return await baseDal.Query();
}
///
/// 根据ID删除
///
///
///
public async Task DeleteById(string id)
{
return await baseDal.DeleteById(id);
}
}
在Model层下的Entity文件,新建student.cs实体类
///
/// 学生表
///
public class Student
{
///
/// id
///
public int Id { get; set; }
///
/// 姓名
///
public string Name { get; set; }
///
/// 年龄
///
public int Age { get; set; }
}
在IRepository层中,新建IStudentRepository接口,继承自IBaseRepository
public interface IStudentRepository:IBaseRepository
{
}
在Repository层中,新建StudentRepository接口,继承自BaseRepository
public class StudentRepository : BaseRepository, IStudentRepository
{
}
在IServices层中,新建IStudentService接口,继承自IBaseServices
public interface IStudentService : IBaseServices
{
}
在Services层中,新建StudentService接口,继承自BaseService
public class StudentService : BaseService, IStudentService
{
}
新建控制器StudentController,新增接口
public class StudentController : BaseApiController
{
///
/// 添加数据
///
///
[HttpGet]
public async Task Add(Student student)
{
IStudentService userService = new StudentService();
var count = await userService.Add(student);
return Ok(count);
}
}
运行项目,插入数据成功。