zoukankan      html  css  js  c++  java
  • IBatis.Net学习笔记六--再谈查询

    IBatis.Net学习笔记五--常用的查询方式 中我提到了一些IBatis.Net中的查询,特别是配置文件的写法。

    后来通过大家的讨论,特别是Anders Cui 的提醒,又发现了其他的多表查询的方式。
    在上一篇文章中我提到了三种方式,都是各有利弊:
    第一种方式当数据关联很多的情况下,实体类会很复杂;
    第二种方式比较灵活,但是不太符合OO的思想(不过,可以适当使用);
    第三种方式最主要的问题就是性能不太理想,配置比较麻烦。

    下面是第四种多表查询的方式,相对第二种多了一点配置,但是其他方面都很好
    (当然可能还有其他更好地解决方法,希望能多提宝贵意见-_-)
    例子还是一样:两张表Account和Degree,使用Account_ID关联,需要查出两张表的所有纪录

    首先:修改实体类,增加以下属性:

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

    (和第三种方法一样)

    然后:修改配置文件,这也是最重要的地方(PS:IBatis.Net中的配置文件真的很强)
    resultMaps节加入:

        <resultMap id="com2result"  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"  resultMapping="Account.Degree-result"/>
        </resultMap>

        <resultMap id="Degree-result"  class="Degree">
          <result property="Id"           column="Account_ID"/>
          <result property="DegreeName"    column="DegreeName"/>
        </resultMap>

    这里最主要的就是使用了resultMapping属性,resultMapping="Account.Degree-result",其中Account是当前配置文件的namespace:
    <sqlMap namespace="Account"  ......


    statements节加入:

        <select id="GetCom2Tables"
         resultMap="com2result">
          select Accounts.*, Degree.*
          from Accounts,Degree
          where Accounts.Account_ID = Degree.Account_ID
        </select>

    这样就可以随心所欲的写自己需要的sql,性能也很好,不会出现第三种方法中的1+n条的查询语句了。

    http://www.cnblogs.com/firstyi/archive/2007/08/22/864942.html

  • 相关阅读:
    syslog远程日志存储/514端口【转】
    干掉僵尸进程
    以太网,IP,TCP,UDP数据包分析【转】
    suricata 的安装编译
    libpcap详解【转】
    蛋疼的经历--wireshark不能启动的问题
    Ubuntu中root用户和user用户的相互切换[转载自93度的饼干]
    OSI七层与TCP/IP五层网络架构详解
    __cplusplus的用法(转)
    IPsec ISAKMP(转)
  • 原文地址:https://www.cnblogs.com/soundcode/p/4981661.html
Copyright © 2011-2022 走看看