@@ -564,13 +564,13 @@ static void Main(string[] args)
564564
565565 #region 初始化 IFreeSql
566566 var fsql = new FreeSql . FreeSqlBuilder ( )
567- // .UseAutoSyncStructure(true)
567+ . UseAutoSyncStructure ( true )
568568 . UseNoneCommandParameter ( true )
569569 //.UseNameConvert(NameConvertType.ToLower)
570570 . UseMappingPriority ( MappingPriorityType . Attribute , MappingPriorityType . FluentApi , MappingPriorityType . Aop )
571- . UseAdoConnectionPool ( true )
571+ // .UseAdoConnectionPool(true)
572572
573- . UseConnectionString ( FreeSql . DataType . Sqlite , "data source=123.db " )
573+ . UseConnectionString ( FreeSql . DataType . Sqlite , "data source=:memory: " )
574574 //.UseConnectionString(DataType.Sqlite, "data source=db1.db;attachs=db2.db")
575575 //.UseSlave("data source=test1.db", "data source=test2.db", "data source=test3.db", "data source=test4.db")
576576 //.UseSlaveWeight(10, 1, 1, 5)
@@ -579,7 +579,7 @@ static void Main(string[] args)
579579 //.UseConnectionString(FreeSql.DataType.Firebird, @"database=localhost:D:\fbdata\EXAMPLES.fdb;user=sysdba;password=123456;max pool size=5")
580580 //.UseQuoteSqlName(false)
581581
582- . UseConnectionString ( FreeSql . DataType . MySql , "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;min pool size=1;Max pool size=3;AllowLoadLocalInfile=true" )
582+ // .UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;min pool size=1;Max pool size=3;AllowLoadLocalInfile=true")
583583
584584 //.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=3;TrustServerCertificate=true")
585585 //.UseAdoConnectionPool(false)
@@ -621,6 +621,63 @@ static void Main(string[] args)
621621 BaseEntity . Initialization ( fsql , ( ) => _asyncUow . Value ) ;
622622 #endregion
623623
624+ Console . WriteLine ( "--- 正在初始化测试数据 ---" ) ;
625+
626+ // 插入班级
627+ var class1Id = ( int ) fsql . Insert ( new ClassesEntity { ClassesName = "高三一班" } ) . ExecuteIdentity ( ) ;
628+ var class2Id = ( int ) fsql . Insert ( new ClassesEntity { ClassesName = "高三二班" } ) . ExecuteIdentity ( ) ;
629+
630+ // 插入小组信息
631+ var groupId1 = ( int ) fsql . Insert ( new BizDingGroupEntity { GroupCode = "G001" , GroupName = "数学竞赛组" } ) . ExecuteIdentity ( ) ;
632+ var groupId2 = ( int ) fsql . Insert ( new BizDingGroupEntity { GroupCode = "G002" , GroupName = "英语角" } ) . ExecuteIdentity ( ) ;
633+ var groupId3 = ( int ) fsql . Insert ( new BizDingGroupEntity { GroupCode = "G003" , GroupName = "物理实验组" } ) . ExecuteIdentity ( ) ;
634+
635+ // 插入学生
636+ var s1Id = ( int ) fsql . Insert ( new StudentEntity { Name = "张三" , ClassId = class1Id , CreatedTime = DateTime . Now } ) . ExecuteIdentity ( ) ;
637+ var s2Id = ( int ) fsql . Insert ( new StudentEntity { Name = "李四" , ClassId = class1Id , CreatedTime = DateTime . Now . AddDays ( - 1 ) } ) . ExecuteIdentity ( ) ;
638+ var s3Id = ( int ) fsql . Insert ( new StudentEntity { Name = "王五" , ClassId = class2Id , CreatedTime = DateTime . Now . AddMinutes ( - 10 ) } ) . ExecuteIdentity ( ) ;
639+
640+ // 插入学生与小组的关联 (BizStudentGroupEntity)
641+ fsql . Insert ( new List < BizStudentGroupEntity > {
642+ new BizStudentGroupEntity { StudentId = s1Id , GroupId = groupId1 } , // 张三 - 数学
643+ new BizStudentGroupEntity { StudentId = s1Id , GroupId = groupId2 } , // 张三 - 英语
644+ new BizStudentGroupEntity { StudentId = s2Id , GroupId = groupId1 } , // 李四 - 数学
645+ new BizStudentGroupEntity { StudentId = s3Id , GroupId = groupId3 } // 王五 - 物理
646+ } ) . ExecuteAffrows ( ) ;
647+
648+ var _studentRepository = fsql . GetRepository < StudentEntity > ( ) ;
649+ var _filter = new FilterDto { SchoolClassGroupCode = "G001" } ;
650+ var input = new { CurrentPage = 1 , PageSize = 10 } ;
651+
652+ // 2. 模拟图片中的查询逻辑
653+ var listxx2 = fsql . Select < StudentEntity , ClassesEntity > ( )
654+ . LeftJoin ( ( a , b ) => a . ClassId == b . Id )
655+ . OrderByDescending ( ( a , b ) => a . CreatedTime )
656+ . Count ( out var totalxx2 )
657+ . Page ( input . CurrentPage , input . PageSize )
658+ . ToList ( ( a , b ) => new StudentGetPageOutput
659+ {
660+ ClassName = b . ClassesName ,
661+
662+ SchoolClassGroups = fsql . Select < BizStudentGroupEntity , BizDingGroupEntity > ( )
663+ . LeftJoin ( ( f , g ) => f . GroupId == g . Id && f . StudentId == a . Id )
664+ . WhereIf ( ! string . IsNullOrEmpty ( _filter . SchoolClassGroupCode ) ,
665+ ( f , g ) => g . GroupCode == _filter . SchoolClassGroupCode )
666+ . ToList ( ( f , g ) => new IdNameDto
667+ {
668+ Id = g . Id ,
669+ Name = g . GroupName
670+ } )
671+ } ) ;
672+ Console . WriteLine ( $ "\n 查询完成,总学生数: { totalxx2 } ") ;
673+ foreach ( var item in listxx2 )
674+ {
675+ var groups = item . SchoolClassGroups != null
676+ ? string . Join ( ", " , item . SchoolClassGroups . Select ( x => x . Name ) )
677+ : "无" ;
678+ Console . WriteLine ( $ "班级: { item . ClassName } | 关联小组: { groups } ") ;
679+ }
680+
624681 var orderStatusValue = Order1 . OrderStatus . Cancelled ;
625682 fsql . Select < Order1 > ( ) . Where ( a => a . status == Order1 . OrderStatus . Completed ) . InsertInto ( null , a => new Order1
626683 {
@@ -3735,4 +3792,54 @@ public partial class platactioninuser
37353792 [ JsonProperty ]
37363793 public int ? PlatStatus { get ; set ; }
37373794
3795+ }
3796+
3797+ public class StudentEntity
3798+ {
3799+ [ Column ( IsIdentity = true ) ] public int Id { get ; set ; }
3800+ public string Name { get ; set ; }
3801+ public int ClassId { get ; set ; }
3802+ public DateTime CreatedTime { get ; set ; }
3803+ }
3804+
3805+ public class ClassesEntity
3806+ {
3807+ [ Column ( IsIdentity = true ) ] public int Id { get ; set ; }
3808+ public string ClassesName { get ; set ; }
3809+ }
3810+
3811+ // 关联表:学生与小组
3812+ public class BizStudentGroupEntity
3813+ {
3814+ [ Column ( IsIdentity = true ) ] public int Id { get ; set ; }
3815+ public int StudentId { get ; set ; }
3816+ public int GroupId { get ; set ; }
3817+ }
3818+
3819+ // 小组信息表
3820+ public class BizDingGroupEntity
3821+ {
3822+ [ Column ( IsIdentity = true ) ] public int Id { get ; set ; }
3823+ public string GroupCode { get ; set ; }
3824+ public string GroupName { get ; set ; }
3825+ }
3826+
3827+ // --- DTO 输出类 ---
3828+
3829+ public class StudentGetPageOutput
3830+ {
3831+ public string ClassName { get ; set ; }
3832+ public List < IdNameDto > SchoolClassGroups { get ; set ; }
3833+ }
3834+
3835+ public class IdNameDto
3836+ {
3837+ public int Id { get ; set ; }
3838+ public string Name { get ; set ; }
3839+ }
3840+
3841+ // 模拟过滤器
3842+ public class FilterDto
3843+ {
3844+ public string SchoolClassGroupCode { get ; set ; }
37383845}
0 commit comments