.NET Core WebApi中实现数据库的操作(之SqlServer)_c# webapi数据库操作通用api_牛奶咖啡13的博客-CSDN博客

来源: .NET Core WebApi中实现数据库的操作(之SqlServer)_c# webapi数据库操作通用api_牛奶咖啡13的博客-CSDN博客

3.3.3、具体的业务逻辑
①设计业务的数据库表

 

②创建业务表的实体类

其中创建好私有的字段属性后,可以选中所有的私有字段,然后同时按下【Ctrl+R+E】键即可一次性自动生成所有字段的公有属性内容。

关于创建的实体中,如果你的属性名称与表中的字段不一致,则需要标识出来,具体内容请参考:从零开始 – 实体配置 – 《SQLSugar 5.0 文档》

/***
* Title:”.NET Core WebApi” 项目
* 主题:学生实体类
* Description:
* 功能:XXX
* Date:2021
* Version:0.1版本
* Author:Coffee
* Modify Recoder:
*/

using SQLSugar;
using System;
using System.Collections.Generic;
using System.Text;

namespace WebApiEntity
{
[SugarTable(“Test_Student”)]
public class StudentEntity
{

private int _iD = 0;
private string _name = string.Empty;
private string _number = string.Empty;
private int _age = 0;
private int _sex = 0;
private string _address = string.Empty;

/// <summary>
/// 主键
/// </summary>
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
public int ID { get => _iD; set => _iD = value; }

/// <summary>
/// 姓名
/// </summary>
public string Name { get => _name; set => _name = value; }

/// <summary>
/// 学号
/// </summary>
public string Number { get => _number; set => _number = value; }

/// <summary>
/// 年龄
/// </summary>
public int Age { get => _age; set => _age = value; }

/// <summary>
/// 性别
/// </summary>
public int Sex { get => _sex; set => _sex = value; }

/// <summary>
/// 家庭住址
/// </summary>
[SugarColumn(ColumnName = “Test_Address”)]
public string Address { get => _address; set => _address = value; }

}//Class_end

}
③创建业务的接口服务(方便业务扩展)

/***
* Title:”.NET Core WebApi” 项目
* 主题:学生服务接口
* Description:
* 功能:
*
* Date:2021
* Version:0.1版本
* Author:Coffee
* Modify Recoder:
*/

using System;
using System.Collections.Generic;
using System.Text;
using WebApiService.Common;
using WebApiEntity;

namespace WebApiService.Interfaces
{
public interface IStudentService : IBaseDbService<StudentEntity>
{
/// <summary>
/// 测试
/// </summary>
void Test();

}//Class_end

}
④实现业务服务

/***
* Title:”.NET Core WebApi” 项目
* 主题:学生服务
* Description:
* 功能:XXX
* Date:2021
* Version:0.1版本
* Author:Coffee
* Modify Recoder:
*/

using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;
using WebApiEntity;
using WebApiService.Common;
using WebApiService.Interfaces;
using WebApiUtils;

namespace WebApiService.Implements
{
class StudentService : BaseDbService<StudentEntity>, IStudentService
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name=”dbType”>数据类类型</param>
public StudentService(DbType dbType=DbType.SQLServer):base(dbType)
{

}

/// <summary>
/// 测试
/// </summary>
public void Test()
{
LogHelper.Debug($”this is { this.GetType().Name} 测试”);
}
}//Class_end

}
⑤实现具体的业务逻辑内容

/***
* Title:”.NET Core WebApi” 项目
* 主题:测试学生服务【数据库操作】
* Description:
* 功能:XXX
* Date:2021
* Version:0.1版本
* Author:Coffee
* Modify Recoder:
*/

using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WebApiEntity;
using WebApiService.Interfaces;
using WebApiUtils.Entity;

