⚡ 优化代码生成数据库连接
This commit is contained in:
parent
5185a8c4d3
commit
b5f8034b26
@ -80,10 +80,7 @@ namespace Infrastructure
|
|||||||
|
|
||||||
public class Gen
|
public class Gen
|
||||||
{
|
{
|
||||||
public string Conn { get; set; }
|
|
||||||
public int DbType { get; set; }
|
|
||||||
public string Database { get; set; }
|
public string Database { get; set; }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public class DbConfigs
|
public class DbConfigs
|
||||||
@ -92,5 +89,10 @@ namespace Infrastructure
|
|||||||
public int DbType { get; set; }
|
public int DbType { get; set; }
|
||||||
public string ConfigId { get; set; }
|
public string ConfigId { get; set; }
|
||||||
public bool IsAutoCloseConnection { get; set; }
|
public bool IsAutoCloseConnection { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 是否代码生成使用库
|
||||||
|
/// </summary>
|
||||||
|
public bool IsGenerateDb { get; set; }
|
||||||
|
public string DbName { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,6 +2,7 @@
|
|||||||
using Infrastructure.Attribute;
|
using Infrastructure.Attribute;
|
||||||
using Infrastructure.Enums;
|
using Infrastructure.Enums;
|
||||||
using Infrastructure.Extensions;
|
using Infrastructure.Extensions;
|
||||||
|
using IP2Region.Ex.Models;
|
||||||
using Mapster;
|
using Mapster;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using SqlSugar;
|
using SqlSugar;
|
||||||
@ -230,8 +231,9 @@ namespace ZR.Admin.WebApi.Controllers
|
|||||||
throw new CustomException(ResultCode.CUSTOM_ERROR, "请求参数为空");
|
throw new CustomException(ResultCode.CUSTOM_ERROR, "请求参数为空");
|
||||||
}
|
}
|
||||||
var genTableInfo = GenTableService.GetGenTableInfo(dto.TableId);
|
var genTableInfo = GenTableService.GetGenTableInfo(dto.TableId);
|
||||||
|
var dbConfig = AppSettings.Get<List<DbConfigs>>("dbConfigs").FirstOrDefault(f => f.IsGenerateDb);
|
||||||
|
|
||||||
dto.DbType = AppSettings.GetAppConfig("gen:dbType", 0);
|
dto.DbType = dbConfig.DbType;
|
||||||
dto.GenTable = genTableInfo;
|
dto.GenTable = genTableInfo;
|
||||||
dto.IsPreview = true;
|
dto.IsPreview = true;
|
||||||
//生成代码
|
//生成代码
|
||||||
@ -255,8 +257,9 @@ namespace ZR.Admin.WebApi.Controllers
|
|||||||
throw new CustomException(ResultCode.CUSTOM_ERROR, "请求参数为空");
|
throw new CustomException(ResultCode.CUSTOM_ERROR, "请求参数为空");
|
||||||
}
|
}
|
||||||
var genTableInfo = GenTableService.GetGenTableInfo(dto.TableId);
|
var genTableInfo = GenTableService.GetGenTableInfo(dto.TableId);
|
||||||
|
var dbConfig = AppSettings.Get<List<DbConfigs>>("dbConfigs").FirstOrDefault(f => f.IsGenerateDb);
|
||||||
|
|
||||||
dto.DbType = AppSettings.GetAppConfig("gen:dbType", 0);
|
dto.DbType = dbConfig.DbType;
|
||||||
dto.GenTable = genTableInfo;
|
dto.GenTable = genTableInfo;
|
||||||
//自定义路径
|
//自定义路径
|
||||||
if (genTableInfo.GenType == "1")
|
if (genTableInfo.GenType == "1")
|
||||||
|
|||||||
@ -35,7 +35,7 @@ namespace ZR.Admin.WebApi.Extensions
|
|||||||
List<DbConfigs> dbConfigs = Configuration.GetSection("DbConfigs").Get<List<DbConfigs>>();
|
List<DbConfigs> dbConfigs = Configuration.GetSection("DbConfigs").Get<List<DbConfigs>>();
|
||||||
|
|
||||||
var iocList = new List<IocConfig>();
|
var iocList = new List<IocConfig>();
|
||||||
foreach (var item in dbConfigs)
|
foreach (var item in dbConfigs.FindAll(f => !f.IsGenerateDb))
|
||||||
{
|
{
|
||||||
iocList.Add(new IocConfig()
|
iocList.Add(new IocConfig()
|
||||||
{
|
{
|
||||||
|
|||||||
@ -12,6 +12,14 @@
|
|||||||
"DbType": 1, //数据库类型 MySql = 0, SqlServer = 1, Oracle = 3,PgSql = 4
|
"DbType": 1, //数据库类型 MySql = 0, SqlServer = 1, Oracle = 3,PgSql = 4
|
||||||
"ConfigId": "0", //多租户唯一标识
|
"ConfigId": "0", //多租户唯一标识
|
||||||
"IsAutoCloseConnection": true
|
"IsAutoCloseConnection": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Conn": "Data Source=LAPTOP-STKF2M8H\\SQLEXPRESS;User ID=admin;Password=admin123;Initial Catalog={dbName};",
|
||||||
|
"DbType": 1,
|
||||||
|
"ConfigId": "0",
|
||||||
|
"IsAutoCloseConnection": true,
|
||||||
|
"DbName": "ZrAdmin",//代码生成默认连接数据库
|
||||||
|
"IsGenerateDb": true //是否代码生成使用库,不要改动
|
||||||
}
|
}
|
||||||
//...下面添加更多的数据库源
|
//...下面添加更多的数据库源
|
||||||
],
|
],
|
||||||
@ -50,13 +58,10 @@
|
|||||||
},
|
},
|
||||||
//代码生成配置
|
//代码生成配置
|
||||||
"gen": {
|
"gen": {
|
||||||
"conn": "Data Source=LAPTOP-STKF2M8H\\SQLEXPRESS;User ID=admin;Password=admin123;Initial Catalog=ZrAdmin;",
|
|
||||||
"dbType": 1, //MySql = 0, SqlServer = 1
|
|
||||||
"autoPre": true, //自动去除表前缀
|
"autoPre": true, //自动去除表前缀
|
||||||
"author": "admin",
|
"author": "admin",
|
||||||
"tablePrefix": "sys_", //"表前缀(生成类名不会包含表前缀,多个用逗号分隔)",
|
"tablePrefix": "sys_", //"表前缀(生成类名不会包含表前缀,多个用逗号分隔)",
|
||||||
"vuePath": "", //前端代码存储路径eg:D:\Work\ZRAdmin-Vue3
|
"vuePath": "" //前端代码存储路径eg:D:\Work\ZRAdmin-Vue3
|
||||||
"oracle_db": ""
|
|
||||||
},
|
},
|
||||||
//邮箱配置信息
|
//邮箱配置信息
|
||||||
"MailOptions": {
|
"MailOptions": {
|
||||||
|
|||||||
@ -1,5 +1,4 @@
|
|||||||
using Infrastructure;
|
using Infrastructure;
|
||||||
using Infrastructure.Extensions;
|
|
||||||
using SqlSugar;
|
using SqlSugar;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
@ -21,27 +20,22 @@ namespace ZR.CodeGenerator
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public SqlSugarClient GetSugarDbContext(string dbName = "")
|
public SqlSugarClient GetSugarDbContext(string dbName = "")
|
||||||
{
|
{
|
||||||
Gen options = new();
|
List<DbConfigs> dbConfigs = AppSettings.Get<List<DbConfigs>>("dbConfigs");
|
||||||
AppSettings.Bind("gen", options);
|
|
||||||
string connStr = options.Conn;
|
DbConfigs configs = dbConfigs.Find(f => f.IsGenerateDb == true);
|
||||||
|
string connStr = configs.Conn;
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(dbName))
|
if (!string.IsNullOrEmpty(dbName))
|
||||||
{
|
{
|
||||||
string replaceStr = GetValue(options.Conn, "Database=", ";");
|
configs.DbName = dbName;
|
||||||
string replaceStr2 = GetValue(options.Conn, "Initial Catalog=", ";");
|
|
||||||
if (replaceStr.IsNotEmpty())
|
|
||||||
{
|
|
||||||
connStr = options.Conn.Replace(replaceStr, dbName, StringComparison.OrdinalIgnoreCase);
|
|
||||||
}
|
|
||||||
if (replaceStr2.IsNotEmpty())
|
|
||||||
{
|
|
||||||
connStr = options.Conn.Replace(replaceStr2, dbName, StringComparison.OrdinalIgnoreCase);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
connStr = connStr.Replace("{dbName}", configs.DbName, StringComparison.OrdinalIgnoreCase);
|
||||||
|
|
||||||
var db = new SqlSugarClient(new List<ConnectionConfig>()
|
var db = new SqlSugarClient(new List<ConnectionConfig>()
|
||||||
{
|
{
|
||||||
new ConnectionConfig(){
|
new ConnectionConfig(){
|
||||||
ConnectionString = connStr,
|
ConnectionString = connStr,
|
||||||
DbType = (DbType)options.DbType,
|
DbType = (DbType)configs.DbType,
|
||||||
IsAutoCloseConnection = true,//开启自动释放模式和EF原理一样
|
IsAutoCloseConnection = true,//开启自动释放模式和EF原理一样
|
||||||
InitKeyType = InitKeyType.Attribute,//从特性读取主键和自增列信息
|
InitKeyType = InitKeyType.Attribute,//从特性读取主键和自增列信息
|
||||||
},
|
},
|
||||||
|
|||||||
@ -7,12 +7,10 @@ namespace ZR.CodeGenerator
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class GenConstants
|
public class GenConstants
|
||||||
{
|
{
|
||||||
public static string Gen_conn = "gen:conn";
|
|
||||||
public static string Gen_conn_dbType = "gen:dbType";
|
public static string Gen_conn_dbType = "gen:dbType";
|
||||||
public static string Gen_author = "gen:author";
|
public static string Gen_author = "gen:author";
|
||||||
public static string Gen_autoPre = "gen:autoPre";
|
public static string Gen_autoPre = "gen:autoPre";
|
||||||
public static string Gen_tablePrefix = "gen:tablePrefix";
|
public static string Gen_tablePrefix = "gen:tablePrefix";
|
||||||
public static string Gen_oracle_db = "gen:oracle_db";
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// InputDto输入实体是不包含字段
|
/// InputDto输入实体是不包含字段
|
||||||
|
|||||||
@ -16,15 +16,15 @@ namespace ZR.CodeGenerator.Service
|
|||||||
{
|
{
|
||||||
var db = GetSugarDbContext();
|
var db = GetSugarDbContext();
|
||||||
//Oracle库特殊处理
|
//Oracle库特殊处理
|
||||||
var dbType = AppSettings.GetAppConfig(GenConstants.Gen_conn_dbType, 0);
|
List<DbConfigs> dbConfigs = AppSettings.Get<List<DbConfigs>>("dbConfigs");
|
||||||
if (dbType == 3)
|
DbConfigs configs = dbConfigs.Find(f => f.IsGenerateDb == true);
|
||||||
|
if (configs.DbType == 3)
|
||||||
{
|
{
|
||||||
var defaultDb = AppSettings.GetAppConfig(GenConstants.Gen_oracle_db, string.Empty);
|
return new List<string>() { configs?.DbName };
|
||||||
return new List<string>() { defaultDb };
|
|
||||||
}
|
}
|
||||||
var templist = db.DbMaintenance.GetDataBaseList(db);
|
var templist = db.DbMaintenance.GetDataBaseList(db);
|
||||||
|
|
||||||
return templist;
|
return templist.FindAll(f => !f.Contains("schema"));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user