zoukankan      html  css  js  c++  java
  • IBatis.Net 表连接查询(五)

      IBatis.Net之多表查询:

    一、定制实际对应类的方式

      首先配置多表的测试数据库,IBatis.Net之Oracle表连接查询配置:

      首先新建两张表如下:

      

      

      为两张表建立外键:

    ALTER TABLE Person
      ADD CONSTRAINT FK_COUNTRY_PERSON FOREIGN KEY(CountryId) 
      REFERENCES Country(Id);

      程序中,建立一个PersonCountry.xml

    <?xml version="1.0" encoding="utf-8" ?>
    <sqlMap namespace="Ibatis" xmlns="http://ibatis.apache.org/mapping" xmlns:xls="http://www.w3.org/2001/XMLSchema-instance">
      <resultMaps>
        <resultMap id="PersonCountry" Class="Ibatis.Net.Domain.PersonCountryModel">   <!--id会被statements节点所用,Class实体类所在位置-->
          <result property="Id" column="ID"/>  <!--property实体类的属性名,column对应的列名-->
          <result property="Name" column="NAME"/>
          <result property="CountryName" column="COUNTRYNAME"/>
        </resultMap>
      </resultMaps>
      <statements>
      <select id="SelectAllPersonWithCountry" resultMap="PersonCountry"> 
        SELECT p.Id,p.Name,c.CountryName FROM Person p INNER JOIN Country c ON p.CountryId = c.Id
      </select>    
      </statements>
    </sqlMap>

      再建立一个对应结果的Model类

    namespace Ibatis.Net.Domain
    {
        public class PersonCountryModel
        {
            public int Id { get; set; }
         public string Name { get;  set; }
         public string CountryName { get; set; }
        }
    }

      建立一个Dao类:

       public class PersonCountryDao
        {
            public IList<PersonCountryModel> GetList()
            {
                ISqlMapper mapper = Mapper.Instance();
                IList<PersonCountryModel> ListPC = mapper.QueryForList<PersonCountryModel>("SelectAllPersonWithCountry", null);
                return ListPC;
            }
        }

      执行代码:

            static void Main(string[] args)
            {
                PersonCountryDao dao = new PersonCountryDao();
                IList<PersonCountryModel> ListPC = dao.GetList();
                foreach (PersonCountryModel p in ListPC)
                {
                    Console.WriteLine(p.Id + p.Name + p.CountryName);
                }
                Console.ReadKey();
            }

      输出如下:

      

    二、AS转换的方式

      下面,我们还是用同样的例子。但是,这次我只想查Person.Id与CountryName。那么这次不用定制类的方式,应该怎样弄呢?

       Person.xml如下:

    <sqlMap namespace="Ibatis" xmlns="http://ibatis.apache.org/mapping" xmlns:xls="http://www.w3.org/2001/XMLSchema-instance">
      <resultMaps>
        <resultMap id="Person" Class="Ibatis.Net.Domain.PersonModel">   <!--id会被statements节点所用,Class实体类所在位置-->
          <result property="Id" column="Id"/>  <!--property实体类的属性名,column对应的列名-->
          <result property="Name" column="Name"/>
        </resultMap>
      </resultMaps>
      <statements>
        <select id="SelectPersonWithCountryName" ResultMap="Person">
          SELECT Person.Id,CountryName AS Name FROM Person INNER JOIN Country ON Person.CountryId = Country.Id
        </select>
      </statements>
    </sqlMap>

      注意此时的Person还是与Person表对应,只有两个字段,Id和Name,不用特别定制。

      输出如下:

      

      这里的例子较为简单,因为CountryName和Name都是字符串类型。如果复杂点,可能在写SQL语句的时候需要使用SQL函数转换类型,例如要查询的列是整型,但是能够接收返回结果的只有浮点型或字符串类型,这个时候就只有在SQL中转换类型了。但如果想查的是DateTime类型,能用于接收结果的属性只有int,那就没办法转了,只有定制对应类。

    三、关联实体类

      其实IBatis.net也支持NHibernate的那种方式,实体类关联的方式来支持表与表之间的关系:

      如,这次我想查询Person,Country所有的信息:

        public class PersonModel
        {
            public int Id
            {
                get;
                set;
            }
    
            public string Name
            {
                get;
                set;
            }
         //在Person之中加入一个CountryModel
            public CountryModel Country
            {
                get;
                set;
            }
        }

      配置文件:

    <sqlMap namespace="Ibatis" xmlns="http://ibatis.apache.org/mapping" xmlns:xls="http://www.w3.org/2001/XMLSchema-instance">
      <resultMaps>
        <resultMap id="Person" Class="Ibatis.Net.Domain.PersonModel">
          <result property="Id" column="Id"/>
          <result property="Name" column="Name"/>
          <result property="Country" resultMapping="Ibatis.Country"/><!--Ibatis是关联类所在xml文件的namespace,Country是下两行的那个id-->
        </resultMap>
        <resultMap id="Country" Class="Ibatis.Net.Domain.CountryModel">
          <result property="Id" column="Id"/>
          <result property="CountryName" column="CountryName"/>
        </resultMap>
      </resultMaps>
      <statements>
        <select id="SelectPersonWithCountryName" resultMap="Person">
          SELECT * FROM Person INNER JOIN Country ON Person.CountryId = Country.Id
        </select>
      </statements>
    </sqlMap>

      运行输出如下:

      

     
    原文地址:http://www.cnblogs.com/kissdodog/p/3302619.html
     
  • 相关阅读:
    如何优雅地使用 Stack Overflow
    Quartz总结
    slf4j-api、slf4j-log4j12以及log4j之间什么关系?
    eclipse 安装 spring boot suite 插件遇到的问题
    Java项目结构总结
    netstat 与 telnet
    微服务架构中的安全认证与鉴权
    git 常用命令
    session 、cookie、token的区别
    List和Set区别
  • 原文地址:https://www.cnblogs.com/caoyc/p/5810491.html
Copyright © 2011-2022 走看看