介绍

sqlsugar是一款 老牌 .net数据库操作框架,由果糖大数据科技团队维护和更新 ,github star数仅次于ef 和 dapper

优点: 简单易用、功能齐全、高性能、轻量级、服务齐全、有专业技术支持一天18小时服务

支持数据库:mysql、sqlserver、sqlite、oracle 、 postgresql、达梦、人大金仓

 

框架新功能

最新稳定版本5.0.2.8 ,发布后1个月时间nuget下载量达到5000的版本,用户使用也相当满意

而在稳定版本的基础上又布了5.0.2.9版本

加入3大新功能

1. 配置查询 

解决了大量字典表和简单就为取一个name 就要写联表的问题,让单表查询解决一切

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

3.行转列

 

1、配置查询

解决了大量字典表和简单就为取一个name 就要写联表的问题,让单表查询解决一切

字典表我相信大家都全用到,他们可以方便的存储性别、学历、岗位等 一串数据 并进行typeid进行区分

1.1 创建测试数据

创建一个字典实体

    public class datadictionary
    {
        public string code { get; set; }
        public string name { get; set; }
        public string type { get; set; }
    }

创建字典表并向里面插入测试数据  

var db = getinstance();
            list<datadictionary> datas = new list<datadictionary>();
            datas.add(new datadictionary() { code="1", name="男",type="sex" });
            datas.add(new datadictionary() { code = "2", name = "女", type = "sex" });
            datas.add(new datadictionary() { code = "1", name = "南通市", type = "city" });
            datas.add(new datadictionary() { code = "2", name = "苏州市", type = "city" });
            datas.add(new datadictionary() { code = "1", name = "江苏省", type = "province" });
            datas.add(new datadictionary() { code = "2", name = "湖南省", type = "province" });

            db.codefirst.inittables<datadictionary>();//这样就能根据实体建表了
            db.insertable(datas).executecommand();//这样就能把数据插进数据库了

在建一个person表  

    public class person 
    {
        //数据库字段
        [sqlsugar.sugarcolumn(isprimarykey =true,isidentity =true)]
        public int id { get; set; }
        public string name { get; set; }
        public int sexid { get; set; }
        public int cityid { get; set; }
        public int provinceid { get; set; }

        //非数据库字段
        [sqlsugar.sugarcolumn(isignore =true)]
        public string sexname { get; set; }
        [sqlsugar.sugarcolumn(isignore = true)]
        public string cityname { get; set; }
        [sqlsugar.sugarcolumn(isignore = true)]
        public string provicename { get; set; }
    } 

1.2 传统字典联表实现缺点

如果我们要将person中的非数据字典查询出来那么我们就需要写有2种实现方式

1.连表或者子查询 (缺点 写起来很浪费时间

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);
                }
                //如果其中code都是唯一值可以按 1.4中的用法使用循环都不要
}

 

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

   var res=db.queryable<person>().select(it => new person()
   {
                 id=it.id.selectall(),
                 sexname=it.sexid.getconfigvalue<datadictionary>("sex"),
                 provincename = it.provinceid.getconfigvalue<datadictionary>("province"),
                 cityname = it.cityid.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
                                                    }
                    });
                    base.context = db;
                    var configid = typeof(t).getcustomattribute<tenantattribute>().configid;
                    db.changedatabase(configid);
                }
            }

            /// <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

 

.net数据库框架源码  

官网地址:  https://www.donet5.com/home/doc