添加自动分表功能
This commit is contained in:
Binary file not shown.
@@ -51,6 +51,18 @@
|
|||||||
<param name="ids"></param>
|
<param name="ids"></param>
|
||||||
<returns></returns>
|
<returns></returns>
|
||||||
</member>
|
</member>
|
||||||
|
<member name="M:Yi.Framework.ApiMicroservice.Controllers.LogController.Add">
|
||||||
|
<summary>
|
||||||
|
自动分表,日志添加
|
||||||
|
</summary>
|
||||||
|
<returns></returns>
|
||||||
|
</member>
|
||||||
|
<member name="M:Yi.Framework.ApiMicroservice.Controllers.LogController.GetList">
|
||||||
|
<summary>
|
||||||
|
查询近20年与21年的日志表
|
||||||
|
</summary>
|
||||||
|
<returns></returns>
|
||||||
|
</member>
|
||||||
<member name="M:Yi.Framework.ApiMicroservice.Controllers.TestController.DbTest">
|
<member name="M:Yi.Framework.ApiMicroservice.Controllers.TestController.DbTest">
|
||||||
<summary>
|
<summary>
|
||||||
仓储上下文对象测试
|
仓储上下文对象测试
|
||||||
@@ -95,7 +107,7 @@
|
|||||||
</member>
|
</member>
|
||||||
<member name="M:Yi.Framework.ApiMicroservice.Controllers.TestController.JobTest">
|
<member name="M:Yi.Framework.ApiMicroservice.Controllers.TestController.JobTest">
|
||||||
<summary>
|
<summary>
|
||||||
启动一个定时任务,每5秒访问一次百度
|
启动一个定时任务
|
||||||
</summary>
|
</summary>
|
||||||
<returns></returns>
|
<returns></returns>
|
||||||
</member>
|
</member>
|
||||||
|
|||||||
@@ -0,0 +1,55 @@
|
|||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Yi.Framework.Common.Models;
|
||||||
|
using Yi.Framework.Interface;
|
||||||
|
using Yi.Framework.Model.Models;
|
||||||
|
using Yi.Framework.Repository;
|
||||||
|
using Yi.Framework.WebCore;
|
||||||
|
using Yi.Framework.WebCore.AttributeExtend;
|
||||||
|
using Yi.Framework.WebCore.AuthorizationPolicy;
|
||||||
|
|
||||||
|
namespace Yi.Framework.ApiMicroservice.Controllers
|
||||||
|
{
|
||||||
|
[ApiController]
|
||||||
|
[Route("api/[controller]/[action]")]
|
||||||
|
public class LogController : ControllerBase
|
||||||
|
{
|
||||||
|
private ILogService _iLogService;
|
||||||
|
//大量日志,将采用自动分表形式,默认1年分一次表
|
||||||
|
public LogController(ILogger<LogEntity> logger, ILogService iLogService)
|
||||||
|
{
|
||||||
|
_iLogService = iLogService;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 自动分表,日志添加
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpPost]
|
||||||
|
public async Task<Result> Add()
|
||||||
|
{
|
||||||
|
Random random = new Random();
|
||||||
|
var logList = new List<LogEntity>() {
|
||||||
|
new LogEntity() { LogCreateTime = Convert.ToDateTime("2019-12-1"), Message = "jack"+random.Next() } ,
|
||||||
|
new LogEntity() { LogCreateTime = Convert.ToDateTime("2022-02-1"), Message = "jack"+random.Next() },
|
||||||
|
new LogEntity() { LogCreateTime = Convert.ToDateTime("2020-02-1"), Message = "jack"+random.Next() },
|
||||||
|
new LogEntity() { LogCreateTime = Convert.ToDateTime("2021-12-1"), Message = "jack"+random.Next() } };
|
||||||
|
return Result.Success().SetData(await _iLogService.AddListTest(logList));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 查询近20年与21年的日志表
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpGet]
|
||||||
|
public async Task<Result> GetList()
|
||||||
|
{
|
||||||
|
return Result.Success().SetData(await _iLogService.GetListTest());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Binary file not shown.
13
Yi.Framework.Net6/Yi.Framework.Interface/ILogService.cs
Normal file
13
Yi.Framework.Net6/Yi.Framework.Interface/ILogService.cs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Yi.Framework.Model.Models;
|
||||||
|
using Yi.Framework.Repository;
|
||||||
|
|
||||||
|
namespace Yi.Framework.Interface
|
||||||
|
{
|
||||||
|
public partial interface ILogService
|
||||||
|
{
|
||||||
|
Task<List<long>> AddListTest(List<LogEntity> logEntities);
|
||||||
|
Task<List<LogEntity>> GetListTest();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Yi.Framework.Model.Models;
|
||||||
|
using Yi.Framework.Repository;
|
||||||
|
|
||||||
|
namespace Yi.Framework.Interface
|
||||||
|
{
|
||||||
|
public partial interface ILogService : IBaseService<LogEntity>
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
17
Yi.Framework.Net6/Yi.Framework.Model/LogEntity.cs
Normal file
17
Yi.Framework.Net6/Yi.Framework.Model/LogEntity.cs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using SqlSugar;
|
||||||
|
namespace Yi.Framework.Model.Models
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 日志表
|
||||||
|
///</summary>
|
||||||
|
[SplitTable(SplitType.Year)]
|
||||||
|
[SugarTable("SplitLog_{year}{month}{day}")]
|
||||||
|
public partial class LogEntity
|
||||||
|
{
|
||||||
|
[SplitField] //分表字段 在插入的时候会根据这个字段插入哪个表,在更新删除的时候用这个字段找出相关表
|
||||||
|
public DateTime? LogCreateTime { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
23
Yi.Framework.Net6/Yi.Framework.Model/Models/LogEntity.cs
Normal file
23
Yi.Framework.Net6/Yi.Framework.Model/Models/LogEntity.cs
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using SqlSugar;
|
||||||
|
namespace Yi.Framework.Model.Models
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 日志表
|
||||||
|
///</summary>
|
||||||
|
public partial class LogEntity:BaseModelEntity
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 租户Id
|
||||||
|
///</summary>
|
||||||
|
[SugarColumn(ColumnName="TenantId" )]
|
||||||
|
public long? TenantId { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 日志信息
|
||||||
|
///</summary>
|
||||||
|
[SugarColumn(ColumnName="Message" )]
|
||||||
|
public string Message { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
23
Yi.Framework.Net6/Yi.Framework.Service/LogService.cs
Normal file
23
Yi.Framework.Net6/Yi.Framework.Service/LogService.cs
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
using SqlSugar;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Yi.Framework.Interface;
|
||||||
|
using Yi.Framework.Model.Models;
|
||||||
|
using Yi.Framework.Repository;
|
||||||
|
|
||||||
|
namespace Yi.Framework.Service
|
||||||
|
{
|
||||||
|
public partial class LogService
|
||||||
|
{
|
||||||
|
public async Task<List<long>> AddListTest(List<LogEntity> logEntities)
|
||||||
|
{
|
||||||
|
return await _repository._Db.Insertable(logEntities).SplitTable().ExecuteReturnSnowflakeIdListAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<List<LogEntity>> GetListTest()
|
||||||
|
{
|
||||||
|
return await _repository._Db.Queryable<LogEntity>().SplitTable(tas => tas.Where(u => u.TableName.Contains("2020") || u.TableName.Contains("2021"))).ToListAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
using SqlSugar;
|
||||||
|
using Yi.Framework.Interface;
|
||||||
|
using Yi.Framework.Model.Models;
|
||||||
|
using Yi.Framework.Repository;
|
||||||
|
|
||||||
|
namespace Yi.Framework.Service
|
||||||
|
{
|
||||||
|
public partial class LogService : BaseService<LogEntity>, ILogService
|
||||||
|
{
|
||||||
|
public LogService(IRepository<LogEntity> repository) : base(repository)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -12,6 +12,9 @@ namespace Yi.Framework.WebCore.MiddlewareExtend
|
|||||||
{
|
{
|
||||||
public static void AddSqlsugarServer(this IServiceCollection services)
|
public static void AddSqlsugarServer(this IServiceCollection services)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
DbType dbType;
|
DbType dbType;
|
||||||
var slavaConFig = new List<SlaveConnectionConfig>();
|
var slavaConFig = new List<SlaveConnectionConfig>();
|
||||||
if (Appsettings.appBool("MutiDB_Enabled"))
|
if (Appsettings.appBool("MutiDB_Enabled"))
|
||||||
@@ -42,7 +45,19 @@ namespace Yi.Framework.WebCore.MiddlewareExtend
|
|||||||
DisableNvarchar = true
|
DisableNvarchar = true
|
||||||
},
|
},
|
||||||
SlaveConnectionConfigs = slavaConFig,
|
SlaveConnectionConfigs = slavaConFig,
|
||||||
|
//设置codefirst非空值判断
|
||||||
|
ConfigureExternalServices = new ConfigureExternalServices
|
||||||
|
{
|
||||||
|
EntityService = (c, p) =>
|
||||||
|
{
|
||||||
|
// int? decimal?这种 isnullable=true
|
||||||
|
if (c.PropertyType.IsGenericType &&
|
||||||
|
c.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
|
||||||
|
{
|
||||||
|
p.IsNullable = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
db =>
|
db =>
|
||||||
{
|
{
|
||||||
@@ -80,6 +95,10 @@ namespace Yi.Framework.WebCore.MiddlewareExtend
|
|||||||
|
|
||||||
Console.WriteLine("_______________________________________________");
|
Console.WriteLine("_______________________________________________");
|
||||||
Console.WriteLine("执行SQL:"+s.ToString());
|
Console.WriteLine("执行SQL:"+s.ToString());
|
||||||
|
foreach (var i in p)
|
||||||
|
{
|
||||||
|
Console.WriteLine("参数:" +i.ParameterName+",参数值"+i.Value);
|
||||||
|
}
|
||||||
Console.WriteLine("_______________________________________________");
|
Console.WriteLine("_______________________________________________");
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user