简化数据库操作

This commit is contained in:
不做码农
2022-05-12 21:28:27 +08:00
parent d503970db7
commit 8e24ac205c
5 changed files with 31 additions and 46 deletions

View File

@@ -6,13 +6,6 @@ namespace Infrastructure
/// </summary>
public class OptionsSetting
{
public static string ConnAdmin = "conn_zrAdmin";
public static string ConnDbType = "conn_zrAdmin_type";
public static string ConnBus = "conn_bus";
public static string ConnBusDbType = "conn_bus_type";
public string Redis { get; set; }
public string Database { get; set; }
/// <summary>
/// 是否演示模式
/// </summary>
@@ -37,7 +30,6 @@ namespace Infrastructure
/// </summary>
public class Upload
{
public string UploadDirectory { get; set; }
public string UploadUrl { get; set; }
}
/// <summary>

View File

@@ -237,7 +237,7 @@ namespace ZR.Admin.WebApi.Controllers
var genTableInfo = GenTableService.GetGenTableInfo(dto.TableId);
genTableInfo.Columns = GenTableColumnService.GenTableColumns(dto.TableId);
dto.DbType = AppSettings.GetAppConfig(OptionsSetting.ConnBusDbType, 0);
dto.DbType = AppSettings.GetAppConfig("gen:dbType", 0);
dto.GenTable = genTableInfo;
dto.IsPreview = true;
//生成代码
@@ -263,7 +263,7 @@ namespace ZR.Admin.WebApi.Controllers
var genTableInfo = GenTableService.GetGenTableInfo(dto.TableId);
genTableInfo.Columns = GenTableColumnService.GenTableColumns(dto.TableId);
dto.DbType = AppSettings.GetAppConfig(OptionsSetting.ConnBusDbType, 0);
dto.DbType = AppSettings.GetAppConfig("gen:dbType", 0);
dto.GenTable = genTableInfo;
//自定义路径
if (genTableInfo.GenType == "1")

View File

@@ -26,43 +26,43 @@ namespace ZR.Admin.WebApi.Extensions
public static void AddDb(IConfiguration Configuration)
{
string connStr = Configuration.GetConnectionString(OptionsSetting.ConnAdmin);
string connStrBus = Configuration.GetConnectionString(OptionsSetting.ConnBus);
int dbType = Convert.ToInt32(Configuration[OptionsSetting.ConnDbType]);
int dbType_bus = Convert.ToInt32(Configuration[OptionsSetting.ConnBusDbType]);
string connStr = Configuration.GetConnectionString("conn_db");
int dbType = Convert.ToInt32(Configuration["conn_db_dbtype"]);
SugarIocServices.AddSqlSugar(new List<IocConfig>() {
new IocConfig() {
ConfigId = "0",
ConfigId = "0",//默认db
ConnectionString = connStr,
DbType = (IocDbType)dbType,
IsAutoCloseConnection = true
}, new IocConfig() {
},
new IocConfig() {
ConfigId = "1",
ConnectionString = connStrBus,
DbType = (IocDbType)dbType_bus,
ConnectionString = "替换成你的字符串",
DbType = IocDbType.MySql,
IsAutoCloseConnection = true
}
//...增加其他数据库
});
SugarIocServices.ConfigurationSugar(db =>
{
//db0数据过滤
FilterData(0);
//FilterData(1);
#region db0
db.GetConnection(0).Aop.OnLogExecuting = (sql, pars) =>
db.GetConnectionScope(0).Aop.OnLogExecuting = (sql, pars) =>
{
var param = db.GetConnection(0).Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value));
var param = db.GetConnectionScope(0).Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value));
logger.Info($"【sql语句】{sql}{param}\n");
};
db.GetConnection(0).Aop.OnError = (e) =>
db.GetConnectionScope(0).Aop.OnError = (e) =>
{
logger.Error(e, $"执行SQL出错{e.Message}");
};
//SQL执行完
db.GetConnection(0).Aop.OnLogExecuted = (sql, pars) =>
db.GetConnectionScope(0).Aop.OnLogExecuted = (sql, pars) =>
{
//执行完了可以输出SQL执行时间 (OnLogExecutedDelegate)
};
@@ -86,7 +86,7 @@ namespace ZR.Admin.WebApi.Extensions
}
/// <summary>
/// 分页获取count 不会追加sql
/// 数据过滤
/// </summary>
/// <param name="configId">多库id</param>
private static void FilterData(int configId)
@@ -98,7 +98,7 @@ namespace ZR.Admin.WebApi.Extensions
if (user == null) return;
//管理员不过滤
if (user.RoleIds.Any(f => f.Equals("admin"))) return;
var db = DbScoped.SugarScope.GetConnection(configId);
var db = DbScoped.SugarScope.GetConnectionScope(configId);
foreach (var role in user.Roles.OrderBy(f => f.DataScope))
{
string dataScope = role.DataScope;

View File

@@ -7,21 +7,18 @@
}
},
"ConnectionStrings": {
"conn_zrAdmin": "server=LAPTOP-STKF2M8H\\SQLEXPRESS;uid=sa;pwd=zradmin123;database=ZrAdmin;Trusted_Connection=SSPI",
"conn_bus": "server=LAPTOP-STKF2M8H\\SQLEXPRESS;uid=zr;pwd=zradmin123;database=ZrAdmin;Trusted_Connection=SSPI"
"conn_db": "server=LAPTOP-STKF2M8H\\SQLEXPRESS;uid=sa;pwd=zradmin123;database=ZrAdmin;Trusted_Connection=SSPI"
},
"conn_zrAdmin_type": 1, //MySql = 0, SqlServer = 1
"conn_bus_type": 1,
"conn_db_dbtype": 1, //<EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> MySql = 0, SqlServer = 1
"urls": "http://localhost:8888", //<2F><>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>url<72><6C><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ķ<EFBFBD><C4B6>˿<EFBFBD>ǰ<EFBFBD>˶<EFBFBD>ӦdevServerҲ<72><D2B2>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD>޸<EFBFBD>
"corsUrls": "http://localhost:8887", //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF>ǰ<EFBFBD><C7B0><EFBFBD>˷<EFBFBD><CBB7><EFBFBD><EBB5A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD>ã<EFBFBD><C3A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>","<22><><EFBFBD><EFBFBD>
"JwtSettings": {
"Issuer": "ZRAdmin.NET",
"Audience": "ZRAdmin.NET",
"SecretKey": "SecretKey-ZRADMIN.NET-20210101",
"Expire": 30//jwt<77><74>¼<EFBFBD><C2BC><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>֣<EFBFBD>
"Expire": 1440 //jwt<77><74>¼<EFBFBD><C2BC><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>֣<EFBFBD>
},
"DemoMode": false, //<2F>Ƿ<EFBFBD><C7B7><EFBFBD>ʾģʽ
"DbKey": "", //<2F><><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD><EFBFBD>key
"Upload": {
"UploadUrl": "http://localhost:8888"
},

