连接数据库

parent 20798d4f
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Performance.Subsidy.Services; using Performance.Subsidy.Services;
using System; using System;
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Performance.Subsidy.Api.Controllers namespace Performance.Subsidy.Api.Controllers
...@@ -17,12 +19,16 @@ public class SubsidyController : ControllerBase ...@@ -17,12 +19,16 @@ public class SubsidyController : ControllerBase
_service = service; _service = service;
} }
// 绩效列表 /// <summary>
/// 绩效列表
/// </summary>
/// <returns></returns>
[HttpGet] [HttpGet]
[ProducesResponseType(typeof(IEnumerable<view_allot>), (int)HttpStatusCode.OK)]
public async Task<ApiResponse> GetAllot() public async Task<ApiResponse> GetAllot()
{ {
var allots = await _service.GetAllot(); var allots = await _service.GetAllot();
return new ApiResponse(ResponseType.OK, allots); return new ApiResponse(Status.Ok, allots);
} }
// 职称查询 // 职称查询
......
<Project Sdk="Microsoft.NET.Sdk.Web"> <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net5.0</TargetFramework> <TargetFramework>net5.0</TargetFramework>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
</ItemGroup> <PackageReference Include="Autofac" Version="6.2.0" />
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="7.1.0" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Performance.Subsidy.Services\Performance.Subsidy.Services.csproj" /> <ProjectReference Include="..\Performance.Subsidy.Services\Performance.Subsidy.Services.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>
using Autofac.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
...@@ -18,6 +19,7 @@ public static void Main(string[] args) ...@@ -18,6 +19,7 @@ public static void Main(string[] args)
public static IHostBuilder CreateHostBuilder(string[] args) => public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args) Host.CreateDefaultBuilder(args)
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureWebHostDefaults(webBuilder => .ConfigureWebHostDefaults(webBuilder =>
{ {
webBuilder.UseStartup<Startup>(); webBuilder.UseStartup<Startup>();
......
using Autofac;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
...@@ -18,6 +19,14 @@ public Startup(IConfiguration configuration) ...@@ -18,6 +19,14 @@ public Startup(IConfiguration configuration)
public IConfiguration Configuration { get; } 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. // This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services) public void ConfigureServices(IServiceCollection services)
{ {
...@@ -27,8 +36,6 @@ 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" }); 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. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
......
...@@ -6,5 +6,7 @@ ...@@ -6,5 +6,7 @@
"Microsoft.Hosting.Lifetime": "Information" "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 ...@@ -7,7 +7,7 @@ namespace Performance.Subsidy
public class ApiResponse<TEntity> public class ApiResponse<TEntity>
where TEntity : class, new() where TEntity : class, new()
{ {
public ResponseType State { get; set; } public Status State { get; set; }
/// <summary> /// <summary>
/// 消息内容。 /// 消息内容。
/// </summary> /// </summary>
...@@ -18,25 +18,25 @@ public class ApiResponse<TEntity> ...@@ -18,25 +18,25 @@ public class ApiResponse<TEntity>
public TEntity Data { get; set; } public TEntity Data { get; set; }
public ApiResponse() public ApiResponse()
: this(ResponseType.Fail, "", null) : this(Status.Fail, "", null)
{ {
} }
public ApiResponse(ResponseType type) public ApiResponse(Status type)
: this(type, type.ToString(), null) : this(type, type.ToString(), null)
{ {
} }
public ApiResponse(ResponseType type, string message) public ApiResponse(Status type, string message)
: this(type, message, null) : this(type, message, null)
{ {
} }
public ApiResponse(ResponseType type, TEntity entity) public ApiResponse(Status type, TEntity entity)
: this(type, type.ToString(), entity) : this(type, type.ToString(), entity)
{ {
} }
public ApiResponse(ResponseType type, string message, TEntity entity) public ApiResponse(Status type, string message, TEntity entity)
{ {
State = type; State = type;
Message = message; Message = message;
...@@ -50,19 +50,19 @@ public ApiResponse() ...@@ -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 @@ ...@@ -4,9 +4,9 @@
namespace Performance.Subsidy namespace Performance.Subsidy
{ {
public enum ResponseType public enum Status
{ {
OK = 1, Ok = 1,
Fail = 2, Fail = 2,
Error = 3, Error = 3,
TokenError = 4, TokenError = 4,
......
namespace Performance.Subsidy.Services 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 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 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> <PropertyGroup>
<TargetFramework>net5.0</TargetFramework> <TargetFramework>net5.0</TargetFramework>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Dapper" Version="2.0.90" /> <PackageReference Include="Dapper" Version="2.0.90" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="5.0.0" /> <PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="5.0.0" />
<PackageReference Include="MySql.Data" Version="8.0.24" /> <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" /> <PackageReference Include="System.Data.SqlClient" Version="4.8.2" />
</ItemGroup> </ItemGroup>
</Project> </Project>
...@@ -12,18 +12,36 @@ namespace Performance.Subsidy.Services.Repository ...@@ -12,18 +12,36 @@ namespace Performance.Subsidy.Services.Repository
{ {
public class ConnectionFactory 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) public IDbConnection Create(DbType type, string connectionString)
{ {
switch (type) try
{
switch (type)
{
case DbType.MySQL:
return new MySqlConnection(connectionString);
case DbType.MSSQL:
return new SqlConnection(connectionString);
case DbType.Oracle:
return new OracleConnection(connectionString);
default:
throw new ArgumentException("DbType类型不支持");
}
}
catch (Exception ex)
{ {
case DbType.MySQL: throw;
return new MySqlConnection(connectionString);
case DbType.MSSQL:
return new SqlConnection(connectionString);
case DbType.Oracle:
return new OracleConnection(connectionString);
default:
throw new ArgumentException("DbType类型不支持");
} }
} }
} }
......
...@@ -10,28 +10,23 @@ namespace Performance.Subsidy.Services ...@@ -10,28 +10,23 @@ namespace Performance.Subsidy.Services
public class SubsidyService public class SubsidyService
{ {
private readonly ConnectionFactory _factory; private readonly ConnectionFactory _factory;
private readonly IConfiguration _configuration;
private readonly int _commandTimeout; private readonly int _commandTimeout;
public SubsidyService( public SubsidyService(
ConnectionFactory factory, ConnectionFactory factory)
IConfiguration configuration)
{ {
_factory = factory; _factory = factory;
_configuration = configuration;
_commandTimeout = 60 * 5; _commandTimeout = 60 * 5;
} }
public async Task<IEnumerable<view_allot>> GetAllot() public async Task<IEnumerable<view_allot>> GetAllot()
{ {
var connectionString = _configuration.GetConnectionString("DefaultConnectionString"); return await _factory.CreateDefault().QueryAsync<view_allot>("SELECT * FROM view_allot");
return await _factory.Create(DbType.MySQL, connectionString).QueryAsync<view_allot>("SELECT * FROM view_allot");
} }
public async Task<IEnumerable<sub_jobtitle>> GetJobTitle(int allotId) public async Task<IEnumerable<sub_jobtitle>> GetJobTitle(int allotId)
{ {
var connectionString = _configuration.GetConnectionString("DefaultConnectionString"); return await _factory.CreateDefault().QueryAsync<sub_jobtitle>
return await _factory.Create(DbType.MySQL, connectionString).QueryAsync<sub_jobtitle>
("SELECT * FROM sub_jobtitle WHERE AllotID=@allotId", ("SELECT * FROM sub_jobtitle WHERE AllotID=@allotId",
new { allotId }, new { allotId },
commandTimeout: _commandTimeout); 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