升级 sqlsugar到5.1.4.115 swagger配置,分页查询

master
lxw 5 months ago
parent 90c6c21091
commit 6532da23df

@ -16,7 +16,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="log4net" Version="2.0.15"/> <PackageReference Include="log4net" Version="2.0.15"/>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3"/> <PackageReference Include="Newtonsoft.Json" Version="13.0.3"/>
<PackageReference Include="SqlSugarCore" Version="5.1.4.95"/> <PackageReference Include="SqlSugarCore" Version="5.1.4.115"/>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

@ -1,11 +0,0 @@
namespace Entity.Common;
public interface IPageList<T> : IList<T>
{
int PageIndex { get; }
int PageSize { get; }
int TotalCount { get; }
int TotalPages { get; }
bool HasPreviousPage { get; }
bool HasNextPage { get; }
}

@ -1,99 +0,0 @@
namespace Entity.Common;
/// <summary>
/// 分页组件实体类
/// </summary>
/// <typeparam name="T">泛型实体</typeparam>
[Serializable]
public class PageList<T> : List<T>, IPageList<T>
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="source">数据源</param>
/// <param name="pageIndex">分页索引</param>
/// <param name="pageSize">分页大小</param>
public PageList(IQueryable<T> source, int pageIndex, int pageSize)
{
var total = source.Count();
TotalCount = total;
TotalPages = total / pageSize;
if (total % pageSize > 0)
TotalPages++;
PageSize = pageSize;
PageIndex = pageIndex;
AddRange(source.Skip(pageIndex * pageSize).Take(pageSize).ToList());
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="source">数据源</param>
/// <param name="pageIndex">分页索引</param>
/// <param name="pageSize">分页大小</param>
public PageList(IList<T> source, int pageIndex, int pageSize)
{
TotalCount = source.Count();
TotalPages = TotalCount / pageSize;
if (TotalCount % pageSize > 0)
TotalPages++;
PageSize = pageSize;
PageIndex = pageIndex;
AddRange(source.Skip(pageIndex * pageSize).Take(pageSize).ToList());
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="source">数据源</param>
/// <param name="pageIndex">分页索引</param>
/// <param name="pageSize">分页大小</param>
/// <param name="totalCount">总记录数</param>
public PageList(IEnumerable<T> source, int pageIndex, int pageSize, int totalCount)
{
TotalCount = totalCount;
TotalPages = TotalCount / pageSize;
if (TotalCount % pageSize > 0)
TotalPages++;
PageSize = pageSize;
PageIndex = pageIndex;
AddRange(source);
}
/// <summary>
/// 分页索引
/// </summary>
public int PageIndex { get; }
/// <summary>
/// 分页大小
/// </summary>
public int PageSize { get; private set; }
/// <summary>
/// 总记录数
/// </summary>
public int TotalCount { get; }
/// <summary>
/// 总页数
/// </summary>
public int TotalPages { get; }
/// <summary>
/// 是否有上一页
/// </summary>
public bool HasPreviousPage => PageIndex > 0;
/// <summary>
/// 是否有下一页
/// </summary>
public bool HasNextPage => PageIndex + 1 < TotalPages;
}

@ -6,8 +6,16 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<DocumentationFile>bin\Debug\Entity.xml</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<DocumentationFile>bin\Release\Entity.xml</DocumentationFile>
</PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="SqlSugarCore" Version="5.1.4.95"/> <PackageReference Include="SqlSugarCore" Version="5.1.4.115"/>
</ItemGroup> </ItemGroup>
</Project> </Project>

@ -0,0 +1,20 @@
namespace HybirdFrameworkCore.Entity;
public class IPage<T>
{
public int Total;
public int PageNum;
public int PageSize;
public List<T>? Rows;
public IPage(int total, QueryPageModel page, List<T>? rows)
{
Total = total;
PageNum = page.Page;
PageSize = page.PageSize;
Rows = rows;
}
}

@ -0,0 +1,58 @@
using System.Diagnostics;
using AutoMapper;
namespace HybirdFrameworkCore.Entity
{
public class PageResult<T>
{
/// <summary>
/// 当前页标
/// </summary>
public int PageNum { get; set; }
/// <summary>
/// 每页大小
/// </summary>
public int PageSize { set; get; }
/// <summary>
/// 返回数据
/// </summary>
public List<T>? Rows { get; set; }
/// <summary>
/// 当前页标
/// </summary>
public long ToTal { get; set; }
public PageResult()
{
PageSize = 0;
PageNum = 0;
ToTal = 0;
Rows = new List<T>();
}
public static PageResult<T> ConvertPage<TS>(IPage<TS> page) where TS : class, new()
{
if (page.Total <= 0 || page.Rows == null)
{
return new PageResult<T>();
}
MapperConfiguration configuration = new MapperConfiguration(cfg => cfg.CreateMap<TS, T>());
Debug.Assert(page.Rows != null, "iPage.Rows != null");
List<T> listDest = configuration.CreateMapper().Map<List<TS>, List<T>>(page.Rows);
return new PageResult<T>()
{
PageSize = page.PageSize,
PageNum = page.PageNum,
ToTal = page.Total,
Rows = listDest,
};
}
}
}

@ -6,8 +6,17 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<DocumentationFile>bin\Debug\HybirdFrameworkCore.xml</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<DocumentationFile>bin\Release\HybirdFrameworkCore.xml</DocumentationFile>
</PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Autofac" Version="7.0.1"/> <PackageReference Include="Autofac" Version="7.0.1"/>
<PackageReference Include="AutoMapper" Version="12.0.1" />
<PackageReference Include="log4net" Version="2.0.15"/> <PackageReference Include="log4net" Version="2.0.15"/>
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0"/> <PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0"/>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0"/> <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0"/>

@ -1,5 +1,5 @@
using System.Linq.Expressions; using System.Linq.Expressions;
using Entity.Common; using HybirdFrameworkCore.Entity;
using SqlSugar; using SqlSugar;
namespace Repository; namespace Repository;
@ -88,6 +88,98 @@ public abstract class BaseRepository<T> where T : class, new()
.WithNoLockOrNot(blUseNoLock) .WithNoLockOrNot(blUseNoLock)
.ToList(); .ToList();
} }
/// <summary>
/// 根据条件查询表单数据(分页)
/// </summary>
/// <param name="page"> 页数</param>
/// <param name="pageSize">每页几条数据</param>
/// <param name="blUseNoLock">是否使用WITH(NOLOCK)</param>
/// <returns></returns>
public IPage<T> QueryIPageByCause(QueryPageModel page, Expression<Func<T, bool>> predicate)
{
if (null == predicate) {
return this.QueryIPage(page);
}
int totalCount = 0;
List<T> pageList = DbBaseClient
.Queryable<T>()
.Where(predicate)
.WithNoLockOrNot(false)
.ToPageList(page.Page, page.PageSize, ref totalCount);
return new IPage<T>(totalCount, page, pageList);
}
/// <summary>
/// 根据条件查询表单数据(分页) 异步
/// </summary>
/// <param name="page"></param>
/// <param name="predicate"></param>
/// <returns></returns>
public async Task<IPage<T>> QueryIPageByCauseAsync(QueryPageModel page, Expression<Func<T, bool>> predicate)
{
if (null == predicate)
{
return await this.QueryIPageAsync(page);
}
RefAsync<int> totalCount = 0;
List<T> pageList = await DbBaseClient
.Queryable<T>()
.Where(predicate)
.WithNoLockOrNot(false)
.ToPageListAsync(page.Page, page.PageSize, totalCount);
return new IPage<T>(totalCount, page, pageList);
}
/// <summary>
/// 查询表单所有数据(分页)
/// </summary>
/// <param name="page"> 页数</param>
/// <param name="pageSize">每页几条数据</param>
/// <param name="blUseNoLock">是否使用WITH(NOLOCK)</param>
/// <returns></returns>
public IPage<T> QueryIPage(QueryPageModel page)
{
int totalCount = 0;
//page.Page = page.Page == 0 ? 1 : page.Page;//默认第一页 10条数据
//page.PageSize = page.PageSize == 0 ? 10 : page.PageSize;
List<T> pageList = DbBaseClient
.Queryable<T>()
.WithNoLockOrNot(false)
.ToPageList(page.Page, page.PageSize, ref totalCount);
return new IPage<T>(totalCount, page, pageList);
}
/// <summary>
/// 查询表单所有数据(分页) 异步
/// </summary>
/// <param name="page"></param>
/// <returns></returns>
public async Task<IPage<T>> QueryIPageAsync(QueryPageModel page)
{
RefAsync<int> totalCount = 0;
List<T> pageList = await DbBaseClient
.Queryable<T>()
.WithNoLockOrNot(false)
.ToPageListAsync(page.Page, page.PageSize, totalCount);
return new IPage<T>(totalCount, page, pageList);
}
/// <summary> /// <summary>
/// 根据主值列表查询单条数据 /// 根据主值列表查询单条数据
@ -1005,104 +1097,7 @@ public abstract class BaseRepository<T> where T : class, new()
return await DbBaseClient.Queryable<T>().Where(predicate).WithNoLockOrNot(blUseNoLock).SumAsync(field); return await DbBaseClient.Queryable<T>().Where(predicate).WithNoLockOrNot(blUseNoLock).SumAsync(field);
} }
/// <summary>
/// 根据条件查询分页数据
/// </summary>
/// <param name="predicate"></param>
/// <param name="orderBy"></param>
/// <param name="pageIndex">当前页面索引</param>
/// <param name="pageSize">分布大小</param>
/// <param name="blUseNoLock">是否使用WITH(NOLOCK)</param>
/// <returns></returns>
public IPageList<T> QueryPage(Expression<Func<T, bool>> predicate, string orderBy = "", int pageIndex = 1,
int pageSize = 20, bool blUseNoLock = false)
{
var totalCount = 0;
var page = DbBaseClient
.Queryable<T>()
.OrderByIF(!string.IsNullOrEmpty(orderBy), orderBy)
.WhereIF(predicate != null, predicate)
.WithNoLockOrNot(blUseNoLock)
.ToPageList(pageIndex, pageSize, ref totalCount);
var list = new PageList<T>(page, pageIndex, pageSize, totalCount);
return list;
}
/// <summary>
/// 根据条件查询分页数据
/// </summary>
/// <param name="predicate"></param>
/// <param name="orderBy"></param>
/// <param name="pageIndex">当前页面索引</param>
/// <param name="pageSize">分布大小</param>
/// <param name="blUseNoLock">是否使用WITH(NOLOCK)</param>
/// <returns></returns>
public async Task<IPageList<T>> QueryPageAsync(Expression<Func<T, bool>> predicate, string orderBy = "",
int pageIndex = 1, int pageSize = 20, bool blUseNoLock = false)
{
RefAsync<int> totalCount = 0;
var page = await DbBaseClient
.Queryable<T>()
.OrderByIF(!string.IsNullOrEmpty(orderBy), orderBy)
.WhereIF(predicate != null, predicate)
.WithNoLockOrNot(blUseNoLock)
.ToPageListAsync(pageIndex, pageSize, totalCount);
var list = new PageList<T>(page, pageIndex, pageSize, totalCount);
return list;
}
/// <summary>
/// 根据条件查询分页数据
/// </summary>
/// <param name="predicate">判断集合</param>
/// <param name="orderByType">排序方式</param>
/// <param name="pageIndex">当前页面索引</param>
/// <param name="pageSize">分布大小</param>
/// <param name="orderByExpression"></param>
/// <param name="blUseNoLock">是否使用WITH(NOLOCK)</param>
/// <returns></returns>
public IPageList<T> QueryPage(Expression<Func<T, bool>> predicate,
Expression<Func<T, object>> orderByExpression, OrderByType orderByType, int pageIndex = 1,
int pageSize = 20, bool blUseNoLock = false)
{
var totalCount = 0;
var page = DbBaseClient
.Queryable<T>()
.OrderByIF(orderByExpression != null, orderByExpression, orderByType)
.WhereIF(predicate != null, predicate)
.WithNoLockOrNot(blUseNoLock)
.ToPageList(pageIndex, pageSize, ref totalCount);
var list = new PageList<T>(page, pageIndex, pageSize, totalCount);
return list;
}
/// <summary>
/// 根据条件查询分页数据
/// </summary>
/// <param name="predicate">判断集合</param>
/// <param name="orderByType">排序方式</param>
/// <param name="pageIndex">当前页面索引</param>
/// <param name="pageSize">分布大小</param>
/// <param name="orderByExpression"></param>
/// <param name="blUseNoLock">是否使用WITH(NOLOCK)</param>
/// <returns></returns>
public async Task<IPageList<T>> QueryPageAsync(Expression<Func<T, bool>> predicate,
Expression<Func<T, object>> orderByExpression, OrderByType orderByType, int pageIndex = 1,
int pageSize = 20, bool blUseNoLock = false)
{
RefAsync<int> totalCount = 0;
var page = await DbBaseClient
.Queryable<T>()
.WhereIF(predicate != null, predicate)
.OrderByIF(orderByExpression != null, orderByExpression, orderByType)
.WithNoLockOrNot(blUseNoLock)
.ToPageListAsync(pageIndex, pageSize, totalCount);
var list = new PageList<T>(page, pageIndex, pageSize, totalCount);
return list;
}
/// <summary> /// <summary>
/// 查询-2表查询 /// 查询-2表查询

