loading

SqlSugar DbContext

什么是SqlSugar?
SqlSugar 是 .NET/C# 平台非常优秀的 ORM 框架,目前 Nuget 总下载突破 1000K,Github 关注量也高达 3.7K
SqlSugar 拥有高性能,具有百万级插入、更新大数据分表等特色功能,是目前当之无愧的国产优秀 ORM 框架之一。

  1. 支持 MySqlSqlServerSqliteOraclepostgresql达梦人大金仓 等多种数据库
  2. 支持 全自动分表
  3. 支持 多库事务
  4. 支持 CodeFirst / DbFirst
  5. 支持 联表查询嵌套查询导航查询子查询动态JSON查询等查询操作
  6. 支持 SqlServer、MySql、PgSql、Oracle 百万级插入和更新

个人感受
其实说起来接触SqlSugar的时间并不是很长,但是用过之后,只有一个感觉:太丝滑了!
文档齐全,新手上路贼容易,真的强烈推荐!!!
目前本人手里的项目,只要与数据库交互的,已经统统切换成了SqlSugar

.Net Framework部分

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data;
    using System.Linq.Expressions;
    using SqlSugar;
    using DbType = SqlSugar.DbType;
    
    namespace CodeProject.Database
    {
        /// <summary>
        /// 数据库上下文
        /// </summary>
        public class SqlSugarDbContext
        {
            /// <summary>
            /// 数据库连接字符串(私有字段)
            /// </summary>
            private static string DbConnString = ConfigurationManager.ConnectionStrings["sqlConStr"].ConnectionString;
    
            /// <summary>
            /// 数据库类型(私有字段)
            /// </summary>
            private static string _databaseType = ConfigurationManager.ConnectionStrings["sqlConStr"].ProviderName;
    
            /// <summary>
            /// 用来处理事务多表查询和复杂的操作
            /// 注意:不能写成静态的
            /// </summary>
            public SqlSugarClient Db;
    
            public SqlSugarDbContext()
            {
                DbType dbType;
                switch (_databaseType)
                {
                    case "System.Data.SqlClient":
                        dbType = DbType.SqlServer;
                        break;
                    case "System.Data.SqliteClient":
                        dbType = DbType.Sqlite;
                        break;
                    case "MySql.Data.MySqlClient":
                        dbType = DbType.MySql;
                        break;
                    case "Oracle.ManagedDataAccess.Client":
                        dbType = DbType.Oracle;
                        break;
                    default:
                        dbType = DbType.SqlServer;
                        break;
                }
    
                Db = new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString = DbConnString,//数据库连接串
                    DbType = dbType,//数据库类型
                    InitKeyType = InitKeyType.SystemTable,//从数据库中读取主键和自增列信息         //InitKeyType.Attribute,//从特性读取主键和自增列信息
                    IsAutoCloseConnection = true,//开启自动释放模式和EF原理一样我就不多解释了
    
                });
                //调式代码 用来打印SQL 
                Db.Aop.OnLogExecuting = (sql, pars) =>
                {
                    Console.WriteLine(sql + "\r\n" +
                        Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
                    Console.WriteLine();
                };
            }
    
            #region 库表操作
            /// <summary>
            /// 备份表
            /// </summary>
            /// <param name="oldname">旧表名称</param>
            /// <param name="newname">新表名称</param>
            /// <returns></returns>
            public virtual bool BackupTable(string oldname, string newname)
            {
                if (!Db.DbMaintenance.IsAnyTable(newname, false))
                {
                    return Db.DbMaintenance.BackupTable(oldname, newname, 0);
                }
                return false;
            }
            /// <summary>
            /// 删除表
            /// </summary>
            /// <param name="tablename">表名称</param>
            /// <returns></returns>
            public virtual bool DropTable(string tablename)
            {
                return Db.DbMaintenance.DropTable(tablename);
            }
    
            /// <summary>
            /// 清空表
            /// </summary>
            /// <param name="tablename">表名称</param>
            /// <returns></returns>
            public virtual bool TruncateTable(string tablename)
            {
                return Db.DbMaintenance.TruncateTable(tablename);
            }
    
            #region CodeFirst 类-->表
            /// <summary>
            /// 创建单一表 
            /// </summary>
            /// <param name="entityType"></param>
            public void CreateTable(Type entityType)
            {
                Db.CodeFirst.SetStringDefaultLength(200).BackupTable().InitTables(entityType);
            }
    
            /// <summary>
            /// 批量创建表
            /// </summary>
            /// <param name="entityTypes"></param>
            public void CreateTables(Type[] entityTypes)
            {
                Db.CodeFirst.SetStringDefaultLength(200).BackupTable().InitTables(entityTypes);
            }
            #endregion
    
            #region DbFirst 表-->类
            /// <summary>
            /// 根据数据库表 生成实体类文件
            /// 数据库表名统一格式:XX_XXX  如:Sys_UserInfo
            /// </summary>
            /// <param name="filePath">类文件地址</param>
            /// <param name="nameSpace">命名空间</param>
            /// <param name="tableName">表名称</param>
            public virtual void CreateClassFiles(string filePath, string nameSpace, string tableName)
            {
                #region 格式化 实体类文件名称
                //循环遍历 数据库里的所有表  
                foreach (var item in Db.DbMaintenance.GetTableInfoList())
                {
                    string entityName = string.Empty;
                    if (item.Name.Contains("_"))
                    {
                        var tbName = item.Name.Split('_');
                        entityName = tbName[1] + "Entity";
                        Db.MappingTables.Add(entityName, item.Name);
                    }
                    else
                    {
                        entityName = item.Name + "Entity";
                        Db.MappingTables.Add(entityName, item.Name);
                    }
                    //循环遍历  当前表的所有列
                    foreach (var col in Db.DbMaintenance.GetColumnInfosByTableName(item.Name))
                    {
                        //所有列全部转大写
                        Db.MappingColumns.Add(col.DbColumnName.ToUpper(), col.DbColumnName, entityName);
                    }
                }
                #endregion
    
                //生成指定表名的实体类文件
                if (!string.IsNullOrEmpty(tableName))
                {
                    //生成带有SqlSugar特性的实体类文件
                    Db.DbFirst.Where(tableName).IsCreateAttribute(true).CreateClassFile(filePath, nameSpace);
                }
                //生成所有表的实体类文件
                else
                {
                    //生成带有SqlSugar特性的实体类文件
                    Db.DbFirst.IsCreateAttribute(true).CreateClassFile(filePath, nameSpace);
                }
            }
            #endregion
            #endregion
    
            #region 事务操作
            /// <summary>
            /// 开启事务
            /// </summary>
            public virtual void BeginTran()
            {
                Db.Ado.BeginTran();
            }
    
            /// <summary>
            /// 提交事务 
            /// </summary>
            public virtual void CommitTran()
            {
                Db.Ado.CommitTran();
            }
    
            /// <summary>
            /// 回滚事务
            /// </summary>
            public virtual void RollbackTran()
            {
                Db.Ado.RollbackTran();
            }
            #endregion
    
            #region 原生Sql
            /// <summary>
            /// 针对于 增删改
            /// </summary>
            /// <param name="sql"></param>
            /// <returns></returns>
            public virtual int ExecuteCommand(string sql, params SugarParameter[] pars)
            {
                return Db.Ado.ExecuteCommand(sql, pars);
            }
    
            /// <summary>
            /// 返回集合
            /// </summary>
            /// <param name="sql"></param>
            /// <returns>返回DataTable</returns>
            public virtual DataTable GetDataTable(string sql, params SugarParameter[] pars)
            {
                return Db.Ado.GetDataTable(sql, pars);
            }
    
            /// <summary>
            /// 返回多个集合
            /// </summary>
            /// <param name="sql"></param>
            /// <param name="pars"></param>
            /// <returns></returns>
            public virtual DataSet GetDataSet(string sql, params SugarParameter[] pars)
            {
                return Db.Ado.GetDataSetAll(sql, pars);
            }
    
            /// <summary>
            /// 调用存储过程 返回Output参数
            /// </summary>
            /// <param name="sql"></param>
            /// <param name="pars"></param>
            /// <returns>返回int</returns>
            public virtual int ExecProcToInt(string sql, params SugarParameter[] pars)
            {
                int proc_count = 0;
                try
                {
                    BeginTran();
                    proc_count = Db.Ado.UseStoredProcedure().GetInt(sql, pars);
                    CommitTran();
                }
                catch (Exception ex)
                {
                    RollbackTran();
                    throw ex;
                }
    
                return proc_count;
            }
    
            /// <summary>
            /// 调用存储过程
            /// </summary>
            /// <param name="sql"></param>
            /// <param name="pars"></param>
            /// <returns>返回DataTable</returns>
            public virtual DataTable ExecProcToDT(string sql, params SugarParameter[] pars)
            {
                DataTable dt = new DataTable();
                try
                {
                    BeginTran();
                    dt = Db.Ado.UseStoredProcedure().GetDataTable(sql, pars);
                    CommitTran();
                }
                catch (Exception ex)
                {
                    RollbackTran();
                    throw ex;
                }
    
                return dt;
            }
    
            /// <summary>
            /// 调用存储过程
            /// </summary>
            /// <param name="sql"></param>
            /// <param name="pars"></param>
            /// <returns>返回DataSet</returns>
            public virtual DataSet ExecProcToDS(string sql, params SugarParameter[] pars)
            {
                DataSet ds = new DataSet();
                try
                {
                    BeginTran();
                    ds = Db.Ado.UseStoredProcedure().GetDataSetAll(sql, pars);
                    CommitTran();
                }
                catch (Exception ex)
                {
                    RollbackTran();
                    throw ex;
                }
    
                return ds;
            }
            #endregion
    
            #region 泛型CURD
            /// <summary>
            /// 校验数据是否存在
            /// </summary>
            /// <param name="expression">Lambda表达式(查询条件)</param>
            /// <returns></returns>
            public virtual bool Any<TEntity>(Expression<Func<TEntity, bool>> expression) where TEntity : class, new()
            {
                return Db.Queryable<TEntity>().Any(expression);
            }
    
            /// <summary>
            /// 检查信息总条数
            /// </summary>
            /// <param name="expression">Lambda表达式(查询条件)</param>
            /// <returns></returns>
            public virtual int Count<TEntity>(Expression<Func<TEntity, bool>> expression) where TEntity : class, new()
            {
                return Db.Queryable<TEntity>().Count(expression);
            }
    
            /// <summary>
            /// 查询实体
            /// </summary>
            /// <param name="keyValue"></param>
            /// <returns>单条记录</returns>
            public virtual TEntity FindEntity<TEntity>(object keyValue) where TEntity : class, new()
            {
                return Db.Queryable<TEntity>().InSingle(keyValue);
            }
    
            /// <summary>
            /// 查询实体集合
            /// </summary>
            /// <returns></returns>
            public virtual ISugarQueryable<TEntity> Queryable<TEntity>() where TEntity : class, new()
            {
                return Db.Queryable<TEntity>();
            }
    
            /// <summary>
            /// 自定义条件查询
            /// </summary>
            /// <param name="expression">Lambda表达式(查询条件)</param>
            /// <returns></returns>
            public virtual ISugarQueryable<TEntity> Queryable<TEntity>(Expression<Func<TEntity, bool>> expression) where TEntity : class, new()
            {
                return Db.Queryable<TEntity>().Where(expression);
            }
    
            /// <summary>
            /// 通过SQL语句查询
            /// </summary>
            /// <param name="strSql">SQL语句</param>
            /// <returns></returns>
            public virtual ISugarQueryable<TEntity> Queryable<TEntity>(string strSql) where TEntity : class, new()
            {
                return Db.SqlQueryable<TEntity>(strSql);
            }
    
            /// <summary>
            /// 新增
            /// </summary>
            /// <param name="entity">实体信息</param>
            /// <returns></returns>
            public virtual int Insertable<TEntity>(TEntity entity) where TEntity : class, new()
            {
                return Db.Insertable(entity).ExecuteCommand();
            }
    
            /// <summary>
            /// 批量新增
            /// </summary>
            /// <param name="entities">实体信息集合</param>
            /// <returns></returns>
            public virtual int Insertable<TEntity>(List<TEntity> entities) where TEntity : class, new()
            {
                return Db.Insertable(entities).ExecuteCommand();
            }
    
            /// <summary>
            /// 编辑
            /// </summary>
            /// <param name="entity">实体信息</param>
            /// <returns></returns>
            public virtual int Updateable<TEntity>(TEntity entity) where TEntity : class, new()
            {
                return Db.Updateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommand();
            }
    
            /// <summary>
            /// 自定义条件编辑
            /// </summary>
            /// <param name="content"></param>
            /// <param name="predicate"></param>
            /// <returns></returns>
            public virtual int Updateable<TEntity>(Expression<Func<TEntity, TEntity>> content, Expression<Func<TEntity, bool>> predicate) where TEntity : class, new()
            {
                return Db.Updateable(content).Where(predicate).ExecuteCommand();
            }
    
            /// <summary>
            /// 删除
            /// </summary>
            /// <param name="entity">实体信息</param>
            /// <returns></returns>
            public virtual int Deleteable<TEntity>(TEntity entity) where TEntity : class, new()
            {
                return Db.Deleteable(entity).ExecuteCommand();
            }
    
            /// <summary>
            /// 自定义条件删除
            /// </summary>
            /// <param name="expression">Lambda表达式(查询条件)</param>
            /// <returns></returns>
            public virtual int Deleteable<TEntity>(Expression<Func<TEntity, bool>> expression) where TEntity : class, new()
            {
                return Db.Deleteable(expression).ExecuteCommand();
            }
            #endregion
        }
    }
    //数据库上下文 初始化
    SqlSugarDbContext db = new SqlSugarDbContext();

    #region 原生Sql
    var dt = db.GetDataTable(strSql,pars);//返回DataTable
    var ds = db.GetDataSet(strSql,pars);//返回DataSet
    var count = db.ExcuteCommand(strSql,pars);//针对于增删改,返回受影响行数
    ...
    #endregion

    #region 泛型CURD
    var list = db.Queryable<TEntity>().ToList();//返回集合
    var list = db.Queryable<TEntity>(expression).ToList();//返回集合
    ...
    #endregion

