一、查询select

还记得第一篇示例中是如何读出数据库里3条数据的吗?就是调用了一个QueryForList方法,从方法名就知道,查询返回列表。

1、QueryForList  返回List<T>强类型数据集合

来看看方法重载:

从重载可以看出,其实只是3个参数不同的方法,分为泛型与非泛型两个版本而已。

主要说说,参数skipResults,表示从结果行中跳过skipResults行后返回,maxResults表示返回的行数。这个在分页中会用到。

下面来看一个最简单的示例:

xml映射文件(就是之前写的那个没有改动):

  <statements>
<select id="SelectAllPerson" resultMap="PersonModel">
<!--id在程序中会被SqlMapper实体类所调用,resultMap就是resultMap节点的id-->
select * from person
</select>
</statements>

对DAO层稍微改造下:

public class PersonDAO
{
//public IList<PersonModel> GetList()
//{
// ISqlMapper mapper = Mapper.Instance();
// //DomSqlMapBuilder builder = new DomSqlMapBuilder();
// //ISqlMapper mapper = builder.Configure(@"D:\SqlMap.config");
// IList<PersonModel> ListPerson = mapper.QueryForList<PersonModel>("SelectAllPerson", null);//这个"SelectAllPerson"就是xml映射文件的Id
// return ListPerson;
//} ISqlMapper mapper = MapperHelper.Instance();//通过MapperHelper获得SqlMapper实例 public IList<PersonModel> GetList(int skipResults, int maxResults)
{
IList<PersonModel> ListPerson = mapper.QueryForList<PersonModel>("SelectAllPerson", null, skipResults, maxResults);//这个"SelectAllPerson"就是xml映射文件的Id
return ListPerson;
}
}

Main方法:

static void Main(string[] args)
{
PersonDAO dao = new PersonDAO();
IList<PersonModel> listPerson = dao.GetList(,);//跳过1条取最多10条
foreach (PersonModel p in listPerson)
{
Console.WriteLine(p.PersonId +" - "+ p.PersonName);
} Console.ReadKey();
}

跳过了第一条数据“刘备”,查询出2条(数据库只有3条记录)。

2、QueryForObject  返回一行数据,对应程序的实体类实例

xml映射文件:

  <select id="SelectOnePerson" resultMap="PersonModel">
<!--其中#Id就是传入的参数-->
select * from person where Id = #Id# 
</select>

DAO层:

public PersonModel SelectOnePerson(int id)
{
PersonModel p = mapper.QueryForObject<PersonModel>("SelectOnePerson", );//1就是传入Sql语句的参数
return p;
}
static void Main(string[] args)
{
PersonDAO dao = new PersonDAO(); PersonModel person = dao.SelectOnePerson();
Console.WriteLine(person.PersonId + " - " + person.PersonName);
Console.ReadKey();
}

3、QueryWithRowDelegate  通过委托过滤返回的数据

4、QueryForDictionary

5、QueryForMap

二、Insert

xml映射文件:

 <insert id="InsertOne" resultMap="PersonModel">
 insert into person(Name) values(#PersonName#)
<selectKey type="post" resultClass="int" property="PersonId">
SELECT @@IDENTITY
</selectKey>   
</insert>

selectKey可以获取到插入记录的Id。

selectKey有3个主要的属性: 
    1)resultClass:返回主键的数据类型
    2)type:表示主键在insert之前或之后生成(取决于数据库的主键生成策略),取值分别为[pre|post],非必须,未填写时如果在insert之前表示pre,否则表示post
    2)property:返回值保存到的属性,非必须

DAO层:

 public int InsertOne(PersonModel p)
{
return (int)mapper.Insert("InsertOne", p);//插入一条数据,返回主键Id
}
static void Main(string[] args)
{
PersonDAO dao = new PersonDAO(); PersonModel personModel = new PersonModel();
personModel.PersonName = "曹操";
personModel.PersonId = ;
int res = dao.InsertOne(personModel);
if (res > )
{
Console.WriteLine("插入成功,Id为" + res);
}
Console.ReadKey();
}

三、Update

xml映射文件:

 <update id="UpdateOne" resultMap="PersonModel">
update person set Name = #PersonName# Where Id = #PersonId#
</update>

DAO层:

 public int UpdateOne(PersonModel p)
{
return (int)mapper.Update("UpdateOne", p);
}
static void Main(string[] args)
{
PersonDAO dao = new PersonDAO(); PersonModel personModel = new PersonModel();
personModel.PersonId = ;
personModel.PersonName = "张三";
int res = dao.UpdateOne(personModel);
if (res > )
{
Console.WriteLine("修改成功");
}
Console.ReadKey();
}

四、Delete

xml映射文件:

  <delete id="DeleteOne" resultMap="PersonModel">
Delete Person Where Id = #PersonId#
</delete>

DAO层:

 public int UpdateOne(PersonModel p)
{
return (int)mapper.Delete("DeleteOne", p);
}
public int UpdateOne(int id)
{
return (int)mapper.Delete("DeleteOne", id);
}
static void Main(string[] args)
{
PersonDAO dao = new PersonDAO(); PersonModel personModel = new PersonModel();
personModel.PersonId = ;
personModel.PersonName = "张三";
int res = dao.UpdateOne(personModel);
//int res = dao.UpdateOne(10);//这样也可以
if (res > )
{
Console.WriteLine("删除成功");
}
Console.ReadKey();
}

Ibatis.Net事务处理 

BeginTransaction(); CommitTransaction(); RollBackTransaction();这里就不详细介绍了。

提供一个封装的方法:

        /// <summary>
/// 执行事务
/// </summary>
/// <param name="fun"></param>
/// <returns></returns>
public bool DoTransaction(Func<bool> fun)
{
Instance().BeginTransaction();
try
{
bool result = fun();
if (result)
{
Instance().CommitTransaction();
}
else
{
Instance().RollBackTransaction();
}
return result;
}
catch (Exception ex)
{
Instance().RollBackTransaction();
throw;
}
finally
{
}
}

参考:http://www.cnblogs.com/caoyc/category/873268.html

Ibatis.Net 数据库操作学习(四)的更多相关文章

  1. Java数据库操作学习

    JDBC是java和数据库的连接,是一种规范,提供java程序与数据库的连接接口,使用户不用在意具体的数据库.JDBC类型:类型1-JDBC-ODBC桥类型2-本地API驱动类型3-网络协议驱动类型4 ...

  2. c# 数据库操作学习

    一. 如何处理数据库连接 1. 数据库连接可以分为“物理连接”和“逻辑连接”(默认使用连接池的情况下Pooling=true): 物理连接:创建数据库连接时,默认会有一定数量的物理连接(默认Min P ...

  3. Hapi+MySql项目实战数据库操作(四)

    数据库访问 下面以Node的ORM框架Sequelize来操作数据库,Mysql为例. 配置数据库连接信息config/db_config.js: //db_config.js module.expo ...

  4. Ibatis.Net 数据库操作(四)

    一.查询select 还记得第一篇示例中的是如何读出数据库里的3条数据吗? 就是调用了一个QueryForList方法,从方法名就知道,查询返回列表. 1.QueryForList 返回List< ...

  5. 【转】Mybatis/Ibatis,数据库操作的返回值

    该问题,我百度了下,根本没发现什么有价值的文章:还是看源代码(详见最后附录)中的注释,最有效了!insert,返回值是:新插入行的主键(primary key):需要包含<selectKey&g ...

  6. 【转】 Mybatis/Ibatis,数据库操作的返回值

    该问题,我百度了下,根本没发现什么有价值的文章:还是看源代码(详见最后附录)中的注释,最有效了!insert,返回值是:新插入行的主键(primary key):需要包含<selectKey&g ...

  7. Mybatis/Ibatis,数据库操作的返回值

    该问题,我百度了下,根本没发现什么有价值的文章:还是看源代码(详见最后附录)中的注释,最有效了!insert,返回值是:新插入行的主键(primary key):需要包含<selectKey&g ...

  8. Oracle 数据库基础学习 (四) group by的使用

      group by分组查询 示例:要求查询出每个部门的编号,人数,以及最高和最低工资  select deptno, count(empno), max(sal), min(sal) from em ...

  9. C# SQLite 数据库操作

    C# SQLite 数据库操作学习 运行环境:Window7 64bit,.NetFramework4.61,C# 7.0 参考: SQLite 官网 SQL As Understood By SQL ...

随机推荐

  1. Gerrit 配置同步到多个仓库

    1.修改replication.config文件 [remote "xxx"] projects = Yilule.Core.Service #aliyun仓库 url = git ...

  2. [转帖]linux namespace 和cgroup lxc

    https://blog.csdn.net/xiaoliuliu2050/article/details/53443863 5.1 linux namespace 和cgroup lxc 2016年1 ...

  3. helm 替换源的方法

    网上找了一个 helm 替换源的方法 挺好用的 mark 一下 helm repo remove stable helm repo add stable https://kubernetes.oss- ...

  4. 解决sublime text3下中文无法输入的问题(Ubuntu)

    sublime-text-imfix,非常无脑.就喜欢这样的.

  5. echarts实现环形图

    前端框架使用的angular,使用echarts实现环形图 1. item.component.html <div id="box1" class="pie&quo ...

  6. Docker一些常用命令

    1.启动Docker服务 service docker start 2.查看所有镜像 docker images 3.查看正在运行的容器 docker ps 4.查看所有容器 docker ps -a ...

  7. 51nod 1102 面积最大的矩形 (单调栈)

    链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1102 思路: 首先介绍下单调栈的功能:利用单调栈,可以找到从左/ ...

  8. hihoCoder 1632 Secret Poems(ACM-ICPC北京赛区2017网络同步赛)

    时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 The Yongzheng Emperor (13 December 1678 – 8 October 1735), was ...

  9. BZOJ 4764: 弹飞大爷

    4764: 弹飞大爷 Time Limit: 30 Sec  Memory Limit: 256 MBSubmit: 4  Solved: 4[Submit][Status][Discuss] Des ...

  10. sql server 小技巧 集锦

    sql server 小技巧(1) 导入csv数据到sql server sql server 小技巧(2) 删除sql server中重复的数据 sql server 小技巧(3) SQL Serv ...