zoukankan      html  css  js  c++  java
  • IBatis.Net学习笔记五--常用的查询方式

    在项目开发过程中,查询占了很大的一个比重,一个框架的好坏也很多程度上取决于查询的灵活性和效率。
    在IBatis.Net中提供了方便的数据库查询方式。

    在Dao代码部分主要有两种方式:
    1、查询结果为一个对象:

                    ISqlMapper sqlMap = sqlMapDaoSession.SqlMap;

                    return (Account) sqlMap.QueryForObject("GetAccountViaColumnName", accountID);

    2、查询结果为一个列表:

                    ISqlMapper sqlMap = sqlMapDaoSession.SqlMap;

                    return (ArrayList)sqlMap.QueryForList("GetAccountAsHashtableResultClass", 1);

    这两种方法同时都提供了面向泛型的重载方法。这两个方法的第一个参数对应配置文件中的select id,第二个参数表示传入查询的条件

    配置文件的写法:
    在IBatis.Net中提供了多种查询配置的写法,我这里列出几种比较常用的方式:
    1、获得一张表的所有数据

            <select id="GetAllAccountsAsHashMapViaResultMap"
                            resultMap="account-hashtable-result">
                select *
                from Accounts
                order by Account_ID
            </select>

    这是最简单的方式,其中resultMap是返回查询结果的形式,需要另外配置:

            <resultMap id="account-hashtable-result" class="Hashtable">
                <result property="Id"           column="Account_ID"/>
                <result property="FirstName"    column="Account_FirstName"/>
                <result property="LastName"     column="Account_LastName"/>
                <result property="EmailAddress" column="Account_Email"/>
            </resultMap>

    表示:得到的结果的每一条记录都映射成一个Hashtable,这个Hashtable中包含四个Key(Id,FirstName......)

    2、根据条件查询(简单方式):

            <select id="GetAccountViaColumnIndex"
                    parameterClass="int"
                    resultMap="indexed-account-result">
                select
                Account_ID,
                Account_FirstName,
                Account_LastName,
                Account_Email
                from Accounts
                where Account_ID = #value#
            </select>

    只有一个条件,传入参数的类型是int型,拼写sql时直接用 #value#就可以了

    3、根据条件查询(较复杂方式):

            <select id="GetAccountsDynamic" resultMap="account-result" parameterClass="Hashtable" >
                select top $MaximumAllowed$ * from Accounts
                <dynamic prepend="where">
                        <isParameterPresent>
                        <isNotEmpty prepend="and" property="FirstName" >
                                Account_FirstName LIKE '%$FirstName$%'
                        </isNotEmpty>
                        <isNotEmpty prepend="and" property="LastName" >
                                Account_LastName LIKE '%$LastName$%'
                        </isNotEmpty>
                        <isNotEmpty prepend="and" property="EmailAddress"  >
                                Account_Email LIKE '%$EmailAddress$%'
                        </isNotEmpty>
                        </isParameterPresent>
                    </dynamic>
                    order by Account_LastName
            </select>

    传入参数是一个Hashtable,MaximumAllowed等表示的是Hashtable里的key值,用$$包含起来。
    并且查询时可以根据条件是否为空动态拼写sql语句 
    PS:输入参数同样可以使用Account类,注意对应的键要和类中的属性名一致(大小写也要一样)

    4、多表查询
    多表查询时返回参数有三种方式,一种是新建一个类,在这个类中包含这多个表的所有属性,还有一种就是直接返回Hastable就可以了:

            <select id="GetAccountAsHashtableResultClass"
        resultClass="HashMap">
                select
                a.*,b.*
                from a,b
                where a.Account_ID = b.Account_ID        </select>

    PS:这里的HashMap实际上就是Hashtable

    第三种方式是使用IBatis中的复杂属性(感谢Anders Cui 的提醒)
    比如现在有两张表Account和Degree,使用Account_ID关联,那么需要在原有的基础上修改:
    1、修改Account实体类,加入一个属性:

            private Degree _degree;
            public Degree Degree
            {
                get
                {
                    return _degree;
                }
                set
                {
                    _degree = value;
                }
            }

    这样是一个1:1的关系,也可以加入IList DegreeList的属性,这样查询的结果就是一个1:n的关系

    2、修改配置文件:
    resultMaps节加入:

        <resultMap id="comresult"  class="Account" >
          <result property="Id"           column="Account_ID"/>
          <result property="FirstName"    column="Account_FirstName"/>
          <result property="LastName"     column="Account_LastName"/>
          <result property="EmailAddress" column="Account_Email" nullValue="no_email@provided.com"/>
          <result property="Degree" column="Account_ID=Account_ID"  select="degreeretrive" />
        </resultMap>

    对于Degree属性,还可以加入lazyLoad=true 延迟加载,优化性能(也就是开始时并没有实际查询数据库,当用到属性Degree时,才实际的查询相应的数据)

    statements节加入:

        <statement id="degreeretrive"
          parameterClass="Hashtable"
          resultClass="Degree">
          select *
          from Degree
          where Account_id = #Account_ID#
        </statement>

        <select id="GetComTables"
          resultMap="comresult">
          select *
          from Accounts
          order by Account_ID
        </select>

    这样可以正确的查询出结果,符合OO,但是也有两个小问题:
    1、比较麻烦,不够灵活
    2、性能受影响:
        这种方式其实和Hibernet比较类似了,查询时首先执行
        select *        from Accounts        order by Account_ID
        然后根据这条语句的结果,比如有100条记录,那就要执行100次以下的语句:
        select *        from Degree        where Account_id =  @param0

    关于输入输出:
    从上面可以看到输入时可以使用:parameterClass和parameterMap,输出时可以使用:resultClass和resultMap
    对于resultMap和parameterMap我们需要另外进行配置(如上所示)
    对于parameterClass和resultClass,如果是C#固有类型可以直接使用,如果是我们自定义类可以在SqlMap.config中先统一声明一下:

        <alias>
            <typeAlias alias="Account" type="GSpring.Domain.Account"/>
        </alias>

    http://www.cnblogs.com/firstyi/archive/2007/08/21/863605.html

  • 相关阅读:
    二手房交易平台需求分析心得——字节移动小组
    结对编程之个人项目代码分析
    五月最新版 重装win10软件
    学习必备的——超级有用的网站!!!
    SQL中sa被禁用
    19 erp沙盘校赛
    temp
    Bootstrap 学习日志(二)
    Bootstrap 学习日志(一)
    关于Android上进行模拟登陆时的验证码问题
  • 原文地址:https://www.cnblogs.com/soundcode/p/4981554.html
Copyright © 2011-2022 走看看