• .net6 web api中使用SqlSugar(MySQL数据库)


    其中SqlSugar,也可以是EFcore,或者Dapper,或者其他ORM框架。

    其中mysql,也可以是SqlServer,或者oracle,或者其他数据库类型。

    1.首先使用vs2022建立.net6 web api

    2.增加SqlSugar和MySQL依赖项。

    Newtonsoft.Json是序列化

    3. 根据官网说明进行注入

    SqlSugar.IOC/依赖注入 - SqlSugar 5x - .NET果糖网

    1. using SqlSugar;
    2. namespace demoAPI.Db
    3. {
    4. public static class SqlsugarSetup
    5. {
    6. public static void AddSqlsugarSetup(this IServiceCollection services, IConfiguration configuration,
    7. string dbName = "ConnectString")
    8. {
    9. SqlSugarScope sqlSugar = new SqlSugarScope(new ConnectionConfig()
    10. {
    11. DbType = SqlSugar.DbType.MySql,
    12. ConnectionString = configuration[dbName],
    13. IsAutoCloseConnection = true,
    14. },
    15. db =>
    16. {
    17. //单例参数配置,所有上下文生效
    18. db.Aop.OnLogExecuting = (sql, pars) =>
    19. {
    20. Console.WriteLine(sql);//输出sql
    21. };
    22. //技巧:拿到非ORM注入对象
    23. //services.GetService<注入对象>();
    24. });
    25. services.AddSingleton(sqlSugar);//这边是SqlSugarScope用AddSingleton
    26. }
    27. }
    28. }

    注入操作

    builder.Services.AddSqlsugarSetup(builder.Configuration);

    其中ConnectString就是MySQL数据库的连接字符串

    "ConnectString": "Server=127.0.0.1;Port=3306;Database=test;Uid=root;Pwd=123456;"

    4. 建立实体类

    这个要和数据库的一致 

    注意: 建立实体类,可以使用DBfirst,也可以使用codefirst,也可以手动,最终只要有实体就行了。

    4. 建立测试控制类

    1. using demoAPI.Model;
    2. using Microsoft.AspNetCore.Mvc;
    3. using SqlSugar;
    4. namespace demoAPI.Controllers
    5. {
    6. [ApiController]
    7. [Route("api/[controller]/[action]")]
    8. public class testAPI : ControllerBase
    9. {
    10. private readonly ISqlSugarClient db;
    11. public testAPI(ISqlSugarClient db)
    12. {
    13. this.db = db;
    14. }
    15. [HttpGet]
    16. public void Get()
    17. {
    18. var a = db.Queryable().ToList();
    19. var b = db.Queryable().Where(a => a.id == "22").ToList();
    20. }
    21. }
    22. }

    5.运行代码,看结果

    点击第一个执行。 

    拓展:数据返回类型

    对webapi操作的时候,会有返回的数据,返回的数据各有不同,有集合,有单体,有数值,有字节流等等方式。也可以对他们进行统一的封装,进行标识,后面将会写,下面的代码,目前可以进行参考一下。

    1. using demoAPI.Model;
    2. using Microsoft.AspNetCore.Mvc;
    3. using SqlSugar;
    4. namespace demoAPI.Controllers
    5. {
    6. [ApiController]
    7. [Route("api/[controller]/[action]")]
    8. public class testAPI : ControllerBase
    9. {
    10. private readonly ISqlSugarClient db;
    11. public testAPI(ISqlSugarClient db)
    12. {
    13. this.db = db;
    14. }
    15. [HttpGet]
    16. public void Get()
    17. {
    18. var a = db.Queryable().ToList();
    19. var b = db.Queryable().Where(a => a.id == "22").ToList();
    20. }
    21. ///
    22. /// 返回所有数据
    23. ///
    24. ///
    25. [HttpGet]
    26. public async Task<ActionResult<IEnumerable<student>>> AAA()
    27. {
    28. return await db.Queryable().ToListAsync();
    29. }
    30. ///
    31. /// 返回单条数据
    32. ///
    33. ///
    34. [HttpGet("{id}")]
    35. public async Task>> AAA(string id)
    36. {
    37. var data = await db.Queryable().Where(s => s.id == id).ToListAsync();
    38. return data;
    39. }
    40. ///
    41. /// 返回体单个数值
    42. ///
    43. ///
    44. [HttpGet]
    45. public ActionResult<string> B( )
    46. {
    47. return "12231";
    48. }
    49. ///
    50. /// 返回状态码
    51. ///
    52. ///
    53. [HttpGet]
    54. public IActionResult B1()
    55. {
    56. return NotFound();
    57. }
    58. }
    59. }

    来源:.net6 web api中使用SqlSugar(MySQL数据库)_.net6 sqlsugar-CSDN博客

  • 相关阅读:
    【损失函数:3】感知损失:Perceptual Loss、总变分损失(TV Loss)(附Pytorch实现)
    IDEA创建Java Web项目
    从Zemax导入光学系统
    视频剪辑中生成花字特效的代码案例详述
    【目标检测算法】利用wandb可视化YOLO-V5模型的训练
    如何在 qmake(QtCreator)中指定 Mac 平台
    光伏功率异常数据识别与清洗(数据集,共262134条数据)
    kubernetes学习-概念3
    Navicat 在合规审计场景中的应用 | 附地方审计局的使用案例
    十二、虚拟 DOM 和 render() 函数(2)
  • 原文地址:https://blog.csdn.net/u012563853/article/details/128163569