自己 简单封装的 dapper help 类



using Dapper;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using ToneCommon.CommonMethod;

namespace ToneFramework
{
    public sealed class DapperHelper
    {
        static readonly DapperHelper instance = null;

        private static IConfiguration Configuration = ConfigurationHelper.GetConfiguration("appsettings.json");
        private static string ConnectionStr = Configuration["SQLServer"];

        private DapperHelper()
        {
        }
        static DapperHelper()
        {

            instance = new DapperHelper();
        }

        /// 
        /// 获取实例
        /// 
        /// 
        public static DapperHelper GetInstance()//string connectionString
        {
            return instance;
        }

        /// 
        /// 获取数据连接
        /// 
        /// 
        public SqlConnection GetConnection()
        {
            SqlConnection connection = new SqlConnection(ConnectionStr);  //这里sqlconnection就是数据库连接字符串
            return connection;
        }
        /// 
        /// 打开获取的连接
        /// 
        /// 
        public SqlConnection OpenConnection()
        {
            SqlConnection connection = GetConnection();
            connection.Open();
            return connection;
        }



        /// 
        /// 插入 一个实体
        /// 
        /// 
        /// 
        /// 实体id
        public Guid Insert(TEntity entity) where TEntity : DBBaseModel
        {
            if (entity == null)
            {
                throw new Exception("传入数据不可为null!");
            }
            if (entity.Id == Guid.Empty)
            {
                throw new Exception("传入数据Id不可为空!");
            }
            Type type = typeof(TEntity);
            var propArray = type.GetProperties();//.Where(p => !p.Name.Equals("Id"));
            string columnString = string.Join(",", propArray.Select(p => $"[{p.Name}] "));
            string valuesString = string.Join(",", propArray.Select(p => $"@{p.Name} "));
            string sql = $"  INSERT INTO  [{type.Name}]  ( {columnString} )   VALUES ( {valuesString} ) ; ";

            using (SqlConnection conn = OpenConnection())
            {
                if (GetConnection().Execute(sql, entity) < 1)
                {
                    throw new Exception("插入不成功。");
                }
            }

            return entity.Id;
        }



        /// 
        /// 执行选择单个值的参数化SQL。
        /// 
        /// 
        /// 返回类型。
        public T ExecuteScalar(string sql, object param = null)
        {
            using (SqlConnection conn = OpenConnection())
            {
                return SqlMapper.ExecuteScalar(conn, sql, param);
            }
        }

        /// 
        /// 根据id 查询实体
        /// 
        /// 
        /// 
        /// 
        public TEntity GetById(Guid id) where TEntity : DBBaseModel
        {
            using (IDbConnection cnn = OpenConnection())
            {
                string sql = $"SELECT * FROM  [{typeof(TEntity).Name}]  WHERE Id={id} ";
                return cnn.Query(sql).FirstOrDefault();
            }
        }

        /// 
        /// 修改实体
        /// 
        /// 
        /// 
        public void Update(TEntity entity) where TEntity : DBBaseModel
        {
            if (entity == null)
            {
                throw new Exception("传入数据不可为null!");
            }
            Type type = typeof(TEntity);
            var propArray = type.GetProperties().Where(p => !p.Name.Equals("Id"));
            string columnString = string.Join(",", propArray.Select(p => $"[{p.Name}]=@{p.Name}"));
            string sql = $"UPDATE [{type.Name}] SET {columnString} WHERE Id={entity.Id}  ";
            using (SqlConnection conn = OpenConnection())
            {
                if (GetConnection().Execute(sql, entity) < 1)
                {
                    throw new Exception("Update数据不存在");
                }
            }

        }

        /// 
        /// 根据 实体 id 删除 实体
        /// 
        /// 
        /// 
        public void Delete(int id) where TEntity : DBBaseModel
        {
            int affectCount = 0;
            string sql = $"SELECT * FROM  [{typeof(TEntity).Name}]  WHERE Id={id}  ";
            using (SqlConnection conn = OpenConnection())
            {
                using (SqlCommand command = new SqlCommand(sql, conn))
                {
                    affectCount = command.ExecuteNonQuery();
                    if (affectCount == 0)
                    {
                        throw new Exception("Delete数据不存在");
                    }
                }
            }
        }

        /// 
        /// 执行 sql 语句 返回影响行数
        /// 
        /// 
        /// 
        /// 
        public int ExecuteSql(string sql, object param = null)
        {
            using (SqlConnection conn = OpenConnection())
            {
                return conn.Execute(sql, param);
            }
        }


        /// 
        /// 执行sql 语句返回 结果集
        /// 
        /// 
        /// 
        /// 
        public IEnumerable Query(string sql, object param = null)
        {
            using (SqlConnection conn = OpenConnection())
            {
                return SqlMapper.Query(conn, sql, param);
            }
        }

        /// 
        /// 执行sql 语句返回 结果集
        /// 
        /// 
        /// 
        /// 
        public IEnumerable Query(string sql, object param = null) where T : class, new()
        {
            using (SqlConnection conn = OpenConnection())
            {
                return SqlMapper.Query(conn, sql, param);
            }
        }



    }
}
using System;
using System.Collections.Generic;
using System.Text;

namespace ToneFramework
{
    /// 
    /// 数据库 基础model (主要用于约束 主键)
    /// 
    public class DBBaseModel
    {
        /// 
        /// 主键
        /// 
        public Guid Id { get; set; }
    }
}

 

你可能感兴趣的:(其他)