轻量级.NET CORE ORM框架Insql使用教程

时间:2023-03-09 00:47:24
轻量级.NET CORE ORM框架Insql使用教程

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

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

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

创建项目

轻量级.NET CORE ORM框架Insql使用教程

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

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

轻量级.NET CORE ORM框架Insql使用教程

使用

打开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 文件

轻量级.NET CORE ORM框架Insql使用教程

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

轻量级.NET CORE ORM框架Insql使用教程

写代码

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" };
}
}

行这就完事了。

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