View File

@@ -4,6 +4,7 @@ using System;
using System.Collections.Generic;
using System.Data;
using System.Linq.Expressions;
using System.Reflection;
using ZR.Model;
namespace ZR.Repository
@@ -18,22 +19,17 @@ namespace ZR.Repository
public BaseRepository(ISqlSugarClient context = null) : base(context)
{
//通过特性拿到ConfigId
//var configId = typeof(T).GetCustomAttribute<TenantAttribute>()?.configId;
//if (configId != null)
//{
// itenant = DbScoped.SugarScope;//设置租户接口
// Context = DbScoped.SugarScope.GetConnection(configId);
//}
//else
//{
// Context = context ?? DbScoped.SugarScope.GetConnection(1);//根据类传入的ConfigId自动选择
//}
Context = DbScoped.SugarScope.GetConnectionWithAttr<T>();
itenant = DbScoped.SugarScope;//设置租户接口
if (Context == null)
var configId = typeof(T).GetCustomAttribute<TenantAttribute>()?.configId;
if (configId != null)
{
Context = DbScoped.SugarScope.GetConnection(1);//根据类传入的ConfigId自动选择
Context = DbScoped.SugarScope.GetConnectionScope(configId);//根据类传入的ConfigId自动选择
}
else
{
Context = context ?? DbScoped.SugarScope.GetConnectionScope(0);//没有默认db0
}
//Context = DbScoped.SugarScope.GetConnectionScopeWithAttr<T>();
itenant = DbScoped.SugarScope;//设置租户接口
}
#region add