SmartSql 快速使用指南(https://github.com/Ahoo-Wang/SmartSql)

ISmartSqlMapper 常用(部分)接口概述

函数 说明
Execute IDbCommand.ExecuteNonQuery,执行返回受影响行数
ExecuteScalar IDbCommand.ExecuteScalar,执行并返回查询返回的ReultSet中第一行的第一列
Query 执行返回实体列表
QuerySingle 执行返回单个实体
GetDataTable 执行返回DataTable
GetDataSet 执行返回DataSet
BeginTransaction 开启事务
CommitTransaction 提交事务
RollbackTransaction 回滚事务

新增

Statement

    <Statement Id="Insert">
INSERT INTO T_User
(UserName
,Password
,Status
,LastLoginTime
,CreationTime)
VALUES
(?UserName
,?Password
,?Status
,?LastLoginTime
,?CreationTime)
;Select Last_Insert_Id();
</Statement>

返回主键


ISmartSqlMapper SqlMapper = MapperContainer.Instance.GetSqlMapper();
long userId = _smartSqlMapper.ExecuteScalar<long>(new RequestContext
{
Scope = "User",
SqlId = "Insert",
Request = new User
{
UserName = request.UserName,
Pwd = request.Pwd,
Status = Entitiy.UserStatus.Ok,
CreationTime = DateTime.Now,
}
});

新增返回受影响行数

            SqlMapper.Execute(new RequestContext
{
Scope = "User",
SqlId = "Insert",
Request = new User
{
UserName = request.UserName,
Pwd = request.Pwd,
Status = Entitiy.UserStatus.Ok,
CreationTime = DateTime.Now,
}
});

删除

    <Statement Id="Delete">
Delete FROM T_User
Where Id=?Id
</Statement>
            SqlMapper.Execute(new RequestContext
{
Scope = "User",
SqlId = "Delete",
Request = new { Id = 3 }
});

更新

Statement.Update

<Statement Id="Update">
UPDATE T_User
<Set>
<IsProperty Prepend="," Property="UserName">
UserName = ?UserName
</IsProperty>
<IsProperty Prepend="," Property="Password">
Password = ?Password
</IsProperty>
<IsProperty Prepend="," Property="Status">
Status = ?Status
</IsProperty>
<IsProperty Prepend="," Property="LastLoginTime">
LastLoginTime = ?LastLoginTime
</IsProperty>
<IsProperty Prepend="," Property="CreationTime">
CreationTime = ?CreationTime
</IsProperty>
</Set>
Where Id=?Id
</Statement>

全量更新

            SqlMapper.Execute(new RequestContext
{
Scope = "User",
SqlId = "Update",
Request = new User
{
Id=1,
UserName = request.UserName,
Pwd = request.Pwd,
Status = Entitiy.UserStatus.Ok,
CreationTime = DateTime.Now,
}
});

局部更新

            SqlMapper.Execute(new RequestContext
{
Scope = "User",
SqlId = "Update",
Request = new { Id=1 , Pwd = "SmartSql" }
});

查询 返回List

<Statement Id="Query">
SELECT T.* From T_User T
<Where>
<IsNotEmpty Prepend="And" Property="EqUserName">
T.UserName=$EqUserName
</IsNotEmpty>
<IsNotEmpty Prepend="And" Property="UserName">
T.UserName Like Concat('%',$UserName,'%')
</IsNotEmpty>
</Where>
<Switch Prepend="Order By" Property="OrderBy">
<Default>
T.Id Desc
</Default>
</Switch>
<IsNotEmpty Prepend="Limit" Property="Taken">?Taken</IsNotEmpty>
</Statement>
            var list = SqlMapper.Query<User>(new RequestContext
{
Scope = "User",
SqlId = "Query",
Request = new
{
Taken = 10
}
});

查询 返回单个实体

    <Statement Id="GetEntity">
Select T.* From T_User T
<Where>
<IsNotEmpty Prepend="And" Property="Id">
T.Id=?Id
</IsNotEmpty>
</Where>
Limit 1
</Statement>
            var user = SqlMapper.QuerySingle<User>(new RequestContext
{
Scope = "User",
SqlId = "GetEntity",
Request = new { Id = 1 }
});

事务

            try
{
SqlMapper.BeginTransaction();
//BizCode();
SqlMapper.CommitTransaction();
}
catch (Exception ex)
{
SqlMapper.RollbackTransaction();
throw ex;
}

存储过程

            DbParameterCollection dbParameterCollection = new DbParameterCollection();
dbParameterCollection.Add(new DbParameter
{
Name = "Total",
DbType = System.Data.DbType.Int32,
Direction = System.Data.ParameterDirection.Output
});
RequestContext context = new RequestContext
{
CommandType = System.Data.CommandType.StoredProcedure,
RealSql = "SP_QueryByPage",
Request = dbParameterCollection
};
var list = SqlMapper.Query<User>(context);
var total = dbParameterCollection.GetValue<int>("Total");

SmartSql 快速使用指南的更多相关文章

  1. Rancher 快速上手指南操作(1)

    Rancher 快速上手指南操作(1)该指南知道用户如何快速的部署Rancher Server 管理容器.前提是假设你的机器已经安装好docker了.1 确认 docker 的版本,下面是 ubunt ...

  2. AngularJS快速入门指南20:快速参考

    thead>tr>th, table.reference>tbody>tr>th, table.reference>tfoot>tr>th, table ...

  3. AngularJS快速入门指南19:示例代码

    本文给出的大部分示例都可以直接运行,通过点击运行按钮来查看结果,同时支持在线编辑代码. <div ng-app=""> <p>Name: <input ...

  4. AngularJS快速入门指南18:Application

    是时候创建一个真正的AngularJS单页面应用程序了(SPA). 一个AngularJS应用程序示例 你已经了解了足够多的内容来创建第一个AngularJS应用程序: My Note Save Cl ...

  5. AngularJS快速入门指南17:Includes

    使用AngularJS,你可以在HTML中包含其它的HTML文件. 在HTML中包含其它HTML文件? 当前的HTML文档还不支持该功能.不过W3C建议在后续的HTML版本中增加HTML import ...

  6. AngularJS快速入门指南16:Bootstrap

    thead>tr>th, table.reference>tbody>tr>th, table.reference>tfoot>tr>th, table ...

  7. AngularJS快速入门指南15:API

    thead>tr>th, table.reference>tbody>tr>th, table.reference>tfoot>tr>th, table ...

  8. AngularJS快速入门指南14:数据验证

    thead>tr>th, table.reference>tbody>tr>th, table.reference>tfoot>tr>th, table ...

  9. AngularJS快速入门指南13:表单

    一个AngularJS表单是一组输入型控件的集合. HTML控件 HTML输入型标签标包括: input标签 select标签 button标签 textarea标签 HTML表单 HTML表单将各种 ...

随机推荐

  1. laravel 中的rbac自己简单的实现

    用户表 CREATE TABLE `sys_user` ( `id` varchar(64) COLLATE utf8_bin NOT NULL COMMENT '用户编号', `ids` int(1 ...

  2. 【爆料】-《南澳大学毕业证书》UniSA一模一样原件

    ☞南澳大学毕业证书[微/Q:2544033233◆WeChat:CC6669834]UC毕业证书/联系人Alice[查看点击百度快照查看][留信网学历认证&博士&硕士&海归&a ...

  3. ASP.NET Core的实时库: SignalR简介及使用

    大纲 本系列会分为2-3篇文章. 第一篇介绍了SignalR的预备知识和原理 本文介绍SignalR以及ASP.NET Core里使用SignalR. 本文的内容: 介绍SignalR 在ASP.NE ...

  4. java游戏开发杂谈 - 游戏物体

    现实生活中,有很多物体,每个物体的长相.行为都不同. 物体存在于不同的空间内,它只在这个空间内发生作用. 物体没用了,空间就把它剔除,不然既占地方,又需要花精力管理. 需要它的时候,就把它造出来,不需 ...

  5. 《k8s 源码分析》- Custom Controller 之 Informer

    Custom Controller 之 Informer 概述 架构概览 reflector - List & Watch API Server Reflector 对象 ListAndWat ...

  6. 8天入门docker系列 —— 第四天 使用aspnetcore小案例熟悉端口映射和挂载目录

    到目前为止大家应该对镜像和容器有了一个大概认知,而且也用了docker进行了一个简单化的部署,但仔细一看问题还有很多,所以这篇我们继续完善. 一:如何让外网访问到容器内应用 我们知道容器内拥有自己的子 ...

  7. [区块链] 密码学——Merkle 树

    在计算机领域,Merkle树大多用来进行完整性验证处理.在处理完整性验证的应用场景中,特别是在分布式环境下进行这样的验证时,Merkle树会大大减少数据的传输量以及计算的复杂度. Merkle哈希树是 ...

  8. 【原创】详细案例解剖——浅谈Redis缓存的常用5种方式(String,Hash,List,set,SetSorted )

    很多小伙伴没接触过Redis,以至于去学习的时候感觉云里雾里的,就有一种:教程随你出,懂了算我输的感觉. 每次听圈内人在谈论的时候总是插不上话,小编就偷偷去了解了一下,也算是初入门径. 然后就整理了一 ...

  9. Python json序列化

    Python内置的json模块提供了非常完善的对象到JSON格式的转换.废话不多说,我们先看看如何把Python对象变成一个JSON: d = dict(name='Kaven', age=17, s ...

  10. Android Aop日志

    Android在许多情况下需要知道一些方法的耗时,然后对与这些耗时进行一些处理和优化.当然我们可以在方法的开头和结尾分别来打一行日志来解决这个问题.但是这个方式侵入性强,而且比较难以修改和删除这些日志 ...