添加项目文件。
This commit is contained in:
149
QYZH.InteractiveMagazine.Repository/BaseRepository.cs
Normal file
149
QYZH.InteractiveMagazine.Repository/BaseRepository.cs
Normal file
@ -0,0 +1,149 @@
|
||||
using QYZH.InteractiveMagazine.Models.Dto;
|
||||
using SqlSugar;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace QYZH.InteractiveMagazine.Repository;
|
||||
|
||||
/// <summary>
|
||||
/// 基础仓储实现
|
||||
/// </summary>
|
||||
/// <typeparam name="T">实体类型</typeparam>
|
||||
public class BaseRepository<T> : IBaseRepository<T> where T : class, new()
|
||||
{
|
||||
/// <summary>
|
||||
/// SqlSugar 数据库实例
|
||||
/// </summary>
|
||||
protected SqlSugarClient Db => SqlSugarDbContext.GetDb();
|
||||
|
||||
/// <summary>
|
||||
/// 根据Id获取实体(自动过滤已删除数据)
|
||||
/// </summary>
|
||||
/// <param name="id">主键Id</param>
|
||||
/// <returns>实体对象</returns>
|
||||
public async Task<T?> GetByIdAsync(long id)
|
||||
{
|
||||
return await Db.Queryable<T>().In(id).FirstAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有列表(自动过滤已删除数据)
|
||||
/// </summary>
|
||||
/// <returns>实体列表</returns>
|
||||
public async Task<List<T>> GetListAsync()
|
||||
{
|
||||
return await Db.Queryable<T>().ToListAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据条件获取列表(自动过滤已删除数据)
|
||||
/// </summary>
|
||||
/// <param name="where">查询条件</param>
|
||||
/// <returns>实体列表</returns>
|
||||
public async Task<List<T>> GetListByWhereAsync(Expression<Func<T, bool>> where)
|
||||
{
|
||||
return await Db.Queryable<T>().Where(where).ToListAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 分页查询(自动过滤已删除数据)
|
||||
/// </summary>
|
||||
/// <param name="where">查询条件</param>
|
||||
/// <param name="pageQuery">分页参数</param>
|
||||
/// <returns>分页结果</returns>
|
||||
public async Task<PageListModel<T>> GetPageListAsync(Expression<Func<T, bool>> where, PageQueryModel pageQuery)
|
||||
{
|
||||
RefAsync<int> total = 0;
|
||||
|
||||
var query = Db.Queryable<T>().Where(where);
|
||||
|
||||
// 处理排序
|
||||
if (!string.IsNullOrEmpty(pageQuery.SortField))
|
||||
{
|
||||
var isAsc = string.IsNullOrEmpty(pageQuery.SortOrder) ||
|
||||
pageQuery.SortOrder.ToLower() == "asc";
|
||||
query = isAsc
|
||||
? query.OrderBy($"{pageQuery.SortField} asc")
|
||||
: query.OrderBy($"{pageQuery.SortField} desc");
|
||||
}
|
||||
|
||||
var list = await query.ToPageListAsync(pageQuery.PageIndex, pageQuery.PageSize, total);
|
||||
|
||||
return new PageListModel<T>
|
||||
{
|
||||
PageIndex = pageQuery.PageIndex,
|
||||
PageSize = pageQuery.PageSize,
|
||||
TotalCount = total,
|
||||
List = list
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 插入单条记录
|
||||
/// </summary>
|
||||
/// <param name="entity">实体对象</param>
|
||||
/// <returns>是否成功</returns>
|
||||
public async Task<bool> InsertAsync(T entity)
|
||||
{
|
||||
return await Db.Insertable(entity).ExecuteCommandAsync() > 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 批量插入记录
|
||||
/// </summary>
|
||||
/// <param name="entities">实体列表</param>
|
||||
/// <returns>是否成功</returns>
|
||||
public async Task<bool> InsertRangeAsync(List<T> entities)
|
||||
{
|
||||
return await Db.Insertable(entities).ExecuteCommandAsync() > 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新单条记录
|
||||
/// </summary>
|
||||
/// <param name="entity">实体对象</param>
|
||||
/// <returns>是否成功</returns>
|
||||
public async Task<bool> UpdateAsync(T entity)
|
||||
{
|
||||
return await Db.Updateable(entity).ExecuteCommandAsync() > 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 批量更新记录
|
||||
/// </summary>
|
||||
/// <param name="entities">实体列表</param>
|
||||
/// <returns>是否成功</returns>
|
||||
public async Task<bool> UpdateRangeAsync(List<T> entities)
|
||||
{
|
||||
return await Db.Updateable(entities).ExecuteCommandAsync() > 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据Id删除记录(软删除,设置 IsDeleted = true)
|
||||
/// </summary>
|
||||
/// <param name="id">主键Id</param>
|
||||
/// <returns>是否成功</returns>
|
||||
public async Task<bool> DeleteByIdAsync(long id)
|
||||
{
|
||||
return await Db.Deleteable<T>().In(id).IsLogic().ExecuteCommandAsync() > 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据条件删除记录(软删除,设置 IsDeleted = true)
|
||||
/// </summary>
|
||||
/// <param name="where">删除条件</param>
|
||||
/// <returns>是否成功</returns>
|
||||
public async Task<bool> DeleteByWhereAsync(Expression<Func<T, bool>> where)
|
||||
{
|
||||
return await Db.Deleteable<T>().Where(where).IsLogic().ExecuteCommandAsync() > 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据条件获取记录数(自动过滤已删除数据)
|
||||
/// </summary>
|
||||
/// <param name="where">查询条件</param>
|
||||
/// <returns>记录数</returns>
|
||||
public async Task<int> GetCountAsync(Expression<Func<T, bool>> where)
|
||||
{
|
||||
return await Db.Queryable<T>().Where(where).CountAsync();
|
||||
}
|
||||
}
|
||||
88
QYZH.InteractiveMagazine.Repository/IBaseRepository.cs
Normal file
88
QYZH.InteractiveMagazine.Repository/IBaseRepository.cs
Normal file
@ -0,0 +1,88 @@
|
||||
using QYZH.InteractiveMagazine.Models.Dto;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace QYZH.InteractiveMagazine.Repository;
|
||||
|
||||
/// <summary>
|
||||
/// 基础仓储接口
|
||||
/// </summary>
|
||||
/// <typeparam name="T">实体类型</typeparam>
|
||||
public interface IBaseRepository<T> where T : class, new()
|
||||
{
|
||||
/// <summary>
|
||||
/// 根据Id获取实体
|
||||
/// </summary>
|
||||
/// <param name="id">主键Id</param>
|
||||
/// <returns>实体对象</returns>
|
||||
Task<T?> GetByIdAsync(long id);
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有列表
|
||||
/// </summary>
|
||||
/// <returns>实体列表</returns>
|
||||
Task<List<T>> GetListAsync();
|
||||
|
||||
/// <summary>
|
||||
/// 根据条件获取列表
|
||||
/// </summary>
|
||||
/// <param name="where">查询条件</param>
|
||||
/// <returns>实体列表</returns>
|
||||
Task<List<T>> GetListByWhereAsync(Expression<Func<T, bool>> where);
|
||||
|
||||
/// <summary>
|
||||
/// 分页查询
|
||||
/// </summary>
|
||||
/// <param name="where">查询条件</param>
|
||||
/// <param name="pageQuery">分页参数</param>
|
||||
/// <returns>分页结果</returns>
|
||||
Task<PageListModel<T>> GetPageListAsync(Expression<Func<T, bool>> where, PageQueryModel pageQuery);
|
||||
|
||||
/// <summary>
|
||||
/// 插入单条记录
|
||||
/// </summary>
|
||||
/// <param name="entity">实体对象</param>
|
||||
/// <returns>是否成功</returns>
|
||||
Task<bool> InsertAsync(T entity);
|
||||
|
||||
/// <summary>
|
||||
/// 批量插入记录
|
||||
/// </summary>
|
||||
/// <param name="entities">实体列表</param>
|
||||
/// <returns>是否成功</returns>
|
||||
Task<bool> InsertRangeAsync(List<T> entities);
|
||||
|
||||
/// <summary>
|
||||
/// 更新单条记录
|
||||
/// </summary>
|
||||
/// <param name="entity">实体对象</param>
|
||||
/// <returns>是否成功</returns>
|
||||
Task<bool> UpdateAsync(T entity);
|
||||
|
||||
/// <summary>
|
||||
/// 批量更新记录
|
||||
/// </summary>
|
||||
/// <param name="entities">实体列表</param>
|
||||
/// <returns>是否成功</returns>
|
||||
Task<bool> UpdateRangeAsync(List<T> entities);
|
||||
|
||||
/// <summary>
|
||||
/// 根据Id删除记录(软删除)
|
||||
/// </summary>
|
||||
/// <param name="id">主键Id</param>
|
||||
/// <returns>是否成功</returns>
|
||||
Task<bool> DeleteByIdAsync(long id);
|
||||
|
||||
/// <summary>
|
||||
/// 根据条件删除记录(软删除)
|
||||
/// </summary>
|
||||
/// <param name="where">删除条件</param>
|
||||
/// <returns>是否成功</returns>
|
||||
Task<bool> DeleteByWhereAsync(Expression<Func<T, bool>> where);
|
||||
|
||||
/// <summary>
|
||||
/// 根据条件获取记录数
|
||||
/// </summary>
|
||||
/// <param name="where">查询条件</param>
|
||||
/// <returns>记录数</returns>
|
||||
Task<int> GetCountAsync(Expression<Func<T, bool>> where);
|
||||
}
|
||||
@ -0,0 +1,20 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\QYZH.InteractiveMagazine.Common\QYZH.InteractiveMagazine.Common.csproj" />
|
||||
<ProjectReference Include="..\QYZH.InteractiveMagazine.Models\QYZH.InteractiveMagazine.Models.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
|
||||
<PackageReference Include="MySqlConnector" Version="2.5.0" />
|
||||
<PackageReference Include="SqlSugar" Version="5.1.4.207" />
|
||||
</ItemGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
99
QYZH.InteractiveMagazine.Repository/SqlSugarDbContext.cs
Normal file
99
QYZH.InteractiveMagazine.Repository/SqlSugarDbContext.cs
Normal file
@ -0,0 +1,99 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using QYZH.InteractiveMagazine.Models.Entity;
|
||||
using SqlSugar;
|
||||
|
||||
namespace QYZH.InteractiveMagazine.Repository;
|
||||
|
||||
/// <summary>
|
||||
/// SqlSugar 数据库上下文封装(静态类)
|
||||
/// </summary>
|
||||
public static class SqlSugarDbContext
|
||||
{
|
||||
/// <summary>
|
||||
/// SqlSugarClient 实例
|
||||
/// </summary>
|
||||
private static SqlSugarClient? _db;
|
||||
|
||||
/// <summary>
|
||||
/// 配置对象
|
||||
/// </summary>
|
||||
private static IConfiguration? _configuration;
|
||||
|
||||
/// <summary>
|
||||
/// 初始化数据库上下文(在应用启动时调用一次)
|
||||
/// </summary>
|
||||
/// <param name="configuration">配置对象</param>
|
||||
public static void Init(IConfiguration configuration)
|
||||
{
|
||||
_configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取 SqlSugarClient 实例
|
||||
/// </summary>
|
||||
/// <returns>SqlSugarClient 实例</returns>
|
||||
public static SqlSugarClient GetDb()
|
||||
{
|
||||
if (_configuration == null)
|
||||
{
|
||||
throw new InvalidOperationException("请先调用 SqlSugarDbContext.Init(configuration) 进行初始化");
|
||||
}
|
||||
|
||||
if (_db == null)
|
||||
{
|
||||
lock (typeof(SqlSugarDbContext))
|
||||
{
|
||||
if (_db == null)
|
||||
{
|
||||
var connectionString = _configuration.GetConnectionString("DefaultConnection");
|
||||
if (string.IsNullOrWhiteSpace(connectionString))
|
||||
{
|
||||
throw new InvalidOperationException("未找到连接字符串 DefaultConnection");
|
||||
}
|
||||
|
||||
_db = new SqlSugarClient(new ConnectionConfig
|
||||
{
|
||||
ConnectionString = connectionString,
|
||||
DbType = DbType.MySql,
|
||||
IsAutoCloseConnection = true,
|
||||
InitKeyType = InitKeyType.Attribute
|
||||
},
|
||||
db =>
|
||||
{
|
||||
// 配置软删除全局过滤(继承 BaseEntity 的实体都有效)
|
||||
db.QueryFilter.AddTableFilter<BaseEntity>(it => it.IsDeleted == false);
|
||||
|
||||
// 开启日志打印
|
||||
db.Aop.OnLogExecuting = (sql, pars) =>
|
||||
{
|
||||
Console.WriteLine($"[SQL执行] {DateTime.Now:yyyy-MM-dd HH:mm:ss}");
|
||||
Console.WriteLine($"[SQL语句] {sql}");
|
||||
Console.WriteLine($"[SQL参数] {string.Join(", ", pars.Select(p => $"{p.ParameterName}={p.Value}"))}");
|
||||
Console.WriteLine(new string('-', 50));
|
||||
};
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return _db;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 代码优先初始化(可选)
|
||||
/// </summary>
|
||||
/// <param name="entityTypes">实体类型数组</param>
|
||||
public static void InitializeCodeFirst(params Type[] entityTypes)
|
||||
{
|
||||
var db = GetDb();
|
||||
|
||||
// 创建数据库(如果不存在)
|
||||
db.DbMaintenance.CreateDatabase();
|
||||
|
||||
// 初始化表结构
|
||||
if (entityTypes != null && entityTypes.Length > 0)
|
||||
{
|
||||
db.CodeFirst.InitTables(entityTypes);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user