好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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

深入浅出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 中包含 9 个 Key

查看更多关于深入浅出Ibatis.net查询方式(1)的详细内容...

  阅读:38次