@ -8,7 +8,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="SqlSugar.IOC" Version="2.0.0"/> <PackageReference Include="SqlSugar.IOC" Version="2.0.0"/>
<PackageReference Include="SqlSugarCore" Version="5.1.4.95"/> <PackageReference Include="SqlSugarCore" Version="5.1.4.115"/>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

@ -1,5 +1,4 @@
using System.Linq.Expressions; using System.Linq.Expressions;
using Entity.Common;
using Repository; using Repository;
using SqlSugar; using SqlSugar;
@ -876,70 +875,7 @@ public class BaseServices<T> where T : class, new()
return await BaseDal.GetSumAsync(predicate, field, blUseNoLock); return await BaseDal.GetSumAsync(predicate, field, blUseNoLock);
} }
/// <summary>
/// 根据条件查询分页数据
/// </summary>
/// <param name="predicate"></param>
/// <param name="orderBy"></param>
/// <param name="pageIndex">当前页面索引</param>
/// <param name="pageSize">分布大小</param>
/// <param name="blUseNoLock">是否使用WITH(NOLOCK)</param>
/// <returns></returns>
public IPageList<T> QueryPage(Expression<Func<T, bool>> predicate, string orderBy = "", int pageIndex = 1,
int pageSize = 20, bool blUseNoLock = false)
{
return BaseDal.QueryPage(predicate, orderBy, pageIndex, pageSize, blUseNoLock);
}
/// <summary>
/// 根据条件查询分页数据
/// </summary>
/// <param name="predicate"></param>
/// <param name="orderBy"></param>
/// <param name="pageIndex">当前页面索引</param>
/// <param name="pageSize">分布大小</param>
/// <param name="blUseNoLock">是否使用WITH(NOLOCK)</param>
/// <returns></returns>
public async Task<IPageList<T>> QueryPageAsync(Expression<Func<T, bool>> predicate, string orderBy = "",
int pageIndex = 1, int pageSize = 20, bool blUseNoLock = false)
{
return await BaseDal.QueryPageAsync(predicate, orderBy, pageIndex, pageSize, blUseNoLock);
}
/// <summary>
/// 根据条件查询分页数据
/// </summary>
/// <param name="predicate">判断集合</param>
/// <param name="orderByType">排序方式</param>
/// <param name="pageIndex">当前页面索引</param>
/// <param name="pageSize">分布大小</param>
/// <param name="orderByExpression"></param>
/// <param name="blUseNoLock">是否使用WITH(NOLOCK)</param>
/// <returns></returns>
public IPageList<T> QueryPage(Expression<Func<T, bool>> predicate,
Expression<Func<T, object>> orderByExpression, OrderByType orderByType, int pageIndex = 1,
int pageSize = 20, bool blUseNoLock = false)
{
return BaseDal.QueryPage(predicate, orderByExpression, orderByType, pageIndex, pageSize, blUseNoLock);
}
/// <summary>
/// 根据条件查询分页数据
/// </summary>
/// <param name="predicate">判断集合</param>
/// <param name="orderByType">排序方式</param>
/// <param name="pageIndex">当前页面索引</param>
/// <param name="pageSize">分布大小</param>
/// <param name="orderByExpression"></param>
/// <param name="blUseNoLock">是否使用WITH(NOLOCK)</param>
/// <returns></returns>
public async Task<IPageList<T>> QueryPageAsync(Expression<Func<T, bool>> predicate,
Expression<Func<T, object>> orderByExpression, OrderByType orderByType, int pageIndex = 1,
int pageSize = 20, bool blUseNoLock = false)
{
return await BaseDal.QueryPageAsync(predicate, orderByExpression, orderByType, pageIndex, pageSize,
blUseNoLock);
}
/// <summary> /// <summary>
/// 查询-多表查询 /// 查询-多表查询

@ -14,7 +14,7 @@
<PackageReference Include="DotNetty.Handlers" Version="0.7.5"/> <PackageReference Include="DotNetty.Handlers" Version="0.7.5"/>
<PackageReference Include="DotNetty.Transport" Version="0.7.5"/> <PackageReference Include="DotNetty.Transport" Version="0.7.5"/>
<PackageReference Include="log4net" Version="2.0.15"/> <PackageReference Include="log4net" Version="2.0.15"/>
<PackageReference Include="SqlSugarCore" Version="5.1.4.95"/> <PackageReference Include="SqlSugarCore" Version="5.1.4.115"/>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

@ -45,12 +45,13 @@ builder.Services.AddCors(options =>
builder.Services.AddSwaggerGen(c => builder.Services.AddSwaggerGen(c =>
{ {
string? basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location); //获取应用程序所在目录 var basePath = Path.GetDirectoryName(AppContext.BaseDirectory);
if (basePath != null) //c.IncludeXmlComments(Path.Combine(basePath, Assembly.GetExecutingAssembly().GetName().Name+".xml"), true);
{ c.IncludeXmlComments(Path.Combine(basePath, "WebStarter.xml"), true);
string xmlPath = Path.Combine(basePath, "WebStarter.xml"); c.IncludeXmlComments(Path.Combine(basePath, "Entity.xml"), true);
c.IncludeXmlComments(xmlPath); c.IncludeXmlComments(Path.Combine(basePath, "HybirdFrameworkCore.xml"), true);
}
}); });
builder.Services.AddControllers().AddJsonOptions(configure => builder.Services.AddControllers().AddJsonOptions(configure =>

@ -6,6 +6,14 @@
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<DocumentationFile>bin\Debug\WebStarter.xml</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<DocumentationFile>bin\Release\WebStarter.xml</DocumentationFile>
</PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Autofac" Version="7.1.0"/> <PackageReference Include="Autofac" Version="7.1.0"/>
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0"/> <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0"/>

@ -12,7 +12,7 @@
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0"/> <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0"/>
<PackageReference Include="log4net" Version="2.0.15"/> <PackageReference Include="log4net" Version="2.0.15"/>
<PackageReference Include="SqlSugar.IOC" Version="2.0.0"/> <PackageReference Include="SqlSugar.IOC" Version="2.0.0"/>
<PackageReference Include="SqlSugarCore" Version="5.1.4.95"/> <PackageReference Include="SqlSugarCore" Version="5.1.4.115"/>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

Loading…
Cancel
Save