轻量级.NET CORE ORM框架Insql使用教程
发布时间:2019-02-27 14:46,
浏览次数:560
, 标签:
NET
CORE
ORM
Insql
Insql 国人开发,是一款汲取 Mybatis 优点的.NET ORM 框架。追求简单直观,使用自由灵活等特点。
项目主页:https://rainrcn.github.io/insql
此 ORM 是以 Mybatis 的 Sql 配置方式,以 Dapper 为对象映射的基础上建立。喜欢写 SQL 的同学们肯定会喜欢的。另外因为对象映射使用
Dapper 的关系,所以性能上不用过多担心。
创建项目
模板选择Api或Web应用程序,如果会自己大家结构选择空也是可以的。
在项目上鼠标右键选择管理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 配置
type="InsqlExample.Domain.Model.UserPo,InsqlExample">
to="UserId" />
name="create_time" to="CreateTime" />
type="InsqlExample.Domain.Model.RolePo,InsqlExample">
to="RoleCode" />
name="role_order" to="RoleOrder" />
select * fromuser_info where user_id = @userId insert
into user_info (user_id,user_name,create_time) value
(@UserId,@UserName,@CreateTime) update
user_info user_name = @UserName,
where user_id = @UserId delete from
user_info where user_id = @userId select *
from role_info order by role_order
select,insert,update,delete 分别代表增删改查,可以看到在update中有特殊 xml 元素,可以进项目文档查看详细说明,有
Mybatis 经验的同学自然就理解了
3. 创建UserDbContext数据上下文
public class UserDbContext : DbContext { public
UserDbContext(DbContextOptions options) : base(options) { }
public UserPo GetUser(string userId) { //"GetUser"对应 select上的id, //第二个查询参数支持
PlainObject和 IDictionary两种类型 return
this.Query("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 GetRoleList() { return
this.Query("GetRoleList"); } }
别忘了在Startup.cs中注册 UserDbContext。 命名空间 using Insql;一下
public void ConfigureServices(IServiceCollection services) {
services.AddInsql(); services.AddInsqlDbContext(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> 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("GetUser", new { userId = "jerry" }); return new
string[] { "value1", "value2" }; } }
行这就完事了。
可以去看看项目文档,支持功能还挺多的。代码生成器也有。