C#ORM框架Dapper封装

接口:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace MVC.Interface
{
    public interface IBaseRepository<T>
    {
        #region  成员方法
        /// <summary>
        /// 增加一条数据
        /// </summary>
        bool Add(T model);
        /// <summary>
        /// 根据ID删除一条数据
        /// </summary>
        bool Delete(int Id);
        /// <summary>
        /// 根据条件删除数据
        /// </summary>
        /// <param name="strWhere"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        bool DeleteList(string strWhere, object parameters);
        /// <summary>
        /// 更新一条数据
        /// </summary>
        bool Update(T model);
        /// <summary>
        /// 根据ID获取实体对象
        /// </summary>
        T GetModel(int Id);
        ///// <summary>
        ///// 根据条件获取实体对象
        ///// </summary>
        //T GetModel(string strWhere, object parameters);
        /// <summary>
        /// 根据条件获取实体对象集合
        /// </summary>
        /// <param name="strWhere"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        IEnumerable<T> GetModelList(string strWhere, object parameters);
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="pageNum">页码</param>
        /// <param name="rowsNum">每页行数</param>
        /// <param name="strWhere">where条件</param>
        /// <param name="orderBy">Orde by排序</param>
        /// <param name="parameters">parameters参数</param>
        /// <returns></returns>
        IEnumerable<T> GetListPage(int pageNum, int rowsNum, string strWhere, string orderBy, object parameters);
        #endregion
    }

}

实现:

using Dapper;
using MVC.Common;
using MVC.Interface;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace MVC.BLL
{
    public class BaseRepository<T> : IBaseRepository<T>
    {
        private IDbConnection _connection;
        public bool Add(T model)
        {
            int? result;
             using (_connection= DbClient.OpenConnection())
            {
                    result= _connection.Insert(model);
                if (result > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }


        public bool Delete(int Id)
        {
            int? result;
            using (_connection = DbClient.OpenConnection())
            {
                result =_connection.Delete<T>(Id);
            }
            if (result > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        public bool DeleteList(string strWhere, object parameters)
        {
            int? result;
            using (_connection = DbClient.OpenConnection())
            {
                result =_connection.DeleteList<T>(strWhere,parameters);
            }
            if (result > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        public IEnumerable<T> GetListPage(int pageNum, int rowsNum, string strWhere, string orderBy, object parameters)
        {
            using (_connection = DbClient.OpenConnection())
            {
                return _connection.GetListPaged<T>(pageNum, rowsNum, strWhere, orderBy, parameters); ;
            }
        }


        public T GetModel(int Id)
        {
            using (_connection = DbClient.OpenConnection())
            {
                return _connection.Get<T>(Id);
            }
        }


        public IEnumerable<T> GetModelList(string strWhere, object parameters)
        {
            using (_connection = DbClient.OpenConnection())
            {
                return _connection.GetList<T>(strWhere, parameters);
            }
        }


        public bool Update(T model)
        {
            int? result;
            using (_connection = DbClient.OpenConnection())
            {
                result =_connection.Update(model);
            }
            if (result > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_35534449/article/details/80852753