简化数据库操作
This commit is contained in:
parent
d503970db7
commit
8e24ac205c
@ -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>
|
||||
|
||||
@ -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")
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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, //数据库类型 MySql = 0, SqlServer = 1
|
||||
"urls": "http://localhost:8888", //项目启动url,如果改动端口前端对应devServer也需要进行修改
|
||||
"corsUrls": "http://localhost:8887", //跨域地址(前端启动项目,前后端分离单独部署需要设置),多个用","隔开
|
||||
"JwtSettings": {
|
||||
"Issuer": "ZRAdmin.NET",
|
||||
"Audience": "ZRAdmin.NET",
|
||||
"SecretKey": "SecretKey-ZRADMIN.NET-20210101",
|
||||
"Expire": 30//jwt登录过期时间(分)
|
||||
"Expire": 1440 //jwt登录过期时间(分)
|
||||
},
|
||||
"DemoMode": false, //是否演示模式
|
||||
"DbKey": "", //数据库加密key
|
||||
"Upload": {
|
||||
"UploadUrl": "http://localhost:8888"
|
||||
},
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user