SmartSql 快速使用指南
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 快速使用指南的更多相关文章
- Rancher 快速上手指南操作(1)
Rancher 快速上手指南操作(1)该指南知道用户如何快速的部署Rancher Server 管理容器.前提是假设你的机器已经安装好docker了.1 确认 docker 的版本,下面是 ubunt ...
- AngularJS快速入门指南20:快速参考
thead>tr>th, table.reference>tbody>tr>th, table.reference>tfoot>tr>th, table ...
- AngularJS快速入门指南19:示例代码
本文给出的大部分示例都可以直接运行,通过点击运行按钮来查看结果,同时支持在线编辑代码. <div ng-app=""> <p>Name: <input ...
- AngularJS快速入门指南18:Application
是时候创建一个真正的AngularJS单页面应用程序了(SPA). 一个AngularJS应用程序示例 你已经了解了足够多的内容来创建第一个AngularJS应用程序: My Note Save Cl ...
- AngularJS快速入门指南17:Includes
使用AngularJS,你可以在HTML中包含其它的HTML文件. 在HTML中包含其它HTML文件? 当前的HTML文档还不支持该功能.不过W3C建议在后续的HTML版本中增加HTML import ...
- AngularJS快速入门指南16:Bootstrap
thead>tr>th, table.reference>tbody>tr>th, table.reference>tfoot>tr>th, table ...
- AngularJS快速入门指南15:API
thead>tr>th, table.reference>tbody>tr>th, table.reference>tfoot>tr>th, table ...
- AngularJS快速入门指南14:数据验证
thead>tr>th, table.reference>tbody>tr>th, table.reference>tfoot>tr>th, table ...
- AngularJS快速入门指南13:表单
一个AngularJS表单是一组输入型控件的集合. HTML控件 HTML输入型标签标包括: input标签 select标签 button标签 textarea标签 HTML表单 HTML表单将各种 ...
随机推荐
- bzoj4067 [Ctsc2015]gender
好神的一道题啊! 我们发现题目中的ln的贡献非常傻逼,但是我们可以发现这个东西的取值只有40个左右,于是我们可以枚举他! 枚举完了对于题里的贡献就是一个普通的最小割,采用的是文理分科的思想,与S连代表 ...
- 记录遭遇挖矿程序kthrotlds的失败处理经历
1 发现问题 在腾讯云上购买了一个centos7的服务器,平时用来练手,偶尔也安装一些程序进行测试,上面安装了mysql和redis,前段时间数据库经常掉线,连不上,到腾讯云后台进行查看,通过服务器实 ...
- 基于udp的套接字编程
一,简单明了了解udp套接字编程 客户端: #Author : Kelvin #Date : 2019/1/30 11:07 from socket import * ip_conf=("1 ...
- C#ComboBox控件“设置 DataSource 属性后无法修改项集合”的解决方法
在使用ComboBox控件时,遇到了重新绑定赋值出问题的情况.正常情况下,对于数据重新赋值的或者绑定数据源的时候,为了防止数据出现问题,都会先清空原来数据,所以就这样写了,但是没有相当恰恰这样写就出现 ...
- maven构建失败。
maven项目编译报“Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1” 解决方案:setting.x ...
- 在已有的Asp.net MVC项目中引入Taurus.MVC
Taurus.MVC是一个优秀的框架,如果要应用到已有的Asp.net MVC项目中,需要修改一下. 1.前提约定: 走Taurus.MVC必须指定后缀.如.api 2.原项目修改如下: web.co ...
- 设计模式 | 简单工厂模式(static factory method)
按理说应该把书全都看完一遍,再开始写博客比较科学,会有比较全面的认识. 但是既然都决定要按规律更新博客了,只能看完一个设计模式写一篇了. 也算是逼自己思考了,不是看完就过,至少得把代码自己都敲一遍. ...
- Cesium 海拔 经纬度 展示
通过参考http://gishome.net.cn/cesium/cesium-coordinates/,整理修改后 <!DOCTYPE html><html lang=" ...
- 高通QCC3026蓝牙音频芯片处理器介绍
QCC3026是一款基于超低功耗架构的入门级闪存可编程蓝牙音频SoC,专为紧凑型功能优化的Qualcomm TrueWireless耳塞而设计.QCC3026旨在为我们的客户提供有助于缩短开发时间并具 ...
- postgresql 添加uuid扩展
去年用EF Core做数据迁移到psql数据库的时候遇到了缺失uuid的错误,当时帅气的脸蛋突然懵逼了.现记录一下 以备参考. 环境:Centos7.2 psql Xshell Xshell连接C ...