Net8 ABP VNext集成FreeSql、SqlSugar

2024-03-11 22:20

本文主要是介绍Net8 ABP VNext集成FreeSql、SqlSugar,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

ABP可以快速搭建开发架构,但是内置的是EFCore,国内中小企业使用FreeSql与SqlSugar还是较多,为新手提供使用提供参考

ABP、FreeSql、SqlSugar参考地址:

ABP Framework | Open source web application framework for ASP.NET Core

指南 | FreeSql 官方文档

SqlSugar .Net ORM 5.X 官网 、文档、教程 - SqlSugar 5x - .NET果糖网

打开abp官网

复制到命令行,创建解决方案,创建后如图

打开创建的解决方案

修改数据库连接配置,运行,生成数据库

添加实体与IRepository接口,如图

生成实体表,方法此处省略,请自行到官网查看文档,或者参考我的笔记(下载链接在最上面)

用Navicat Premium 连接mysql

添加SqlSugarCore

添加FreeSql.All

具体原理请参考ABP官方Dapper 集成

添加MESFreeSqlModule、FreeSqlRepository、SqlSugarRepository、MESSqlSugarModule

代码

public class MESFreeSqlModule : AbpModule
{public override void ConfigureServices(ServiceConfigurationContext context){var configuration = context.Services.GetConfiguration();var connectionString = configuration.GetConnectionString("Default");           var freeSql = new FreeSql.FreeSqlBuilder().UseConnectionString(FreeSql.DataType.MySql, connectionString).Build();context.Services.AddSingleton<IFreeSql>(freeSql);}
}
  public abstract class FreeSqlRepository : DomainService{protected IFreeSql FreeSql => LazyServiceProvider.LazyGetRequiredService<IFreeSql>();private ICancellationTokenProvider CancellationTokenProvider =>LazyServiceProvider.LazyGetService<ICancellationTokenProvider>(NullCancellationTokenProvider.Instance);protected virtual CancellationToken GetCancellationToken(CancellationToken preferredValue = default){return CancellationTokenProvider.FallbackToProvider(preferredValue);}}
    public abstract class SqlSugarRepository : DomainService{protected ISqlSugarClient SugarClient => LazyServiceProvider.LazyGetRequiredService<ISqlSugarClient>();private ICancellationTokenProvider CancellationTokenProvider =>LazyServiceProvider.LazyGetService<ICancellationTokenProvider>(NullCancellationTokenProvider.Instance);protected virtual CancellationToken GetCancellationToken(CancellationToken preferredValue = default){return CancellationTokenProvider.FallbackToProvider(preferredValue);}}
