• Dapper的使用


    轻量型ORM框架Dapper的使用

            /// <summary>
            /// 查询操作
            /// </summary>
            /// <typeparam name="T">返回集合的类型</typeparam>
            /// <param name="sql">sql语句</param>
            /// <param name="param">参数化值</param>
            /// <returns></returns>
            public IEnumerable<T> Query<T>(string sql, object param)
            {
                IEnumerable<T> _list = default(IEnumerable<T>);
                if (!string.IsNullOrEmpty(sql))
                {
                    try
                    {
                        _list = DbConnection.Query<T>(sql, param);
                    }
                    catch { }
                }
                return _list;
            }
    
            /// <summary>
            /// 执行存储过程查询操作
            /// </summary>
            /// <typeparam name="T">返回集合的类型</typeparam>
            /// <param name="storedName">存储过程</param>
            /// <param name="param">参数化值</param>
            /// <returns></returns>
            public IEnumerable<T> QueryStored<T>(string storedName, object param)
            {
                IEnumerable<T> _list = default(IEnumerable<T>);
                if (!string.IsNullOrEmpty(storedName))
                {
                    try
                    {
                        _list = DbConnection.Query<T>(storedName,commandType: CommandType.StoredProcedure);
                    }
                    catch { }
                }
                return _list;
            }
    
            /// <summary>
            /// 查询操作返回默认第一条数据(如返回null则创建默认类型)
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="sql"></param>
            /// <param name="param"></param>
            /// <returns></returns>
            public T FirstOrDefault<T>(string sql, object param)
            {
                var model = default(T);
                if (!string.IsNullOrEmpty(sql))
                {
                    try
                    {
                        model = DbConnection.Query<T>(sql, param).FirstOrDefault();
                    }
                    catch { }
                }
                return model == null ? Activator.CreateInstance<T>() : model;
            }
    
    
    
            /// <summary>
            /// 查询一组SQL语句并返回值
            /// </summary>
            /// <typeparam name="T1">第一条语句返回集合类型</typeparam>
            /// <typeparam name="T2">第二条语句返回集合类型</typeparam>
            /// <param name="sql">sql语句</param>
            /// <param name="param">参数化值</param>
            /// <returns></returns>
            public Tuple<IEnumerable<T1>, IEnumerable<T2>> Query<T1, T2>(string sql, object param)
            {
                IEnumerable<T1> _item1 = null; IEnumerable<T2> _item2 = null;
                if (!string.IsNullOrEmpty(sql))
                {
                    try
                    {
                        using (var multi = DbConnection.QueryMultiple(sql, param))
                        {
                            _item1 = multi.Read<T1>();
                            _item2 = multi.Read<T2>();
                        }
                    }
                    catch { }
                }
                return Tuple.Create<IEnumerable<T1>, IEnumerable<T2>>(_item1, _item2);
            }
    
            /// <summary>
            /// 查询一组SQL语句并返回值
            /// </summary>
            /// <typeparam name="T1">第一条语句返回集合类型</typeparam>
            /// <typeparam name="T2">第二条语句返回集合类型</typeparam>
            /// <typeparam name="T3">第三条语句返回集合类型</typeparam>
            /// <param name="sql">sql语句</param>
            /// <param name="param">参数化值</param>
            /// <returns></returns>
            public Tuple<IEnumerable<T1>, IEnumerable<T2>, IEnumerable<T3>> Query<T1, T2, T3>(string sql, object param)
            {
                IEnumerable<T1> _item1 = null; IEnumerable<T2> _item2 = null; IEnumerable<T3> _item3 = null;
                if (!string.IsNullOrEmpty(sql))
                {
                    try
                    {
                        using (var multi = DbConnection.QueryMultiple(sql, param))
                        {
                            _item1 = multi.Read<T1>();
                            _item2 = multi.Read<T2>();
                            _item3 = multi.Read<T3>();
                        }
                    }
                    catch { }
                }
                return Tuple.Create<IEnumerable<T1>, IEnumerable<T2>, IEnumerable<T3>>(_item1, _item2, _item3);
            }
    
    
    
            public void Dispose()
            {
                if (dbConnection != null)
                {
                    try
                    {
                        bool isClosed = dbConnection.State == ConnectionState.Closed;
                        if (!isClosed) dbConnection.Close();
                        //dbConnection.Dispose();
                    }
                    catch { }
                }
            }
        }
    复制代码

     

    调用示例如下:

     

     

    复制代码
    public abstract class BaseProvide
        {
            public Lazy<LiunianContext> lazyContext;
    
            protected LiunianContextDb
            {
                get
                {
                    return lazyContext.Value;
                }
            }
    
        }
    
       public class EmployeeProvide : BaseProvide
        {
            /// <summary>
            /// 获取数据
            /// </summary>
            /// <returns></returns>
            public List<Employee> ListEmployeeByParam(int[] param)
            {
                string sql = "select * from Employee where ID in @Ids";
                return Db.Query<Employee>(sql, new { Ids = param }).ToList();
            }
    
            /// <summary>
            /// 获取全部数据
            /// </summary>
            /// <returns></returns>
            public List<Employee> ListEmployee()
            {
                string sql = "select * from Employee";
                return Db.Query<Employee>(sql,null).ToList();
            }
    
            /// <summary>
            /// 查询单个实体
            /// </summary>
            /// <param name="Id"></param>
            /// <returns></returns>
            public Employee loadEmployee(int Id)
            {
                string sql = "select * from Employee where ID= @Id";
                return Db.FirstOrDefault<Employee>(sql, new { Id = Id });
            }
    
            /// <summary>
            /// 插入操作
            /// </summary>
            /// <param name="model"></param>
            /// <returns></returns>
            public bool InsertEmployee(Employee model)
            {
                StringBuilder sql = new StringBuilder();
                sql.Append("insert into Employee values (@UpdateTime,@CreateTime,@State,@LoginName,@UserName,");
                sql.Append("@RoleId,@IsUse)");
                return Db.Execute(sql.ToString(), model);
            }
    
            /// <summary>
            /// 更新操作
            /// </summary>
            /// <param name="model"></param>
            /// <returns></returns>
            public bool UpdateEmployee(Employee model)
            {
                StringBuilder sql = new StringBuilder();
                sql.Append("update Employee set UpdateTime = @UpdateTime where  ID = @ID");
                return Db.Execute(sql.ToString(), model);
            }
    
            /// <summary>
            /// 删除操作
            /// </summary>
            /// <param name="Id"></param>
            /// <returns></returns>
            public bool DeleteEmployeeById(int Id)
            {
                string sql = "delete from Employee where ID = @Id";
                return Db.Execute(sql.ToString(), new { Id = Id });
            }
    
            /// <summary>
            /// 执行多个语句返回多个结果
            /// </summary>
            /// <param name="Id"></param>
            /// <returns></returns>
            public Tuple<int,IEnumerable<Employee>> ListGroupEmployee()
            {
                string sql = "select Count(*) from Employee select * from Employee";
                Tuple<IEnumerable<int>, IEnumerable<Employee>> queryGroup = Db.Query<int, Employee>(sql, null);
                return Tuple.Create<int, IEnumerable<Employee>>(queryGroup.Item1.FirstOrDefault(),queryGroup.Item2);
            }
    
    
        }
    复制代码

     

    除了没把事务加上之外其他的增删改查都有了,同时在传对象的时候Dapper也解决了sql注入的问题,在性能上也是比较好的ORM框架,需要的拿去!

  • 相关阅读:
    android 生命周期图
    c++中 箭头> 双冒号:: 点号.操作符区别
    Blocks 笔记
    使用AVAudioRecorder 录音
    音频输入大小变化图
    【Linux】本机与服务器文件互传、Linux服务器文件上传下载
    【Oracle】Oracle解锁、Oracle锁表处理
    使用three.js创建3D机房模型分享一
    WPF下递归生成树形数据绑定到TreeView上
    (转载)C++抽象工厂模式(大话设计模式)
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/4953722.html
Copyright © 2020-2023  润新知