连接数据库

parent 20798d4f
using Microsoft.AspNetCore.Mvc;
using Performance.Subsidy.Services;
using System;
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
namespace Performance.Subsidy.Api.Controllers
......@@ -17,12 +19,16 @@ public class SubsidyController : ControllerBase
_service = service;
}
// 绩效列表
/// <summary>
/// 绩效列表
/// </summary>
/// <returns></returns>
[HttpGet]
[ProducesResponseType(typeof(IEnumerable<view_allot>), (int)HttpStatusCode.OK)]
public async Task<ApiResponse> GetAllot()
{
var allots = await _service.GetAllot();
return new ApiResponse(ResponseType.OK, allots);
return new ApiResponse(Status.Ok, allots);
}
// 职称查询
......
......@@ -6,6 +6,8 @@
<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
<PackageReference Include="Autofac" Version="6.2.0" />
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="7.1.0" />
</ItemGroup>
<ItemGroup>
......
using Autofac.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
......@@ -18,6 +19,7 @@ public static void Main(string[] args)
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
......
using Autofac;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
......@@ -18,6 +19,14 @@ public Startup(IConfiguration configuration)
public IConfiguration Configuration { get; }
public void ConfigureContainer(ContainerBuilder builder)
{
var connectionString = Configuration.GetConnectionString("DefaultConnectionString");
builder.RegisterType<SubsidyService>().InstancePerLifetimeScope();
builder.RegisterInstance(new ConnectionFactory(connectionString)).SingleInstance();
}
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
......@@ -27,8 +36,6 @@ public void ConfigureServices(IServiceCollection services)
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Performance.Subsidy.Api", Version = "v1" });
});
services.AddScoped<SubsidyService>();
services.AddSingleton<ConnectionFactory>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
......
......@@ -6,5 +6,7 @@
"Microsoft.Hosting.Lifetime": "Information"
}
},
"DefaultConnectionString": "server=192.168.18.166;database=db_test_liutie;uid=root;pwd=1234qwer;pooling=true;charset=utf8;convert zero datetime=true;port=3306;connection timeout=120;max pool size=512;allow user variables=true;"
"ConnectionStrings": {
"DefaultConnectionString": "server=192.168.18.166;database=db_performance_subsidy;uid=root;pwd=1234qwer;pooling=true;charset=utf8;convert zero datetime=true;port=3306;connection timeout=120;max pool size=512;allow user variables=true;"
}
}
......@@ -7,7 +7,7 @@ namespace Performance.Subsidy
public class ApiResponse<TEntity>
where TEntity : class, new()
{
public ResponseType State { get; set; }
public Status State { get; set; }
/// <summary>
/// 消息内容。
/// </summary>
......@@ -18,25 +18,25 @@ public class ApiResponse<TEntity>
public TEntity Data { get; set; }
public ApiResponse()
: this(ResponseType.Fail, "", null)
: this(Status.Fail, "", null)
{
}
public ApiResponse(ResponseType type)
public ApiResponse(Status type)
: this(type, type.ToString(), null)
{
}
public ApiResponse(ResponseType type, string message)
public ApiResponse(Status type, string message)
: this(type, message, null)
{
}
public ApiResponse(ResponseType type, TEntity entity)
public ApiResponse(Status type, TEntity entity)
: this(type, type.ToString(), entity)
{
}
public ApiResponse(ResponseType type, string message, TEntity entity)
public ApiResponse(Status type, string message, TEntity entity)
{
State = type;
Message = message;
......@@ -50,19 +50,19 @@ public ApiResponse()
{
}
public ApiResponse(ResponseType type) : base(type)
public ApiResponse(Status type) : base(type)
{
}
public ApiResponse(ResponseType type, string message) : base(type, message)
public ApiResponse(Status type, string message) : base(type, message)
{
}
public ApiResponse(ResponseType type, object entity) : base(type, entity)
public ApiResponse(Status type, object entity) : base(type, entity)
{
}
public ApiResponse(ResponseType type, string message, object entity) : base(type, message, entity)
public ApiResponse(Status type, string message, object entity) : base(type, message, entity)
{
}
}
......
......@@ -4,9 +4,9 @@
namespace Performance.Subsidy
{
public enum ResponseType
public enum Status
{
OK = 1,
Ok = 1,
Fail = 2,
Error = 3,
TokenError = 4,
......
namespace Performance.Subsidy.Services
{
public class sub_jobtitle { }
public class sub_jobtitle
{
public int ID { get; set; }
public int AllotId { get; set; }
public string JobTitle { get; set; }
public decimal? BasicPerforFee { get; set; }
}
}
namespace Performance.Subsidy.Services
{
public class sub_subsidy { }
public class sub_subsidy
{
public int ID { get; set; }
public int AllotID { get; set; }
public string Department { get; set; }
public string PersonnelNumber { get; set; }
public string PersonnelName { get; set; }
public string JobTitle { get; set; }
public decimal? BasicPerforFee { get; set; }
public decimal? Attendance { get; set; }
public decimal? GiveAmount { get; set; }
public decimal? RealAmount { get; set; }
}
}
namespace Performance.Subsidy.Services
{
public class view_allot { }
public class view_allot
{
public int AllotId { get; set; }
public int HospitalId { get; set; }
public int Year { get; set; }
public int Month { get; set; }
}
}
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
......@@ -8,7 +8,7 @@
<PackageReference Include="Dapper" Version="2.0.90" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="5.0.0" />
<PackageReference Include="MySql.Data" Version="8.0.24" />
<PackageReference Include="Oracle.ManagedDataAccess" Version="19.11.0" />
<PackageReference Include="Oracle.ManagedDataAccess.Core" Version="3.21.1" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.2" />
</ItemGroup>
......
......@@ -12,8 +12,21 @@ namespace Performance.Subsidy.Services.Repository
{
public class ConnectionFactory
{
private readonly string _connectionString;
public ConnectionFactory(string connectionString)
{
_connectionString = connectionString;
}
public IDbConnection CreateDefault()
{
return new MySqlConnection(_connectionString);
}
public IDbConnection Create(DbType type, string connectionString)
{
try
{
switch (type)
{
case DbType.MySQL:
......@@ -26,5 +39,10 @@ public IDbConnection Create(DbType type, string connectionString)
throw new ArgumentException("DbType类型不支持");
}
}
catch (Exception ex)
{
throw;
}
}
}
}
......@@ -10,28 +10,23 @@ namespace Performance.Subsidy.Services
public class SubsidyService
{
private readonly ConnectionFactory _factory;
private readonly IConfiguration _configuration;
private readonly int _commandTimeout;
public SubsidyService(
ConnectionFactory factory,
IConfiguration configuration)
ConnectionFactory factory)
{
_factory = factory;
_configuration = configuration;
_commandTimeout = 60 * 5;
}
public async Task<IEnumerable<view_allot>> GetAllot()
{
var connectionString = _configuration.GetConnectionString("DefaultConnectionString");
return await _factory.Create(DbType.MySQL, connectionString).QueryAsync<view_allot>("SELECT * FROM view_allot");
return await _factory.CreateDefault().QueryAsync<view_allot>("SELECT * FROM view_allot");
}
public async Task<IEnumerable<sub_jobtitle>> GetJobTitle(int allotId)
{
var connectionString = _configuration.GetConnectionString("DefaultConnectionString");
return await _factory.Create(DbType.MySQL, connectionString).QueryAsync<sub_jobtitle>
return await _factory.CreateDefault().QueryAsync<sub_jobtitle>
("SELECT * FROM sub_jobtitle WHERE AllotID=@allotId",
new { allotId },
commandTimeout: _commandTimeout);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment