深入浅出Ibatis.net查询方式(1)

这里我们按照ibatis.net提供的相关的方法来介绍其拥有的查询方式,后续会对其进行分类归纳总结

在iBatis.net中有一个ISqlMapper接口,它是和外界交流的核心接口,下面是该接口下设计的属性和方法

(可以放大查看该图片)

可以看到里面有很多重载的方法,这些方法是实际使用中需要重点关注的,掌握和熟悉这些方法,可以帮助开发人员高效实现业务,避免再造轮子。

  • QueryForObject

    其用法及签名如下所示

Executes a Sql SELECT statement that returns that returns data to populate a single object instance.

The parameter object is generally used to supply the input data for the WHERE clause parameter(s) of the SELECT statement.

Overload List

Executes a Sql SELECT statement that returns that returns data to populate a single object instance. The parameter object is generally used to supply the input data for the WHERE clause parameter(s) of the SELECT statement.

object QueryForObject(String,Object);

Executes a Sql SELECT statement that returns a single object of the type of the resultObject parameter.

object QueryForObject(String,Object,Object);

Executes a Sql SELECT statement that returns that returns data to populate a single object instance. The parameter object is generally used to supply the input data for the WHERE clause parameter(s) of the SELECT statement.

T QueryForObject<T>(String,Object);

Executes a Sql SELECT statement that returns a single object of the type of the resultObject parameter.

T QueryForObject<T>(String,Object,ISqlMapper.T);

 

QueryForQbject通俗说其作用是将数据库中的一条记录以特定的方式呈现,相当于其它框架中的GetModel(string id)等方法。建议使用generic版本。

下面举两个例子

 

例子1

代码:

SqlMap.QueryForObject<Account>("Account.SelectOne", id)

在ibatis.net的配置文件中,由于启用了useStatementNamespaces设置为true,因此在上面的Account.SelectOne需要带上命名空间,命名空间的名字对应Account.xml中第一行namespace中的名称,此处为Account

Account.xml映射文件片段

<sqlMap namespace="Account" xmlns="http://ibatis.apache.org/mapping"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >

 

    <alias>

        <typeAlias alias="Account" type="theone.domain.Account,theone.domain" />

    </alias>

        <select id="SelectOne" parameterClass="string" resultClass="Account">

            select * from Accounts where Id=#Id#

        </select>

resultClass="Account"中,Account是设置的别名,简化缩写

 

例子2

代码

        public Hashtable FindAllOrderById5()

        {

            return SqlMap.QueryForObject<Hashtable>("Account.select-all2", null);

        }

Account.xml映射文件

        <select id="select-all2" resultMap="FindAllResult2">

            select * from Accounts order by Id desc

        </select>

        <resultMap id="FindAllResult2" class="Hashtable">

            <result property="Id" column="Id" />

            <result property="Item" column="Item" />

            <result property="Money" column="Money" />

            <result property="Year" column="Year" />

            <result property="Month" column="Month" />

            <result property="Day" column="Day" />

            <result property="FavoritorLevel" column="FavoritorLevel" />

            <result property="CreatedOn" column="CreatedOn" />

            <result property="ModifiedOn" column="ModifiedOn" />

        </resultMap>

这个映射配置使得到的结果的每一条记录都映射成一个Hashtable,这个Hashtable中包含9Key

posted on 2010-01-11 11:23  lexus 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lexus/archive/2010/01/11/1643941.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值