.net 简单的数据库操作框架
source link: https://www.cnblogs.com/sunkaixuan/p/14701585.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
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 ProviceId { 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"), 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 } }); 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
Recommend
-
108
如何利用简单的Excel操作来预测用户流失?! 什么是用户流失率?我们为什么需要关注用户流失率?简单来说,用户流失率是指用户的流失数量与全部使用/消费产品(或服务)用户的数量的比例,是用户流失的定量...
-
50
Linux的简单操作
-
44
2019年11月20日阅读 18067JS操作小技巧,工作简单了一、Optional Chaining 在javaScript中,对象的属性链访问,很容易因为某一个属性不存在出现 Cannot read property 'xxx' of u...
-
25
安装erlang 因为RabbitMQ是基于erlang开发的 安装RabbiMQ 上述步骤自行百度windows linux都支持 安装注意事项: erlang要和RabbiMQ版本对应上 RabbiMQ windo...
-
13
Three20研究院之结合第三方FMDB框架操作数据库详解(四) 开始本章之间,首先了解一下FMDB,它是一个老外写的第三...
-
4
title: 简单分支操作 今天的主角是分支,因为不介绍分支( branch )的概念,下面的操作是没办法介绍了。可以这样说,Git 最核心的操作对象是版本( commit ),最核心的操作技巧就是分支。 什么是分支? 仓库创建后,一旦有了新...
-
6
Golang简单写文件操作的四种方法 bluezwt · 2014-12-18 18:00:01 · 113262 次点击 · 预计阅读时间 2 分钟 · 大约8小时之前 开始浏览
-
4
python简单操作redis 作者:the5fire | 标签: python redis | 发...
-
6
K8S CronJob简单入门,和手动重复操作Say Goodbye! ...
-
9
V2EX › Go 编程语言 Go 开发的 数据库操作框架 Joker123456789 · 1 天前 · 1289 次点击
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK