Insql 国人开发,是一款汲取 Mybatis 优点的.NET ORM 框架。追求简单直观,使用自由灵活等特点。

项目主页:https://rainrcn.github.io/insql

此 ORM 是以 Mybatis 的 Sql 配置方式,以 Dapper 为对象映射的基础上建立。喜欢写 SQL 的同学们肯定会喜欢的。另外因为对象映射使用 Dapper 的关系,所以性能上不用过多担心。

创建项目

模板选择ApiWeb应用程序,如果会自己大家结构选择也是可以的。

在项目上鼠标右键选择管理Nuget程序包,搜索Insql并添加安装,Insql 包自带 SqlServer 数据库连接,如果需要 MySql 数据库,需要另外安装Insql.MySql

使用

打开Startup.cs,在ConfigureServices中加入AddInsql

public void ConfigureServices(IServiceCollection services)
{
services.AddInsql(); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}

Insql 就已经可以开始用了。

在项目下创建Domain目录,并创建UserDbContext.cs UserDbContext.insql.xml UserPo.cs RolePo.cs 文件

UserDbContext.insql.xml 要右键属性选择嵌入式资源

写代码

1. 创建数据库模型类 UserPo.cs RolePo.cs

public class UserPo
{
public string UserId { get; set; } public string UserName { get; set; } public DateTime CreateTime { get; set; }
} public class RolePo
{
public string RoleCode { get; set; } public string RoleName { get; set; } public int RoleOrder { get; set; }
}

2. 创建UseDbContext.insql.xmlSQL 配置

<insql type="InsqlExample.Domain.Context.UserDbContext,InsqlExample" >

  <!--定义UserPo类型数据库字段到对象属性映射-->
<map type="InsqlExample.Domain.Model.UserPo,InsqlExample">
<key name="user_id" to="UserId" />
<column name="user_name" to="UserName" />
<column name="create_time" to="CreateTime" />
</map> <map type="InsqlExample.Domain.Model.RolePo,InsqlExample">
<key name="role_code" to="RoleCode" />
<column name="role_name" to="RoleName" />
<column name="role_order" to="RoleOrder" />
</map> <select id="GetUser">
select * from user_info where user_id = @userId
</select> <insert id="InsertUser">
insert into user_info (user_id,user_name,create_time) value (@UserId,@UserName,@CreateTime)
</insert> <update id="UpdateUser">
update user_info
<set>
<if test="UserName != null">
user_name = @UserName,
</if>
</set>
where user_id = @UserId
</update> <delete id="DeleteUser">
delete from user_info where user_id = @userId
</delete> <select id="GetRoleList">
select * from role_info order by role_order
</select>
</insql>

select,insert,update,delete 分别代表增删改查,可以看到在update中有特殊 xml 元素,可以进项目文档查看详细说明,有 Mybatis 经验的同学自然就理解了

3. 创建UserDbContext数据上下文

public class UserDbContext : DbContext
{
public UserDbContext(DbContextOptions<UserDbContext> options) : base(options)
{
} public UserPo GetUser(string userId)
{
//"GetUser"对应 select上的id,
//第二个查询参数支持 PlainObject和 IDictionary<string,object>两种类型
return this.Query<UserPo>("GetUser", new { userId }).SingleOrDefault();
} public void InsertUser(UserPo user)
{
this.Execute(nameof(InsertUser), user);
} public void UpdateUser(UserPo user)
{
this.Execute(nameof(UpdateUser), user);
} public void DeleteUser(string userId)
{
this.Execute(nameof(DeleteUser), new { userId });
} public IEnumerable<RolePo> GetRoleList()
{
return this.Query<RolePo>("GetRoleList");
}
}

别忘了在Startup.cs中注册 UserDbContext。 命名空间 using Insql;一下

public void ConfigureServices(IServiceCollection services)
{
services.AddInsql(); services.AddInsqlDbContext<UserDbContext>(options =>
{
//这里代表这个上下文使用这个SqlServer数据库
options.UseSqlServer("这里是连接字符串");
}); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}

增删改查这就 OK 了。然后我们可以在 Controller 或者 Service 中直接注入 UserDbContext 来用。

4. 在ValuesController.cs中使用UserDbContext

[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
private readonly UserDbContext dbContext; public ValuesController(UserDbContext dbContext)
{
this.dbContext = dbContext;
} [HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
//查询用户
var user1 = this.dbContext.GetUser("tome"); //增加用户
this.dbContext.InsertUser(new UserPo
{
UserId = Guid.NewGuid().ToString(),
UserName = "tom",
CreateTime = DateTime.Now
}); //查询角色列表
var roleList = this.dbContext.GetRoleList(); //....其他的不演示了 //还可以这样用,通过dbContext直接调用sql,和在DbContext里面写方法一样的
var userJerry = this.dbContext.Query<UserPo>("GetUser", new { userId = "jerry" }); return new string[] { "value1", "value2" };
}
}

行这就完事了。

可以去看看项目文档,支持功能还挺多的。代码生成器也有。

轻量级.NET CORE ORM框架Insql使用教程的更多相关文章

  1. OsharpNS轻量级.net core快速开发框架简明入门教程-从零开始启动Osharp

    OsharpNS轻量级.net core快速开发框架简明入门教程 教程目录 从零开始启动Osharp 1.1. 使用OsharpNS项目模板创建项目 1.2. 配置数据库连接串并启动项目 1.3. O ...

  2. OsharpNS轻量级.net core快速开发框架简明入门教程-Osharp.Redis使用

    OsharpNS轻量级.net core快速开发框架简明入门教程 教程目录 从零开始启动Osharp 1.1. 使用OsharpNS项目模板创建项目 1.2. 配置数据库连接串并启动项目 1.3. O ...

  3. OsharpNS轻量级.net core快速开发框架简明入门教程-代码生成器的使用

    OsharpNS轻量级.net core快速开发框架简明入门教程 教程目录 从零开始启动Osharp 1.1. 使用OsharpNS项目模板创建项目 1.2. 配置数据库连接串并启动项目 1.3. O ...

  4. OsharpNS轻量级.net core快速开发框架简明入门教程-基于Osharp实现自己的业务功能

    OsharpNS轻量级.net core快速开发框架简明入门教程 教程目录 从零开始启动Osharp 1.1. 使用OsharpNS项目模板创建项目 1.2. 配置数据库连接串并启动项目 1.3. O ...

  5. OsharpNS轻量级.net core快速开发框架简明入门教程-Osharp.Hangfire使用

    OsharpNS轻量级.net core快速开发框架简明入门教程 教程目录 从零开始启动Osharp 1.1. 使用OsharpNS项目模板创建项目 1.2. 配置数据库连接串并启动项目 1.3. O ...

  6. OsharpNS轻量级.net core快速开发框架简明入门教程-Osharp.Permissions使用

    OsharpNS轻量级.net core快速开发框架简明入门教程 教程目录 从零开始启动Osharp 1.1. 使用OsharpNS项目模板创建项目 1.2. 配置数据库连接串并启动项目 1.3. O ...

  7. OsharpNS轻量级.net core快速开发框架简明入门教程-切换数据库(从SqlServer改为MySql)

    OsharpNS轻量级.net core快速开发框架简明入门教程 教程目录 从零开始启动Osharp 1.1. 使用OsharpNS项目模板创建项目 1.2. 配置数据库连接串并启动项目 1.3. O ...

  8. OsharpNS轻量级.net core快速开发框架简明入门教程-多上下文配置(多个数据库的使用)

    OsharpNS轻量级.net core快速开发框架简明入门教程 教程目录 从零开始启动Osharp 1.1. 使用OsharpNS项目模板创建项目 1.2. 配置数据库连接串并启动项目 1.3. O ...

  9. PetaPoco - 轻量级高性能的ORM框架(支持.NET Core)

    我们都知道ORM全称叫做Object Relationship Mapper,也就是可以用object来map我们的db. 而且市面上的orm框架有很多,有重量级的Entity Framework,有 ...

随机推荐

  1. spring mvc+mybatis 构建 cms + 实现UC浏览器文章功能

    最近公司在模拟UC浏览器做一个简单的cms系统,主要针对于企业内部的文章浏览需求,这边考虑用户大多用mobile浏览文章内容,故使用原生的ios和android进行开发,后面也会集成html5. 1. ...

  2. [Python] Window机器上同时安装Python 2 和 Python 3,如何兼容切换使用?

    不论python2还是python3,python可执行文件都叫python.exe,在cmd下输入python得到的版本号取决于环境变量里哪个版本的python路径更靠前. 切换的方法有3种(方法3 ...

  3. Apache Beam是什么?

    Apache Beam 的前世今生 1月10日,Apache软件基金会宣布,Apache Beam成功孵化,成为该基金会的一个新的顶级项目,基于Apache V2许可证开源. 2003年,谷歌发布了著 ...

  4. CPU与IRP的一些相关函数

    VOID KiAdjustIrpCredits ( VOID )其中 Number = KeNumberProcessors;Prcb = KiProcessorBlock[Index]; 多核情况下 ...

  5. C#中 property 与 attribute的区别

    说的通俗些Attribute是类,不过是一类比较特殊的类,Attribute必须写在一对方括号中,用来处理.NET中多种问题:序列化.程序的安全特征等等,在.NET中的作用非同凡响 Attribute ...

  6. centos7.2 下 nginx 开机启动

    1.在系统服务目录里创建nginx.service文件 1 vi /lib/systemd/system/nginx.service 内容如下 1 2 3 4 5 6 7 8 9 10 11 12 1 ...

  7. vue全局路由守卫beforeEach

    在main.js里使用方法 router.beforeEach((to,from,next)=>{}) to,是将要跳转的路由, from,是离开的路由 next是个方法,判断to.path 或 ...

  8. 前端之js

    简介:     JavaScript是运行在浏览器端的脚步语言,JavaScript主要解决的是前端与用户交互的问题,包括使用交互与数据交互,JavaScript是浏览器解释执行的 前端三大块    ...

  9. Python之路(一)-python简介

    一.python简介,python2.x与python3.x的区别 Python是著名的“龟叔”Guido van Rossum在1989年圣诞节期间,为了打发无聊的圣诞节而编写的一个编程语言. Py ...

  10. Docker环境安装与配置

    Docker 简介 Docker使用Go语言编写的 安装Docker推荐LInux内核在3.10上 在2.6内核下运行较卡(CentOS 7.X以上内核是3.10) Docker 安装 安装yum-u ...