public class MESSqlSugarModule : AbpModule
{public override void ConfigureServices(ServiceConfigurationContext context){var configuration = context.Services.GetConfiguration();var connectionString = configuration.GetConnectionString("Default"); context.Services.AddSingleton<ISqlSugarClient>(s =>{                SqlSugarScope sqlSugar = new SqlSugarScope(new ConnectionConfig(){DbType = SqlSugar.DbType.MySql,ConnectionString = connectionString,IsAutoCloseConnection = true,ConfigureExternalServices = new ConfigureExternalServices(){EntityService = (property, column) =>{var attributes = property.GetCustomAttributes(true);//get all attributes if (attributes.Any(it => it is KeyAttribute))// by attribute set primarykey{column.IsPrimarykey = true; //有哪些特性可以看 1.2 特性明细}//可以写多个,这边可以断点调试// if (attributes.Any(it => it is NotMappedAttribute))//{//    column.IsIgnore= true; //}},EntityNameService = (type, entity) =>{var attributes = type.GetCustomAttributes(true);if (attributes.Any(it => it is TableAttribute)){var attr = (attributes.First(it => it is TableAttribute) as TableAttribute);entity.DbTableName = attr.Name;}}}},db =>{                   db.Aop.OnLogExecuting = (sql, pars) =>{};});return sqlSugar;});}

上面为什么加ConfigureExternalServices这段代码,参考【实体配置】实体使用自定义特性,如下图

增加Repository数据仓库,实现数据读写

同一个接口不允许同时使用FreeSql与SqlSugar,可以右建先排除掉,方便测试

添加AppService业务层

在JC.AI.MES.HttpApi.Host注入MESSqlSugarModule

启动调试即可

没有基础的可以下载我的ABP Vnext敏捷开发笔记

这篇关于Net8 ABP VNext集成FreeSql、SqlSugar的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/799264

相关文章

SpringBoot集成Netty,Handler中@Autowired注解为空

最近建了个技术交流群,然后好多小伙伴都问关于Netty的问题,尤其今天的问题最特殊,功能大概是要在Netty接收消息时把数据写入数据库,那个小伙伴用的是 Spring Boot + MyBatis + Netty,所以就碰到了Handler中@Autowired注解为空的问题 参考了一些大神的博文,Spring Boot非controller使用@Autowired注解注入为null的问题,得到

vue项目集成CanvasEditor实现Word在线编辑器

CanvasEditor实现Word在线编辑器 官网文档:https://hufe.club/canvas-editor-docs/guide/schema.html 源码地址:https://github.com/Hufe921/canvas-editor 前提声明: 由于CanvasEditor目前不支持vue、react 等框架开箱即用版,所以需要我们去Git下载源码,拿到其中两个主

BD错误集锦8——在集成Spring MVC + MyBtis编写mapper文件时需要注意格式 You have an error in your SQL syntax

报错的文件 <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.yuan.dao.YuanUserDao"><!

BD错误集锦7——在集成Spring MVC + MyBtis时使用c3p0作为数据库时报错Method com/mchange/v2/c3p0/impl/NewProxyPreparedStatem

异常信息如下: Type Exception ReportMessage Handler dispatch failed; nested exception is java.lang.AbstractMethodError: Method com/mchange/v2/c3p0/impl/NewProxyPreparedStatement.isClosed()Z is abstractDescr

陀螺仪LSM6DSV16X与AI集成(8)----MotionFX库解析空间坐标

陀螺仪LSM6DSV16X与AI集成.8--MotionFX库解析空间坐标 概述视频教学样品申请源码下载开启CRC串口设置开启X-CUBE-MEMS1设置加速度和角速度量程速率选择设置FIFO速率设置FIFO时间戳批处理速率配置过滤链初始化定义MotionFX文件卡尔曼滤波算法主程序执行流程lsm6dsv16x_motion_fx_determin欧拉角简介演示 概述 本文将探讨

Spring 集成 RabbitMQ 与其概念,消息持久化,ACK机制

目录 RabbitMQ 概念exchange交换机机制 什么是交换机binding?Direct Exchange交换机Topic Exchange交换机Fanout Exchange交换机Header Exchange交换机RabbitMQ 的 Hello - Demo(springboot实现)RabbitMQ 的 Hello Demo(spring xml实现)RabbitMQ 在生产环境

springboot集成prometheus监控

1 Maven pom.xml引入依赖 <dependency><groupId>io.prometheus</groupId><artifactId>simpleclient_spring_boot</artifactId></dependency> 2 启动类引入注解 import io.prometheus.client.spring.boot.EnablePrometheusEnd

在vue项目中集成cesium

首先创建一个新的vue项目 安装vite中cesium插件 https://github.com/nshen/vite-plugin-cesium 安装插件 npm i cesium vite-plugin-cesium vite -D 配置插件 注释原有样式 修改代码 效果

web前端不可错过的开发工具–Adobe Brackets(开源、简洁强大的HTML、CSS和JavaScript集成开发环境)

Adobe Brackets是一个开源的基于HTML/CSS/JavaScript开发,运行在native shell上的集成开发环境。该项目由Adobe创建和维护,根据MIT许可证发布。提供Windows和OS X平台支持。 Brackets的特点是简约、快捷,没有很多的视图或者面板,它的核心目标是减少在开发过程中那些效率低下的重复性工作,例如浏览器刷新,修改元素的样式,搜索功能等等。

【jenkins1】gitlab与jenkins集成

文章目录 1.Jenkins-docker配置:运行在8080端口上,机器只要安装docker就能装载image并运行容器2.Jenkins与GitLab配置:docker ps查看正在运行,浏览器访问http://10....:8080/2.1 GitLab与Jenkins的Access Token配置:不涉及具体项目2.2 新建jenkins job和配置job:jenkins的Dash