博客
关于我
.net 简单的数据库操作框架
阅读量:453 次
发布时间:2019-03-06

本文共 6431 字,大约阅读时间需要 21 分钟。

介绍

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
datas = new List
(); 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
();//这样就能根据实体建表了 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
().Select(it => it.Type).Distinct().ToList(); foreach (var type in types) { db.ConfigQuery.SetTable
(it => it.Code, it => it.Name, type, it => it.Type == type); } //如果其中Code都是唯一值可以按 1.4中的用法使用循环都不要}

 

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

var res=db.Queryable
().Select(it => new Person() { Id=it.Id.SelectAll(), SexName=it.SexId.GetConfigValue
("sex"), ProvinceName = it.ProvinceId.GetConfigValue
("province"), CityName = it.CityId.GetConfigValue
("city"), }).ToList();//也支持支持写在Where或者Orderby 

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

db.ConfigQuery.SetTable
(it => it.Id, it => it.Name);//配置Order var list3 = db.Queryable
().Select(it => new OrderItem { ItemId = it.ItemId.SelectAll(), OrderName = it.OrderId.GetConfigValue
() //查询的时候直接用 }).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
: SimpleClient
where T : class, new() { public Repository(ISqlSugarClient context = null) : base(context)//注意这里要有默认值等于null { if (context == null) { var db = new SqlSugarClient(new List
{ 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
().configId; db.ChangeDatabase(configId); } } ///
/// 扩展方法,自带方法不能满足的时候可以添加新方法 /// ///
public List
CommQuery(string sql) { //base.Context.Queryable
().ToList();可以拿到SqlSugarClient 做复杂操作 return base.Context.Queryable
().Where(sql).ToList(); } }

  

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

public class C1Service : Repository
{ public void Test() { base.AsTenant().BeginTran(); base.GetList(); //调用内部仓储方法 base.ChangeRepository
>().GetList();//调用外部仓储 base.AsTenant().CommitTran(); } }

  

 3、行列互转功能 

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

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

 

  

官网地址:  

转载地址:http://shofz.baihongyu.com/

你可能感兴趣的文章
MySQL Binlog 日志监听与 Spring 集成实战
查看>>
MySQL binlog三种模式
查看>>
multi-angle cosine and sines
查看>>
Mysql Can't connect to MySQL server
查看>>
mysql case when 乱码_Mysql CASE WHEN 用法
查看>>
Multicast1
查看>>
mysql client library_MySQL数据库之zabbix3.x安装出现“configure: error: Not found mysqlclient library”的解决办法...
查看>>
MySQL Cluster 7.0.36 发布
查看>>
Multimodal Unsupervised Image-to-Image Translation多通道无监督图像翻译
查看>>
MySQL Cluster与MGR集群实战
查看>>
multipart/form-data与application/octet-stream的区别、application/x-www-form-urlencoded
查看>>
mysql cmake 报错,MySQL云服务器应用及cmake报错解决办法
查看>>
Multiple websites on single instance of IIS
查看>>
mysql CONCAT()函数拼接有NULL
查看>>
multiprocessing.Manager 嵌套共享对象不适用于队列
查看>>
multiprocessing.pool.map 和带有两个参数的函数
查看>>
MYSQL CONCAT函数
查看>>
multiprocessing.Pool:map_async 和 imap 有什么区别?
查看>>
MySQL Connector/Net 句柄泄露
查看>>
multiprocessor(中)
查看>>