输入banner图图片脚本导航/分类

ORM 创新解放劳动力 -SqlSugar 新功能介绍

2.将字典存到内存,通过内存赋值 (缺点 字典表超过1000条以上性能很差 ,并且不能排序,或者LIKE

下面介绍通过SqlSugar的配置查询解决上2面个难题

 

1.3 配置表简化字典联表

配置字典表

   if (!db.ConfigQuery.Any()) 
   {
                var types= db.Queryable<DataDictionary>().Select(it => it.Type).Distinct().ToList();
                foreach (var type in types)
                {
                    db.ConfigQuery.SetTable<DataDictionary>(it => it.Code, it => it.Name, type, it => it.Type == type);
                }
   }

配置完我们查询就会很方便了

   var res=db.Queryable<Person>().Select(it => new Person()
   {
                 Id=it.Id.SelectAll(),
                 SexName=it.SexId.GetConfigValue<DataDictionary>("sex"),
                 ProviceName = it.SexId.GetConfigValue<DataDictionary>("province"),
                 CityName = it.SexId.GetConfigValue<DataDictionary>("city"),
   }).ToList();

//也支持支持写在Where或者Orderby 

1.4 简单联表查询也可以配置

 db.ConfigQuery.SetTable<Order>(it => it.Id, it => it.Name);//配置Order
var list3 = db.Queryable<OrderItem>().Select(it => new OrderItem { ItemId = it.ItemId.SelectAll(), OrderName = it.OrderId.GetConfigValue<Order>() //查询的时候直接用 }).ToList();

 总结:配置表查询的方式可以大大降低重复联表问题,并且配置好后基本就不要写JOIN了 

  

2、多租户+仓储+自动分配

SqlSugar多租户是通过ConfigId进行识别连接哪个库,新版本添加了实体配置ConfigId

      [TenantAttribute("1")]
        public class C1Table 
        {
           public string Id { get; set; }
        }

        [TenantAttribute("2")]
        public class C2Table
        {
            public string Id { get; set; }
        }

  

下面我们仓储就可以通过实体配置自动识别是连接哪个库  

       public class Repository<T> : SimpleClient<T> where T : class, new()
        {
            public Repository(ISqlSugarClient context = null) : base(context)//注意这里要有默认值等于null
            {
                if (context == null)
                {
                    var db = new SqlSugarClient(new List<ConnectionConfig> {
                                                        new ConnectionConfig()
                                                    {
                                                        ConfigId=1,
                                                        DbType = SqlSugar.DbType.SqlServer,
                                                        IsAutoCloseConnection = true,
                                                        ConnectionString = Config.ConnectionString
                                                    },
                                                        new ConnectionConfig()
                                                    {
                                                        ConfigId="2",
                                                        DbType = SqlSugar.DbType.SqlServer,
                                                        IsAutoCloseConnection = true,
                                                        ConnectionString = Config.ConnectionString2
                                                    }
                    });
                    var configId = typeof(T).GetCustomAttribute<TenantAttribute>().configId;
                    base.Context = db.GetConnection(configId);//根据实体的租户Id自动实别连接字符串
                }
            }

            /// <summary>
            /// 扩展方法,自带方法不能满足的时候可以添加新方法
            /// </summary>
            /// <returns></returns>
            public List<T> CommQuery(string sql)
            {
                //base.Context.Queryable<T>().ToList();可以拿到SqlSugarClient 做复杂操作
                return base.Context.Queryable<T>().Where(sql).ToList();
            }

        }

新版本还添加了切换仓储功能

        public class C1Service : Repository<C1Table>
        {
            public void Test() 
            {
                base.AsTenant().BeginTran();

                base.GetList(); //调用内部仓储方法
                base.ChangeRepository<Repository<C2Table>>().GetList();//调用外部仓储

                base.AsTenant().CommitTran();
            }
        }

  

 3、行列互转功能 

第一个参数 列名、第二个参数 头行名、第三个参数 值

 var test06 = db.Queryable<Order>()
                   .ToPivotTable(it => it.Id, it => it.Name, it => it.Sum(x => x.Price));//返回Datatable

 var test07 = db.Queryable<Order>()
            .ToPivotList(it => it.Id, it => it.Name, it => it.Sum(x => x.Price));//返回List<dynamic>
var test08 = db.Queryable<Order>() .ToPivotJson(it => it.Id, it => it.Name, it => it.Sum(x => x.Price));//返回Json

 

源码下载: https://github.com/donet5/SqlSugar

官网地址:  https://www.donet5.com/Home/Doc

ORM 创新解放劳动力 -SqlSugar 新功能介绍

标签:tom   reac   数据   插入   ==   团队   创建   var   new