namespace WebApi_Learn.Controllers.Test
{
[Route(“api/[controller]/[action]”)]
[ApiController]
public class Test_Db_StudentService
{
#region 私有方法
//学生的接口服务
private readonly IStudentService _studentService;

#endregion

#region 公有方法

/// <summary>
/// 构造函数
/// </summary>
/// <param name=”studentService”>学生服务接口</param>
public Test_Db_StudentService(IStudentService studentService)
{
this._studentService = studentService;
}

/// <summary>
/// 插入数据
/// </summary>
/// <param name=”studentEntity”>学生实体</param>
/// <returns>返回当前学生的ID</returns>
[HttpPost, Route(“AddInfo”)]
public ActionResult<int> Insert(StudentEntity studentEntity)
{
int id = _studentService.Insert(studentEntity);

return id;
}

/// <summary>
/// 修改数据
/// </summary>
/// <param name=”studentEntity”>学生实体</param>
/// <returns>返回修改结果</returns>
[HttpPost, Route(“UpdateInfo”)]
public ActionResult<bool> Update(StudentEntity studentEntity)
{
return _studentService.Update(studentEntity);
}

/// <summary>
/// 删除数据(根据主键)
/// </summary>
/// <param name=”id”>主键</param>
/// <returns></returns>
[HttpGet]
public ActionResult<bool> Delete(int id)
{
return _studentService.Delete(id, true);
}

/// <summary>
/// 查询数据(单条数据)
/// </summary>
/// <param name=”field”>过滤字段</param>
/// <param name=”fieldValue”>过滤字段对应的值</param>
/// <returns></returns>
[HttpGet]
public ActionResult<StudentEntity> QuaryData(string field,int fieldValue)
{
StudentEntity studentEntity = new StudentEntity();

//显示字段
string strFields = “Name,Age,Test_Address”;

//根据条件查询到数据
SqlFilterEntity sqlFilterEntity = new SqlFilterEntity();
sqlFilterEntity.Append($”{field}=@{field}”);
sqlFilterEntity.Add(field,fieldValue);

studentEntity = _studentService.GetEntity(strFields, sqlFilterEntity);

return studentEntity;
}

/// <summary>
/// 查询数据(多条数据)
/// </summary>
/// <param name=”field”>字段</param>
/// <param name=”fieldValue”>字段对应的值</param>
/// <returns></returns>
[HttpGet]
public ActionResult<List<StudentEntity>> QuaryDatas(string field, string fieldValue)
{
List<StudentEntity> studentEntityList = new List<StudentEntity>();

//根据条件查询到数据
SqlFilterEntity sqlFilterEntity = new SqlFilterEntity();
sqlFilterEntity.Append($”{field}=@{field}”);
sqlFilterEntity.Add(field, fieldValue);
studentEntityList= _studentService.GetList(null, sqlFilterEntity);

return studentEntityList;
}

/// <summary>
/// 获取开始数据
/// </summary>
/// <returns></returns>
[HttpGet]
public List<StudentEntity> GetStartDatas()
{
List<StudentEntity> studentEntities = new List<StudentEntity>();
return _studentService.GetStartList(2);

}

/// <summary>
/// 分页查看
/// </summary>
/// <param name=”pageIndex”></param>
/// <param name=”pageSize”></param>
/// <param name=”strOrder”></param>
/// <returns></returns>
[HttpPost]
public List<StudentEntity> GetPageList(int pageIndex, int pageSize,
string strOrder=”Age DESC”)
{
//显示字段
string strField = “ID,Name,Age”;

//过滤条件
SqlFilterEntity sqlFilterEntity = new SqlFilterEntity();
sqlFilterEntity.Append($”Age>@Age”);
sqlFilterEntity.Add(“Age”, 21);

int totalCount=0;

return _studentService.GetPageList(pageIndex,pageSize, strField, sqlFilterEntity,strOrder,out totalCount);

}

#endregion

}//Class_end
}
3.3.4、服务的依赖注入
主要实现统一管理业务的接口与实现服务的对应关系。

/***
* Title:”.NET Core WebApi” 项目
* 主题:服务的依赖注入
* Description:
* 功能:XXX
* Date:2021
* Version:0.1版本
* Author:Coffee
* Modify Recoder:
*/

using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Text;
using WebApiService.Implements;
using WebApiService.Interfaces;

namespace WebApiService.Common.Depends
{
public class ServiceInjection
{
public static void ConfigureRepository(IServiceCollection services)
{
services.AddSingleton<IStudentService, StudentService>();

}

}//Class_end

}
对于依赖注入的简要补充如下所示:

方法 说明
Transient 每一次调用都会创建一个新的实例
Scoped 一个作用域中只实例化一个
Singleton 整个应用程序生命周期以内只创建一个实例
①在Startup类中【ConfigureServices】方法中注册【服务的依赖注入】

 

3.3.5、运行程序执行测试
比如:只显示【名称,年龄、地址】内容,查看条件是【字段为:ID;且该ID字段的值为:2021003165】信息:

 

 

参考文章:

①net core Webapi基础工程搭建(六)——数据库操作_Part 1

②net core Webapi基础工程搭建(六)——数据库操作_Part 2
————————————————
版权声明:本文为CSDN博主「牛奶咖啡13」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/xiaochenXIHUA/article/details/119574119

赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