zoukankan      html  css  js  c++  java
  • sqlsugar Mapper功能

    Mapper功能

    如果说 .Select() 也可以实现一对一的查询或者一些SQL函数但是毕竟是用来生成SQL的所以有很多局限性,Mapper是在查询出结果后进行处理所以任何C#方法都支持

    也更强大

    复制代码
     var s12 = db.Queryable<Student, School>((st, sc) => st.SchoolId == sc.Id).Select<ViewModelStudent3>()
    
                    .Mapper(it =>
                    {
    
                        it.Name = Md5(it.Name);
                        //有多少列要处理写多少列,能用Mapper的就少用Select兼容性更好些
    
                    }).ToList();
    复制代码

    高性能的一对多查询

    我们也可以用Mapper来实现一对多,弥补.Select()不足

    复制代码
    var s12 = db.Queryable<Student, School>((st, sc) => st.SchoolId == sc.Id).Select<ViewModelStudent3>()
    
    .Mapper((it, cache) =>
    {
    
        var allSchools = cache.GetListByPrimaryKeys<School>(vmodel => vmodel.SchoolId);
        //in(ViewModelStudent3[0].SchoolId , ViewModelStudent3[1].SchoolId...)
    
    
        /*one to many*/
        it.Schools = allSchools.Where(i => i.Id == it.SchoolId).ToList();
    
    
        /*C# syntax conversion*/
        it.Name = it.Name == null ? "null" : it.Name;
    
    }).ToList();
    复制代码

    一对多查询的性能可以秒杀其它ORM ,因为生成的SQL只有2条,并且这2条不会多查询一条没用的记录,有幸趣的可以研究一下,其它的都内存处理

    多Queryable查询

    Union all查询将结果集合并

    var getUnionAllList2 = db.UnionAll(db.Queryable<Student>(), db.Queryable<Student>()).ToList();//union all


    两个Queryable联表查询(有人说我只支持12表JOIN,那这样就可以支持24张表了)

    复制代码
    var q1 = db.Queryable<Student, School>((st,sc)=>new object[] {
                    JoinType.Left,st.SchoolId==sc.Id
                }).Select((st, sc) => new ViewModelStudent4() { Id=st.Id, Name=st.Name,SchoolName=sc.Name });
     
    var q2 = db.Queryable<School>();
     
     
    var innerJoinList = db.Queryable(q1, q2, (j1, j2) => j1.Id == j2.Id).Select((j1, j2) => j1).ToList();//inner join
     
    var leftJoinList = db.Queryable(q1, q2,JoinType.Left, (j1, j2) => j1.Id == j2.Id).Select((j1, j2) => j1).ToList();/
    复制代码

    二级缓存支持

    二级缓存功能是对查询出来的数据进行缓存,在缓存不失效的情况下,下次同样的查询操作都会从缓存内读取

    使用缓存查询

    var list=db.Queryable<Student, School>((s1, s2) => s1.Id == s2.Id).Select(s1 => s1).WithCache().ToList();//可以设置过期时间WithCache(60)

    删除缓存

    我们需要删除缓存也相当方便,只需要在对该表操作的时候加 RemoveDataCache 就能把查询中引用该表的缓存全部清除

    db.Deleteable<Student>().Where(it => it.Id == 1).RemoveDataCache().ExecuteCommand();

    //Updateable和Insertable一样用法

    自动删除缓存

    SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() {
                    ConnectionString = Config.ConnectionString,
                    MoreSettings =new  ConnMoreSettings(){
                          IsAutoRemoveDataCache=true
                    }

    创建db对象

    我们需要创建一个MyCache类,你可以用我写好的也可以用你自已写的实现缓存

    复制代码
    ICacheService myCache = new RedisCache("10.1.249.196");//ICacheService
    SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
    {
    ConnectionString = Config.ConnectionString,
    DbType = DbType.SqlServer,
    IsAutoCloseConnection = true,
    ConfigureExternalServices = new ConfigureExternalServices()
    {
    DataInfoCacheService = new RedisCache() //RedisCache是继承ICacheService自已实现的一个类
    }
    });
    
     
    复制代码

    我写好的Cache类可以作为参考

    Redis:

    https://github.com/sunkaixuan/SqlSugar/blob/dev/Src/Asp.Net/SqlSugar.Extensions.DataCache/RedisCache.cs

    .Net自带Cache:

    https://github.com/sunkaixuan/SqlSugar/blob/dev/Src/Asp.Net/SqlSugar.Extensions.DataCache/HttpRuntimeCache.cs

  • 相关阅读:
    关于bind named.conf.options
    MASM 16位汇编程序几种典型的格式
    About GCC
    WebForms UnobtrusiveValidationMode 需要“jQuery”ScriptResourceMapping。
    Linux系统下的shutdown命令用于安全的关闭/重启计算机
    TreeView.ImageSet 属性
    python seaborn
    python neo4j
    Impala与Hive的比较
    pandas.resample()
  • 原文地址:https://www.cnblogs.com/mrray/p/12855837.html
Copyright © 2011-2022 走看看