🚀 feat(新增): EfCore所以CURD包含事务、分页查询全部完成
This commit is contained in:
parent
8e3c07a360
commit
08762564cc
|
@ -1,6 +1,6 @@
|
||||||
namespace Bunny.Dao.Result.Page;
|
namespace Bunny.Dao.Result;
|
||||||
|
|
||||||
public class Pagination<T>
|
public class PageResult<T>
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 当前页
|
/// 当前页
|
||||||
|
@ -25,5 +25,5 @@ public class Pagination<T>
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 数据
|
/// 数据
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public IList<T> Rows { get; set; }
|
public List<T>? Data { get; set; }
|
||||||
}
|
}
|
|
@ -1,4 +1,5 @@
|
||||||
using Bunny.Dao.Models.System;
|
using Bunny.Dao.Models.System;
|
||||||
|
using Bunny.Dao.Result;
|
||||||
|
|
||||||
namespace Bunny.Service.IService;
|
namespace Bunny.Service.IService;
|
||||||
|
|
||||||
|
@ -27,4 +28,27 @@ public interface IBlogService
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="id"></param>
|
/// <param name="id"></param>
|
||||||
void DeleteBlog(string id);
|
void DeleteBlog(string id);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 批量添加数据
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="url"></param>
|
||||||
|
void AddBatchBlogs(string url);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 批量删除BLog
|
||||||
|
/// </summary>
|
||||||
|
void DeleteBatchBlogs();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 批量更新_带事务
|
||||||
|
/// </summary>
|
||||||
|
void UseTransaction();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 分页查询
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="page"></param>
|
||||||
|
/// <param name="limit"></param>
|
||||||
|
PageResult<Blog> QueryPage(int page, int limit);
|
||||||
}
|
}
|
|
@ -1,5 +1,6 @@
|
||||||
using Bunny.Common.Connect;
|
using Bunny.Common.Connect;
|
||||||
using Bunny.Dao.Models.System;
|
using Bunny.Dao.Models.System;
|
||||||
|
using Bunny.Dao.Result;
|
||||||
|
|
||||||
namespace Bunny.Service.IService.Service;
|
namespace Bunny.Service.IService.Service;
|
||||||
|
|
||||||
|
@ -52,4 +53,90 @@ public class BlogService : IBlogService
|
||||||
_dbContext.Blogs.Remove(blog);
|
_dbContext.Blogs.Remove(blog);
|
||||||
_dbContext.SaveChanges();
|
_dbContext.SaveChanges();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 批量添加数据
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="url"></param>
|
||||||
|
public void AddBatchBlogs(string url)
|
||||||
|
{
|
||||||
|
var list = new List<Blog>();
|
||||||
|
for (var i = 0; i <= 100000; i++)
|
||||||
|
list.Add(new Blog
|
||||||
|
{
|
||||||
|
Id = $"{i}",
|
||||||
|
Url = url,
|
||||||
|
CreateTime = DateTime.Now,
|
||||||
|
UpdateTime = DateTime.Now,
|
||||||
|
UpdateUserId = Random.Shared.Next(),
|
||||||
|
CreateUserId = Random.Shared.NextInt64()
|
||||||
|
});
|
||||||
|
|
||||||
|
_dbContext.Blogs.AddRange(list);
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 批量删除BLog
|
||||||
|
/// </summary>
|
||||||
|
public void DeleteBatchBlogs()
|
||||||
|
{
|
||||||
|
var list = new List<Blog>();
|
||||||
|
for (var i = 0; i < 10; i++)
|
||||||
|
{
|
||||||
|
var blog = new Blog { Id = $"{i}" };
|
||||||
|
list.Add(blog);
|
||||||
|
}
|
||||||
|
|
||||||
|
_dbContext.Blogs.RemoveRange(list);
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 批量更新_带事务
|
||||||
|
/// </summary>
|
||||||
|
public void UseTransaction()
|
||||||
|
{
|
||||||
|
// 还可以使用异步的
|
||||||
|
var transaction = _dbContext.Database.BeginTransaction();
|
||||||
|
|
||||||
|
// 执行批量更新操作
|
||||||
|
var list = new List<Blog>();
|
||||||
|
for (var i = 0; i < 10; i++)
|
||||||
|
{
|
||||||
|
var blog = new Blog { Id = $"{i}", Url = "https://learn.microsoft.com/zh-cn/ef/core/saving/transactions" };
|
||||||
|
list.Add(blog);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 更新内容
|
||||||
|
_dbContext.Blogs.UpdateRange(list);
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
|
||||||
|
// 还可以使用异步方式
|
||||||
|
transaction.Commit();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 分页查询
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="page"></param>
|
||||||
|
/// <param name="limit"></param>
|
||||||
|
public PageResult<Blog> QueryPage(int page, int limit)
|
||||||
|
{
|
||||||
|
var items = _dbContext.Blogs.ToList();
|
||||||
|
var total = items.Count;
|
||||||
|
var pages = (int)Math.Ceiling(total / (double)limit);
|
||||||
|
|
||||||
|
var blogs = items.Skip((page - 1) * limit).Take(limit).ToList();
|
||||||
|
|
||||||
|
return new PageResult<Blog>
|
||||||
|
{
|
||||||
|
PageNo = page,
|
||||||
|
Total = total,
|
||||||
|
PageSize = limit,
|
||||||
|
Pages = pages,
|
||||||
|
Data = blogs
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -8,8 +8,7 @@ namespace Bunny.WebApi.Controllers;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// BLog相关接口
|
/// BLog相关接口
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[ApiController]
|
[Route("/api/[controller]/[action]")]
|
||||||
[Route("/api/[controller]")]
|
|
||||||
public class BlogController : ControllerBase
|
public class BlogController : ControllerBase
|
||||||
{
|
{
|
||||||
private readonly IBlogService _blogService;
|
private readonly IBlogService _blogService;
|
||||||
|
@ -24,7 +23,7 @@ public class BlogController : ControllerBase
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="dto"></param>
|
/// <param name="dto"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpPost("AddBlog")]
|
[HttpPost]
|
||||||
public Result<string> AddBlog(Blog dto)
|
public Result<string> AddBlog(Blog dto)
|
||||||
{
|
{
|
||||||
_blogService.AddBlog(dto);
|
_blogService.AddBlog(dto);
|
||||||
|
@ -35,7 +34,7 @@ public class BlogController : ControllerBase
|
||||||
/// 查询BLog
|
/// 查询BLog
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpGet("QueryBlog")]
|
[HttpGet]
|
||||||
public Result<List<Blog>> QueryBlog()
|
public Result<List<Blog>> QueryBlog()
|
||||||
{
|
{
|
||||||
var vo = _blogService.QueryBlog();
|
var vo = _blogService.QueryBlog();
|
||||||
|
@ -47,7 +46,7 @@ public class BlogController : ControllerBase
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="dto"></param>
|
/// <param name="dto"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpPost("UpdateBlog")]
|
[HttpPost]
|
||||||
public Result<string> UpdateBlog(Blog dto)
|
public Result<string> UpdateBlog(Blog dto)
|
||||||
{
|
{
|
||||||
_blogService.UpdateBlog(dto);
|
_blogService.UpdateBlog(dto);
|
||||||
|
@ -58,10 +57,55 @@ public class BlogController : ControllerBase
|
||||||
/// 删除BLog
|
/// 删除BLog
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpDelete("DeleteBlog")]
|
[HttpDelete]
|
||||||
public Result<string> DeleteBlog(string id)
|
public Result<string> DeleteBlog(string id)
|
||||||
{
|
{
|
||||||
_blogService.DeleteBlog(id);
|
_blogService.DeleteBlog(id);
|
||||||
return Result<string>.Success();
|
return Result<string>.Success();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 批量添加数据
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpPost]
|
||||||
|
public Result<string> AddBatchBlogs(string url)
|
||||||
|
{
|
||||||
|
_blogService.AddBatchBlogs(url);
|
||||||
|
return Result<string>.Success();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 批量删除BLog
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpGet]
|
||||||
|
public Result<string> DeleteBatchBlogs()
|
||||||
|
{
|
||||||
|
_blogService.DeleteBatchBlogs();
|
||||||
|
return Result<string>.Success();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 批量更新_带事务
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpGet]
|
||||||
|
public Result<string> UseTransaction()
|
||||||
|
{
|
||||||
|
_blogService.UseTransaction();
|
||||||
|
return Result<string>.Success();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 分页查询
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpPost]
|
||||||
|
public Result<PageResult<Blog>> QueryPage(int page = 1, int limit = 10)
|
||||||
|
{
|
||||||
|
var vo = _blogService.QueryPage(page, limit);
|
||||||
|
|
||||||
|
return Result<PageResult<Blog>>.Success(vo);
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -23,7 +23,7 @@ public class RedisOptionController : ControllerBase
|
||||||
/// <param name="key"></param>
|
/// <param name="key"></param>
|
||||||
/// <param name="value"></param>
|
/// <param name="value"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpPost("AddStringValue")]
|
[HttpPost]
|
||||||
public Result<string> AddStringValue(string key, string value)
|
public Result<string> AddStringValue(string key, string value)
|
||||||
{
|
{
|
||||||
_redisOptionService.AddStringValue(key, value);
|
_redisOptionService.AddStringValue(key, value);
|
||||||
|
@ -34,7 +34,7 @@ public class RedisOptionController : ControllerBase
|
||||||
/// 查询字符串Key
|
/// 查询字符串Key
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpPost("QueryStringKey")]
|
[HttpPost]
|
||||||
public Result<string> QueryStringKey(string key)
|
public Result<string> QueryStringKey(string key)
|
||||||
{
|
{
|
||||||
var queryStringKey = _redisOptionService.QueryStringKey(key);
|
var queryStringKey = _redisOptionService.QueryStringKey(key);
|
||||||
|
@ -47,7 +47,7 @@ public class RedisOptionController : ControllerBase
|
||||||
/// <param name="key"></param>
|
/// <param name="key"></param>
|
||||||
/// <param name="value"></param>
|
/// <param name="value"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpPost("AddTimeRedisKey")]
|
[HttpPost]
|
||||||
public Result<string> AddTimeRedisKey(string key, string value)
|
public Result<string> AddTimeRedisKey(string key, string value)
|
||||||
{
|
{
|
||||||
_redisOptionService.AddTimeRedisKey(key, value);
|
_redisOptionService.AddTimeRedisKey(key, value);
|
||||||
|
@ -71,7 +71,7 @@ public class RedisOptionController : ControllerBase
|
||||||
/// Redis存入JSON内容
|
/// Redis存入JSON内容
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpGet("AddJson")]
|
[HttpGet]
|
||||||
public Result<string> AddJson()
|
public Result<string> AddJson()
|
||||||
{
|
{
|
||||||
var json = _redisOptionService.AddJson();
|
var json = _redisOptionService.AddJson();
|
||||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue