From 42914f873450cfb0c1a351ba18664472ff2ca542 Mon Sep 17 00:00:00 2001 From: xuejiaming <326308290@qq.com> Date: Wed, 28 Jun 2023 21:51:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=88=86=E9=85=8D=E5=92=8C?= =?UTF-8?q?=E6=9C=AA=E5=88=86=E7=89=87=E7=9A=84=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/WeatherForecastController.cs | 41 ++++++++++++- .../DbContexts/UnShardingDbContext.cs | 20 +++++++ .../Domain/Entities/RangeOrder.cs | 8 +++ .../Shardings/RangeOrderTableRoute.cs | 60 +++++++++++++++++++ samples/Sample.MySql/Startup.cs | 15 ++++- 5 files changed, 140 insertions(+), 4 deletions(-) create mode 100644 samples/Sample.MySql/DbContexts/UnShardingDbContext.cs create mode 100644 samples/Sample.MySql/Domain/Entities/RangeOrder.cs create mode 100644 samples/Sample.MySql/Shardings/RangeOrderTableRoute.cs diff --git a/samples/Sample.MySql/Controllers/WeatherForecastController.cs b/samples/Sample.MySql/Controllers/WeatherForecastController.cs index e91b00a0..91f60b64 100644 --- a/samples/Sample.MySql/Controllers/WeatherForecastController.cs +++ b/samples/Sample.MySql/Controllers/WeatherForecastController.cs @@ -61,13 +61,14 @@ namespace Sample.MySql.Controllers [Route("[controller]/[action]")] public class WeatherForecastController : ControllerBase { - + private readonly UnShardingDbContext _unShardingDbContext; private readonly DefaultShardingDbContext _defaultTableDbContext; private readonly IShardingRuntimeContext _shardingRuntimeContext; private readonly ABC _abc; - public WeatherForecastController(DefaultShardingDbContext defaultTableDbContext,IShardingRuntimeContext shardingRuntimeContext) + public WeatherForecastController(UnShardingDbContext unShardingDbContext,DefaultShardingDbContext defaultTableDbContext,IShardingRuntimeContext shardingRuntimeContext) { + _unShardingDbContext = unShardingDbContext; _defaultTableDbContext = defaultTableDbContext; _shardingRuntimeContext = shardingRuntimeContext; _abc=new ABC(_defaultTableDbContext); @@ -383,5 +384,41 @@ namespace Sample.MySql.Controllers }).ToListAsync(); return Ok(); } + + + [HttpGet] + public async Task Get14() + { + var sysUserLogByMonths = new List(); + for (int i = 0; i < 100; i++) + { + var sysUserLogByMonth = new SysUserLogByMonth(); + sysUserLogByMonth.Id = Guid.NewGuid().ToString("n"); + sysUserLogByMonth.Time=DateTime.Now; + sysUserLogByMonths.Add(sysUserLogByMonth); + } + + await _defaultTableDbContext.AddRangeAsync(sysUserLogByMonths); + await _defaultTableDbContext.SaveChangesAsync(); + return Ok(); + } + + + [HttpGet] + public async Task Get15() + { + var sysUserLogByMonths = new List(); + for (int i = 0; i < 100; i++) + { + var sysUserLogByMonth = new SysUserLogByMonth(); + sysUserLogByMonth.Id = Guid.NewGuid().ToString("n"); + sysUserLogByMonth.Time=DateTime.Now; + sysUserLogByMonths.Add(sysUserLogByMonth); + } + + await _unShardingDbContext.AddRangeAsync(sysUserLogByMonths); + await _unShardingDbContext.SaveChangesAsync(); + return Ok(); + } } } diff --git a/samples/Sample.MySql/DbContexts/UnShardingDbContext.cs b/samples/Sample.MySql/DbContexts/UnShardingDbContext.cs new file mode 100644 index 00000000..353b0ac3 --- /dev/null +++ b/samples/Sample.MySql/DbContexts/UnShardingDbContext.cs @@ -0,0 +1,20 @@ +using Microsoft.EntityFrameworkCore; +using Sample.MySql.Domain.Maps; + +namespace Sample.MySql.DbContexts +{ + + public class UnShardingDbContext:DbContext + { + public UnShardingDbContext(DbContextOptions options):base(options) + { + + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + modelBuilder.ApplyConfiguration(new SysUserLogByMonthMap()); + } + } +} diff --git a/samples/Sample.MySql/Domain/Entities/RangeOrder.cs b/samples/Sample.MySql/Domain/Entities/RangeOrder.cs new file mode 100644 index 00000000..7c4a82f8 --- /dev/null +++ b/samples/Sample.MySql/Domain/Entities/RangeOrder.cs @@ -0,0 +1,8 @@ +namespace Sample.MySql.Domain.Entities +{ + public class RangeOrder + { + public long OrderId { get; set; } + public string OrderNo { get; set; } + } +} \ No newline at end of file diff --git a/samples/Sample.MySql/Shardings/RangeOrderTableRoute.cs b/samples/Sample.MySql/Shardings/RangeOrderTableRoute.cs new file mode 100644 index 00000000..3eba8e1b --- /dev/null +++ b/samples/Sample.MySql/Shardings/RangeOrderTableRoute.cs @@ -0,0 +1,60 @@ +using Sample.MySql.Domain.Entities; +using ShardingCore.Core.EntityMetadatas; +using ShardingCore.Core.VirtualRoutes; +using ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions; + +namespace Sample.MySql.Shardings +{ + public class RangeOrderTableRoute : AbstractShardingOperatorVirtualTableRoute + { + public override string ShardingKeyToTail(object shardingKey) + { + //取商 + var value = ((long)shardingKey) / (1000 * 10000); + return value.ToString().PadLeft(4, '0'); //左补零 range_order_0000 range_order_0001 range_order_0002 + } + + public override List GetTails() + { + //查询拨号器现在是多少位 + long nextId = 0; + var id = nextId / (1000 * 10000); + var tails = new List(); + for (int i = 0; i <= id; i++) + { + var tail = i.ToString().PadLeft(4, '0'); + tails.Add(tail); + } + + return tails; + } + + public override void Configure(EntityMetadataTableBuilder builder) + { + builder.ShardingProperty(o => o.OrderId); + } + + public override Func GetRouteToFilter(long shardingKey, ShardingOperatorEnum shardingOperator) + { + //当前值是对应表后缀多少 + var t = ShardingKeyToTail(shardingKey); + switch (shardingOperator) + { + case ShardingOperatorEnum.GreaterThan: + case ShardingOperatorEnum.GreaterThanOrEqual: + return tail => String.Compare(tail, t, StringComparison.Ordinal) >= 0; + case ShardingOperatorEnum.LessThan: + { + return tail => String.Compare(tail, t, StringComparison.Ordinal) <= 0; + } + case ShardingOperatorEnum.LessThanOrEqual: + return tail => String.Compare(tail, t, StringComparison.Ordinal) <= 0; + case ShardingOperatorEnum.Equal: return tail => tail == t; + default: + { + return tail => true; + } + } + } + } +} \ No newline at end of file diff --git a/samples/Sample.MySql/Startup.cs b/samples/Sample.MySql/Startup.cs index f4c8d500..baa005d5 100644 --- a/samples/Sample.MySql/Startup.cs +++ b/samples/Sample.MySql/Startup.cs @@ -70,7 +70,10 @@ namespace Sample.MySql // }); // - + services.AddDbContext(o => + o.UseMySql( + "server=127.0.0.1;port=3306;database=dbdbdxx;userid=root;password=root;",new MySqlServerVersion( + new Version()))); services.AddShardingDbContext() .UseRouteConfig((sp, o) => { @@ -186,7 +189,15 @@ namespace Sample.MySql app.UseDeveloperExceptionPage(); } app.ApplicationServices.UseAutoTryCompensateTable(); - + using (var scope = app.ApplicationServices.CreateScope()) + { + var unShardingDbContext = scope.ServiceProvider.GetService(); + unShardingDbContext.Database.EnsureCreated(); + // var shardingRuntimeContext = defaultShardingDbContext.GetShardingRuntimeContext(); + // var tableRouteManager = shardingRuntimeContext.GetTableRouteManager(); + // var virtualTableRoute = (SysUserLogByMonthRoute)tableRouteManager.GetRoute(typeof(SysUserLogByMonth)); + // virtualTableRoute.Append("2021"); + } // var shardingRuntimeContext = app.ApplicationServices.GetRequiredService(); // var entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager(); // var entityMetadata = entityMetadataManager.TryGet();