🚀 feat(新增): 添加事务使用示例

This commit is contained in:
bunny 2024-09-03 13:48:02 +08:00 committed by Bunny
parent 992b110d1a
commit 57eb804feb
10 changed files with 23 additions and 50 deletions

View File

@ -2,19 +2,7 @@
<project version="4"> <project version="4">
<component name="GitCommitMessageStorage"> <component name="GitCommitMessageStorage">
<option name="messageStorage"> <option name="messageStorage">
<MessageStorage> <MessageStorage />
<option name="commitTemplate">
<CommitTemplate>
<option name="body" value="" />
<option name="changes" value="" />
<option name="closes" value="" />
<option name="scope" value="" />
<option name="skipCi" value="" />
<option name="subject" value="" />
<option name="type" value="feat" />
</CommitTemplate>
</option>
</MessageStorage>
</option> </option>
</component> </component>
</project> </project>

View File

@ -1,5 +1,4 @@
using Bunny.Common.Interceptor; using Bunny.Dao.Entity.Base;
using Bunny.Dao.Entity.Base;
using Bunny.Dao.Entity.System; using Bunny.Dao.Entity.System;
using log4net; using log4net;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;

View File

@ -2,7 +2,7 @@
using Bunny.Dao.Common.Constant; using Bunny.Dao.Common.Constant;
using Microsoft.EntityFrameworkCore.Diagnostics; using Microsoft.EntityFrameworkCore.Diagnostics;
namespace Bunny.Common.Interceptor; namespace Bunny.Common.Context.Database;
public class TransactionInterceptor : SaveChangesInterceptor public class TransactionInterceptor : SaveChangesInterceptor
{ {

View File

@ -19,6 +19,6 @@ public static class MinioContext
MinioClient = new MinioClient().WithEndpoint(endpoint).WithCredentials(accessKey, secretKey) MinioClient = new MinioClient().WithEndpoint(endpoint).WithCredentials(accessKey, secretKey)
// .WithSSL() // 使用HTTPS // .WithSSL() // 使用HTTPS
.Build(); .Build();
Log.Info($"Minio 初始化...\n初始化桶:{BucketName}"); Log.Info($"Minio 初始化...\t初始化桶:{BucketName}");
} }
} }

View File

@ -3,7 +3,7 @@
/// <summary> /// <summary>
/// 错误常量 /// 错误常量
/// </summary> /// </summary>
public class ExceptionConstant public static class ExceptionConstant
{ {
public const string FileSystemException = "文件系统错误"; public const string FileSystemException = "文件系统错误";
public const string UnknownException = "未知错误"; public const string UnknownException = "未知错误";

View File

@ -61,22 +61,17 @@ public class BlogService : IBlogService
/// <param name="url"></param> /// <param name="url"></param>
public void AddBatchBlogs(string url) public void AddBatchBlogs(string url)
{ {
using var transaction = DbContext.Database.BeginTransaction();
var list = new List<Blog>(); var list = new List<Blog>();
for (var i = 0; i <= 100000; i++)
list.Add(new Blog for (var i = 0; i <= 100; i++)
{ list.Add(new Blog { Url = $"Blog{i}" });
Url = url,
CreateTime = DateTime.Now,
UpdateTime = DateTime.Now,
UpdateUserId = Random.Shared.Next(),
CreateUserId = Random.Shared.NextInt64()
});
DbContext.Blogs.AddRange(list); DbContext.Blogs.AddRange(list);
DbContext.SaveChanges(); DbContext.SaveChanges();
transaction.Commit();
} }
/// <summary> /// <summary>
/// 批量删除BLog /// 批量删除BLog
/// </summary> /// </summary>

View File

@ -1,5 +1,4 @@
using System.Net; using NUnit.Framework;
using NUnit.Framework;
namespace Bunny.Test.Until.Test; namespace Bunny.Test.Until.Test;
@ -52,9 +51,14 @@ public class Test1
foreach (var i in arg) Console.WriteLine(i); foreach (var i in arg) Console.WriteLine(i);
} }
private static void Method1() [Test]
public void Method1()
{ {
Console.WriteLine("Method1"); var s = Guid.NewGuid().ToString();
Console.WriteLine(s);
var guid = Guid.Parse("9b7cbd24-cf3d-4944-ab5b-9cf5278a5921");
Console.WriteLine(guid);
} }
private static void Method2() private static void Method2()

View File