.Net Core部分

1.创建泛型仓储

/// <summary>
/// 泛型仓储
/// </summary>
public class DbRepository<TEntity> : SimpleClient<TEntity> where TEntity : class,new()
{
    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="db"></param>
    public DbRepository(ISqlSugarClient db)
    {
        Context = db;
    }
}

2.注入SqlSugar

/// <summary>
/// 注入SqlSugar数据库配置
/// </summary>
public static class DbStartup
{
    /// <summary>
    /// 注入
    /// </summary>
    /// <param name="services"></param>
    public static void AddSqlSugar(this IServiceCollection services)
    {
        SqlSugarScope Db = new SqlSugarScope(new ConnectionConfig()
        {
            ConfigId = "MasterDb",//数据库Id  区分数据库
            DbType = DbType.SqlServer,//数据库类型
            ConnectionString = "XXXX",//数据库链接串
            IsAutoCloseConnection = true,//自动释放
            LanguageType = LanguageType.Default,//多语言配置 Chinese/English
            MoreSettings = new ConnMoreSettings
            {
                IsAutoRemoveDataCache = true,//启用删除数据缓存
                IsAutoDeleteQueryFilter = true,//启用删除查询过滤器
                IsAutoUpdateQueryFilter = true,//启用更新查询过滤器
                SqlServerCodeFirstNvarchar = true//SqlServer数据库使用nvarchar
            }
        },db =>
        {
            //调试sql
            db.Aop.OnLogExecuting = (sql, pars) =>
            {
                Console.WriteLine(UtilMethods.GetNativeSql(sql, pars));
            };
        });

        //注入仓储
        services.AddScoped(typeof(DbRepository<>));

        //单例注入
        services.AddSingleton<ISqlSugarClient>(Db);
    }
}

