zoukankan      html  css  js  c++  java
  • Dapper基础知识二

    在下刚毕业工作,之前实习有用到Dapper?这几天新项目想用上Dapper,在下比较菜鸟,这块只是个人对Dapper的一种总结。

    2,如何使用Dapper?

        首先Dapper是支持多种数据库的,当时在学习的时候参考蓝老师的资料https://www.cnblogs.com/lanxiaoke/p/6503022.html。

         Dapper支持多数据库的工厂类,设计模式的工厂模式,Skr·  Skr~。

     public interface IRepository<T> where T : class
        {
            void Add(T entity);
            void AddBatch(IEnumerable<T> entitys);
            void Update(T entity);
            void Delete(T entity);
            void Delete(string Id);
            void Delete(int Id);
            void Delete(Guid Id);
            T Get(string Id);
            T Get(Guid Id);
            T Get(int Id);
            T Get(T entity);
            T Get(Expression<Func<T, bool>> func);
            IEnumerable<T> GetAll();
            IEnumerable<T> GetList(Expression<Func<T, bool>> where = null, Expression<Func<T, bool>> order = null);
            Tuple<int, IEnumerable<T>> GetPage(Page page, Expression<Func<T, bool>> where = null, Expression<Func<T, bool>> order = null);
            long Count(Expression<Func<T, bool>> where = null);
        }
      public class IDapperRepository<T> :IRepository<T> where T:class
        {
            protected IDbConnection Conn { get; private set; }
            public IDapperRepository()
            {
                Conn = DbConnectionFactory.CreateDbConnection();
            }
            public void SetDbConnection(IDbConnection conn)
            {
                Conn = conn;
            } 
            
    
            public void Add(T entity)
            {
                
                Conn.Insert<T>(entity);
            }
    
            public void AddBatch(IEnumerable<T> entitys)
            {
                foreach (T entity in entitys)
                {
                    Add(entity);
                }
            }
    
            public void Update(T entity)
            {
                Conn.Update(entity);
            }
    
            public void Delete(T entity)
            {
                Conn.Delete(entity);
            }
    
            public void Delete(string Id)
            {
                var entity = Get(Id);
                if (entity == null)
                    return;
                Delete(entity);
            }
    
            public void Delete(int Id)
            {
                var entity = Get(Id);
                if (entity == null) return;
    
                Delete(entity);
            }
    
            public void Delete(Guid Id)
            {
                var entity = Get(Id);
                if (entity == null) return;
    
                Delete(entity);
            }
    
            public T Get(T entity)
            {
                return Conn.Get<T>(entity);
            }
    
            public T Get(Guid Id)
            {
                return Conn.Get<T>(Id);
            }
    
            public T Get(string Id)
            {
                return Conn.Get<T>(Id);
            }
    
            public T Get(int Id)
            {
                return Conn.Get<T>(Id);
            }
    
            public T Get(Expression<Func<T, bool>> func)
            {
                throw new NotImplementedException();
            }
    
            public IEnumerable<T> GetAll()
            {
                throw new NotImplementedException();
            }
    
            public IEnumerable<T> GetList(Expression<Func<T, bool>> where = null, Expression<Func<T, bool>> order = null)
            {
                throw new NotImplementedException();
            }
    
            public Tuple<int, IEnumerable<T>> GetPage(Page page, Expression<Func<T, bool>> where = null, Expression<Func<T, bool>> order = null)
            {
                throw new NotImplementedException();
            }
    
            public long Count(Expression<Func<T, bool>> where = null)
            {
                throw new NotImplementedException();
            }
        }

       

     由于Dapper是对Sqlmapper的扩展,所以当引入Dapper或者Dapper的扩展类之后,实例化IDbConnection 就可以使用上面的Dapper已经封装好的方法了。

    具体Dapper如何使用可看上一篇的小白的参考资料

    具体的方法可以查看引用的对象浏览器

    以上就是对Dapper的初级使用了。

    Dapper基础知识一

    Dapper基础知识二

  • 相关阅读:
    推销
    5132. 颜色交替的最短路径
    5130. 等价多米诺骨牌对的数量
    @babel/plugin-transform-runtime和@babel/preset-env的区别
    5128. 最深叶节点的最近公共祖先(二叉树)
    1094. 拼车
    1109. 航班预订统计(数组)
    5129. 表现良好的最长时间段(数组)
    path.resove 和 path.join
    【原生】 call、apply、bind 的基本使用方法,已经解析了某些源码
  • 原文地址:https://www.cnblogs.com/MasterLin/p/10041167.html
Copyright © 2011-2022 走看看