@ -19,7 +19,7 @@
&lt;TestId&gt;NUnit3x::945BE294-8057-40EE-9A98-5598D1A728B9::net8.0::Bunny.Test.Until.JobTest.JobTest1.TestJob&lt;/TestId&gt;&#xD; &lt;TestId&gt;NUnit3x::945BE294-8057-40EE-9A98-5598D1A728B9::net8.0::Bunny.Test.Until.JobTest.JobTest1.TestJob&lt;/TestId&gt;&#xD;
&lt;/TestAncestor&gt;&#xD; &lt;/TestAncestor&gt;&#xD;
&lt;/SessionState&gt;</s:String> &lt;/SessionState&gt;</s:String>
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=83126680_002Dc767_002D40a2_002Dadfd_002Daa4542ab8b4b/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="TestJob #2" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD; <s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=83126680_002Dc767_002D40a2_002Dadfd_002Daa4542ab8b4b/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="TestJob #2" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
&lt;TestAncestor&gt;&#xD; &lt;TestAncestor&gt;&#xD;
&lt;TestId&gt;NUnit3x::945BE294-8057-40EE-9A98-5598D1A728B9::net8.0::Bunny.Test.Until.JobTest.JobTest1.TestJob&lt;/TestId&gt;&#xD; &lt;TestId&gt;NUnit3x::945BE294-8057-40EE-9A98-5598D1A728B9::net8.0::Bunny.Test.Until.JobTest.JobTest1.TestJob&lt;/TestId&gt;&#xD;
&lt;TestId&gt;NUnit3x::945BE294-8057-40EE-9A98-5598D1A728B9::net8.0::Bunny.Test.Until.Test.Test1.Start&lt;/TestId&gt;&#xD; &lt;TestId&gt;NUnit3x::945BE294-8057-40EE-9A98-5598D1A728B9::net8.0::Bunny.Test.Until.Test.Test1.Start&lt;/TestId&gt;&#xD;
@ -27,7 +27,7 @@
&lt;TestId&gt;NUnit3x::945BE294-8057-40EE-9A98-5598D1A728B9::net8.0::Bunny.Test.Until.NetUtilTest.NetTest2&lt;/TestId&gt;&#xD; &lt;TestId&gt;NUnit3x::945BE294-8057-40EE-9A98-5598D1A728B9::net8.0::Bunny.Test.Until.NetUtilTest.NetTest2&lt;/TestId&gt;&#xD;
&lt;/TestAncestor&gt;&#xD; &lt;/TestAncestor&gt;&#xD;
&lt;/SessionState&gt;</s:String> &lt;/SessionState&gt;</s:String>
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=eb9da086_002D824a_002D4ac4_002Db755_002D94438e510122/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="Test1" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD; <s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=eb9da086_002D824a_002D4ac4_002Db755_002D94438e510122/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="Test1" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
&lt;Or&gt;&#xD; &lt;Or&gt;&#xD;
&lt;And&gt;&#xD; &lt;And&gt;&#xD;
&lt;TestAncestor&gt;&#xD; &lt;TestAncestor&gt;&#xD;
@ -48,6 +48,7 @@
&lt;TestId&gt;NUnit3x::945BE294-8057-40EE-9A98-5598D1A728B9::net8.0::Bunny.Test.Until.JobTest.JobTest1.TestJob&lt;/TestId&gt;&#xD; &lt;TestId&gt;NUnit3x::945BE294-8057-40EE-9A98-5598D1A728B9::net8.0::Bunny.Test.Until.JobTest.JobTest1.TestJob&lt;/TestId&gt;&#xD;
&lt;TestId&gt;NUnit3x::945BE294-8057-40EE-9A98-5598D1A728B9::net8.0::Bunny.Test.Until.Test.Test1.Start&lt;/TestId&gt;&#xD; &lt;TestId&gt;NUnit3x::945BE294-8057-40EE-9A98-5598D1A728B9::net8.0::Bunny.Test.Until.Test.Test1.Start&lt;/TestId&gt;&#xD;
&lt;TestId&gt;NUnit3x::945BE294-8057-40EE-9A98-5598D1A728B9::net8.0::Bunny.Test.Until.NetUtilTest.NetTest2&lt;/TestId&gt;&#xD; &lt;TestId&gt;NUnit3x::945BE294-8057-40EE-9A98-5598D1A728B9::net8.0::Bunny.Test.Until.NetUtilTest.NetTest2&lt;/TestId&gt;&#xD;
&lt;TestId&gt;NUnit3x::945BE294-8057-40EE-9A98-5598D1A728B9::net8.0::Bunny.Test.Until.Test.Test1.Method1&lt;/TestId&gt;&#xD;
&lt;/TestAncestor&gt;&#xD; &lt;/TestAncestor&gt;&#xD;
&lt;/Or&gt;&#xD; &lt;/Or&gt;&#xD;
&lt;/SessionState&gt;</s:String> &lt;/SessionState&gt;</s:String>

View File

@ -17,9 +17,9 @@ public class BaseConfig(WebApplicationBuilder builder)
{ {
// 配置跨域 // 配置跨域
UseCors(); UseCors();
// 配置日志相关 // 配置日志相关
// builder.Logging.AddLog4Net("Configuration/log4net.config"); builder.Logging.AddLog4Net("Configuration/log4net.config");
Log4NetConfiguration.ConfigureLog4Net("Configuration/log4net.config");
// 自定义时间格式 // 自定义时间格式
builder.Services.AddControllers().AddJsonOptions(options => builder.Services.AddControllers().AddJsonOptions(options =>
options.JsonSerializerOptions.Converters.Add(new JsonDateTimeConverter())); options.JsonSerializerOptions.Converters.Add(new JsonDateTimeConverter()));

View File

@ -1,14 +0,0 @@
using log4net;
using log4net.Config;
namespace Bunny.WebApi.Configuration;
public class Log4NetConfiguration
{
private static readonly ILog Log = LogManager.GetLogger(typeof(Log4NetConfiguration));
public static void ConfigureLog4Net(string configFilePath)
{
XmlConfigurator.Configure(new FileInfo(configFilePath));
}
}