3.在program.cs文件里注入

#region 注入SqlSugar数据库配置
builder.Services.AddSqlSugar();
#endregion

4.使用示例

public class AuthController
{
  private readonly DbRepository<object> _db;

  /// <summary>
  /// 构造函数注入
  /// </summary>
  /// <param name="db"></param>
  public AuthController(DbRepository<object> db)
  {
      _db = db;
  }

  /// <summary>
  /// 登录
  /// </summary>
  /// <returns></returns>
  [AllowAnonymous]
  [HttpPost]
  public async Task<IActionResult> Login([FromBody] LoginDto dto)
  {
      if (string.IsNullOrEmpty(dto.username))
      {
          return JsonView("登录账户不可为空");
      }
      else if (string.IsNullOrEmpty(dto.password))
      {
          return JsonView("登录密码不可为空");
      }
      else
      {
          //将密码进行Md5加密 方便对比
          var pass = dto.password.MDString().ToUpper();
          //查询是否存在该用户
          var res = await _db.Context.Queryable<UserInfoEntity>().Where(t => (t.AccountName == dto.username || t.RealName == dto.username) && t.Password == pass).FirstAsync();

          if (res != null)
          {
              //生成一个刷新令牌
              var refreshToken = CommonUtils.RefreshToken();
              //将刷新令牌存起来

              var entity = new UserInfoDto
              {
                  UserId = res.Id,
                  EnCode = res.EnCode,
                  UserName = res.RealName,
                  RoleId = res.RoleId,
                  Expires = DateTime.Now.AddHours(24)
                  //RefreshToken = refreshToken
              };

              #region 签发Jwt
              //用户信息
              var claims = new[]
              {
                  new Claim(ClaimsConst.UserId,res.Id),
                  new Claim(ClaimsConst.EnCode,res.EnCode),
                  new Claim(ClaimsConst.RealName,res.RealName),
                  new Claim(ClaimsConst.RoleId,res.RoleId)
              };

              //创建对称加密密钥
              var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(App.GetConfiguration()["JwtInfo:JwtSecurityKey"]));
              //创建签名凭据 并使用HmacSha256进行签名
              var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
              //创建Jwt令牌
              var token = new JwtSecurityToken(
                      issuer: App.GetConfiguration()["JwtInfo:Issuer"],
                      audience: App.GetConfiguration()["JwtInfo:Audience"],
                      claims: claims,
                      expires: entity.Expires,
                      signingCredentials: creds);
              //使用jwt令牌处理程序,将令牌写入字符串形式
              entity.AccessToken = new JwtSecurityTokenHandler().WriteToken(token);
              #endregion

              return JsonView(entity);
          }
          else
          {
              return JsonView("账号或密码错误,请确认后重试");
          }
      }
  }
}
posted @ 2024-01-24 16:30  将进酒丶杯莫停  阅读(260)  评论(2编辑  收藏  举报