diff --git a/.idea/.idea.Bunny.WebApi/.idea/dataSources.xml b/.idea/.idea.Bunny.WebApi/.idea/dataSources.xml index 9b7261b..fec695d 100644 --- a/.idea/.idea.Bunny.WebApi/.idea/dataSources.xml +++ b/.idea/.idea.Bunny.WebApi/.idea/dataSources.xml @@ -1,23 +1,18 @@ - - sqlite.xerial + + sqlserver.jb true - org.sqlite.JDBC - jdbc:sqlite:D:\Project\web\Bunny-Cli\template\CSharp\CSharp-Single-EFCore\Bunny.WebApi\Database\bunny.db + com.jetbrains.jdbc.sqlserver.SqlServerDriver + Server=192.168.3.98;Database=BunnyDemo + + + $ProjectFileDir$ - - - file://$APPLICATION_CONFIG_DIR$/jdbc-drivers/Xerial SQLiteJDBC/3.45.1/org/xerial/sqlite-jdbc/3.45.1.0/sqlite-jdbc-3.45.1.0.jar - - - file://$APPLICATION_CONFIG_DIR$/jdbc-drivers/Xerial SQLiteJDBC/3.45.1/org/slf4j/slf4j-api/1.7.36/slf4j-api-1.7.36.jar - - \ No newline at end of file diff --git a/.idea/.idea.Bunny.WebApi/.idea/sqldialects.xml b/.idea/.idea.Bunny.WebApi/.idea/sqldialects.xml new file mode 100644 index 0000000..03ba4ef --- /dev/null +++ b/.idea/.idea.Bunny.WebApi/.idea/sqldialects.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/Bunny.Common/Bunny.Common.csproj b/Bunny.Common/Bunny.Common.csproj index cac58f4..0628694 100644 --- a/Bunny.Common/Bunny.Common.csproj +++ b/Bunny.Common/Bunny.Common.csproj @@ -22,6 +22,7 @@ all + diff --git a/Bunny.Common/Context/Database/EFCoreContext.cs b/Bunny.Common/Context/Database/EFCoreContext.cs index be02b96..61ef95a 100644 --- a/Bunny.Common/Context/Database/EFCoreContext.cs +++ b/Bunny.Common/Context/Database/EFCoreContext.cs @@ -5,6 +5,7 @@ using Bunny.Dao.Entity.System.Menu; using Bunny.Dao.Entity.System.User; using log4net; using Microsoft.EntityFrameworkCore; +using Microsoft.IdentityModel.Tokens; namespace Bunny.Common.Context.Database; @@ -22,8 +23,6 @@ public class EfCoreContext : DbContext public EfCoreContext() { var dataBaseConnection = AppSettings.GetAppConfig("DataBase:DataBaseConnection"); - // const string dataBaseConnection = - // "192.168.3.98;Initial Catalog=BunnyDemo;TrustServerCertificate=True;Persist Security Info=True;Users ID=sa;Password=abc1234."; DbPath = $"Data Source={dataBaseConnection}"; } @@ -46,7 +45,9 @@ public class EfCoreContext : DbContext /// protected override void OnConfiguring(DbContextOptionsBuilder options) { + Log.Info("正在连接数据库。。。"); options.UseSqlServer(DbPath); + Log.Info("连接数据库完成层。。。"); } /// @@ -55,7 +56,7 @@ public class EfCoreContext : DbContext protected override void OnModelCreating(ModelBuilder modelBuilder) { // 为每个表指定 blogging 架构 - modelBuilder.HasDefaultSchema("blogging"); + modelBuilder.HasDefaultSchema("dbo"); // 根据基础类型创建数据库表 foreach (var entityType in modelBuilder.Model.GetEntityTypes()) @@ -116,7 +117,7 @@ public class EfCoreContext : DbContext // 添加操作 case EntityState.Added: { - if (entity.Id == Guid.Empty) entity.Id = Guid.NewGuid(); + if (entity.Id.IsNullOrEmpty()) entity.Id = Guid.NewGuid().ToString(); entity.CreateTime = DateTime.Now; entity.UpdateTime = DateTime.Now; entity.Version = DateTime.Now; diff --git a/Bunny.Common/Context/Database/GetPagedResultAsync.cs b/Bunny.Common/Context/Database/GetPagedResultAsync.cs new file mode 100644 index 0000000..28471e2 --- /dev/null +++ b/Bunny.Common/Context/Database/GetPagedResultAsync.cs @@ -0,0 +1,28 @@ +using Bunny.Dao.Common.Result; +using Microsoft.EntityFrameworkCore; + +namespace Bunny.Common.Context.Database; + +public static class GetPagedResultAsync +{ + public static async Task> GetPagedListAsync(IQueryable source, int page, int limit) + { + var total = await source.CountAsync(); + IEnumerable items = await source.Skip((page - 1) * limit).Take(limit).ToListAsync(); + return new PageResult(page, limit, total, items); + } + + public static async Task> GetPagedListFromSqlAsync(DbContext dbContext, string sql, string table, int page, int limit) + where T : class + { + // 计算SQL查询的总数 + var total = await dbContext.Set().CountAsync(); + + // 执行分页查询 + var paginatedSql = + $"{sql} ORDER BY (SELECT NULL) OFFSET {(page - 1) * limit} ROWS FETCH NEXT {limit} ROWS ONLY"; + IEnumerable items = await dbContext.Set().FromSqlRaw(paginatedSql).ToListAsync(); + + return new PageResult(page, limit, total, items); + } +} \ No newline at end of file diff --git a/Bunny.Dao/Common/Result/Pagination.cs b/Bunny.Dao/Common/Result/Pagination.cs index 6c21d19..4b036e7 100644 --- a/Bunny.Dao/Common/Result/Pagination.cs +++ b/Bunny.Dao/Common/Result/Pagination.cs @@ -1,29 +1,29 @@ namespace Bunny.Dao.Common.Result; -public class PageResult +public class PageResult(int page, int limit, int total, IEnumerable data) { /// /// 当前页 /// - public int PageNo { get; set; } + public int Page { get; set; } = page; /// /// 页容量 /// - public int PageSize { get; set; } + public int Limit { get; set; } = limit; /// /// 总页数 /// - public int Pages { get; set; } + public int Pages { get; set; } = (int)Math.Ceiling(total / (double)limit); /// /// 总条数 /// - public int Total { get; set; } + public int Total { get; set; } = total; /// /// 数据 /// - public List? Data { get; set; } + public IEnumerable? Data { get; set; } = data; } \ No newline at end of file diff --git a/Bunny.Dao/Entity/BaseEntity.cs b/Bunny.Dao/Entity/BaseEntity.cs index b76bf58..2e8537c 100644 --- a/Bunny.Dao/Entity/BaseEntity.cs +++ b/Bunny.Dao/Entity/BaseEntity.cs @@ -9,7 +9,7 @@ public class BaseEntity [Comment("主键")] [Column(Order = 0)] [Key] - public Guid? Id { get; set; } + public string? Id { get; set; } [Comment("创建时间")] public DateTime CreateTime { get; set; } diff --git a/Bunny.Dao/Entity/System/User/Users.cs b/Bunny.Dao/Entity/System/User/Users.cs index a2d6384..028fd4d 100644 --- a/Bunny.Dao/Entity/System/User/Users.cs +++ b/Bunny.Dao/Entity/System/User/Users.cs @@ -4,7 +4,7 @@ using Microsoft.EntityFrameworkCore; namespace Bunny.Dao.Entity.System.User; -[Table("System_Users", Schema = "blogging")] +[Table("System_Users")] [Comment("系统用户表")] public class Users : BaseEntity { diff --git a/Bunny.Service/IService/IUserService.cs b/Bunny.Service/IService/IUserService.cs index b913a63..82b73c8 100644 --- a/Bunny.Service/IService/IUserService.cs +++ b/Bunny.Service/IService/IUserService.cs @@ -51,5 +51,5 @@ public interface IUserService /// /// /// - PageResult QueryPage(int page, int limit); + Task> QueryPage(int page, int limit); } \ No newline at end of file diff --git a/Bunny.Service/IService/Service/RedisOptionService.cs b/Bunny.Service/IService/Service/RedisOptionService.cs index 6cd1f1a..8905700 100644 --- a/Bunny.Service/IService/Service/RedisOptionService.cs +++ b/Bunny.Service/IService/Service/RedisOptionService.cs @@ -60,7 +60,7 @@ public class RedisOptionService : IRedisOptionService { var post = new Users { - Id = Guid.NewGuid(), + Id = Guid.NewGuid().ToString(), Username = "存入JSON内容", Password = "正在存入JSON内容" }; diff --git a/Bunny.Service/IService/Service/UserService.cs b/Bunny.Service/IService/Service/UserService.cs index 0df05fc..cca3da9 100644 --- a/Bunny.Service/IService/Service/UserService.cs +++ b/Bunny.Service/IService/Service/UserService.cs @@ -1,5 +1,4 @@ -using Bunny.Common.Attribute; -using Bunny.Common.Context.Database; +using Bunny.Common.Context.Database; using Bunny.Common.Exception; using Bunny.Common.Utils.Net; using Bunny.Dao.Common.Result; @@ -52,7 +51,7 @@ public class UserService : IUserService /// public void DeleteBlog(string id) { - var blog = new Users { Id = Guid.NewGuid() }; + var blog = new Users { Id = Guid.NewGuid().ToString() }; DbContext.Users.Remove(blog); DbContext.SaveChanges(); } @@ -118,21 +117,9 @@ public class UserService : IUserService /// /// /// - [Cacheable("UserService::Get", "00:00:59")] - public PageResult QueryPage(int page, int limit) + public Task> QueryPage(int page, int limit) { - var items = DbContext.Users.Take(limit).Skip(page).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 - { - PageNo = page, - Total = total, - PageSize = limit, - Pages = pages, - Data = blogs - }; + var pagedListAsync = GetPagedResultAsync.GetPagedListFromSqlAsync(DbContext, "select * from System_Users ", "System_Users", page, limit); + return pagedListAsync; } } \ No newline at end of file diff --git a/Bunny.WebApi/Configuration/AddAutofacConfig.cs b/Bunny.WebApi/Configuration/AddAutofacConfig.cs index 590f998..f7fccef 100644 --- a/Bunny.WebApi/Configuration/AddAutofacConfig.cs +++ b/Bunny.WebApi/Configuration/AddAutofacConfig.cs @@ -11,7 +11,7 @@ public static class AddAutofacConfig public static void BuildContainer(ContainerBuilder builder) { // 异步方式创建数据库 - new EfCoreContext().Database.EnsureCreatedAsync(); + new EfCoreContext().Database.EnsureCreated(); // 如果不在在 Controller 层写构造函数可以打开这个,自动完成注入 var controllerBaseType = typeof(ControllerBase); diff --git a/Bunny.WebApi/Configuration/BaseConfig.cs b/Bunny.WebApi/Configuration/BaseConfig.cs index 95b68bd..481a423 100644 --- a/Bunny.WebApi/Configuration/BaseConfig.cs +++ b/Bunny.WebApi/Configuration/BaseConfig.cs @@ -38,7 +38,7 @@ public class BaseConfig(WebApplicationBuilder builder) // 让控制器实例由容器创建 builder.Services.Replace(ServiceDescriptor.Transient()); // 添加定时任务 - builder.AddApplicationBackendServices(); + // builder.AddApplicationBackendServices(); // 设置过滤器 builder.AddFilterConfigInitialize(); // 初始化Redis diff --git a/Bunny.WebApi/Controllers/BlogController.cs b/Bunny.WebApi/Controllers/UserController.cs similarity index 92% rename from Bunny.WebApi/Controllers/BlogController.cs rename to Bunny.WebApi/Controllers/UserController.cs index ba76e15..27bc382 100644 --- a/Bunny.WebApi/Controllers/BlogController.cs +++ b/Bunny.WebApi/Controllers/UserController.cs @@ -12,7 +12,7 @@ namespace Bunny.WebApi.Controllers; /// BLog相关接口 /// [Microsoft.AspNetCore.Mvc.Route("/api/[controller]/[action]")] -public class BlogController : ControllerBase +public class UserController : ControllerBase { [Inject] public required IUserService UserService { get; set; } @@ -101,10 +101,9 @@ public class BlogController : ControllerBase /// /// [HttpPost] - public Result> QueryPage(int page = 1, int limit = 10) + public async Task>> QueryPage(int page = 1, int limit = 10) { - var vo = UserService.QueryPage(page, limit); - + var vo = await UserService.QueryPage(page, limit); return Result>.Success(vo); } } \ No newline at end of file