Ibatis.Net 表连接查询学习(五)

IBatis.Net之多表查询

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

首先配置多表的测试数据库,在之前Person表中增加一列"CountryId",新建一张Country表,两张表关系如下:

  

建立一个Model类:

public class PersonCountryModel
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public string CountryName { get; set; }
    }

在binDebug下建立一个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="PersonCountry">
      <!--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.CountryId
    </select>
  </statements>
  
</sqlMap>

SqlMap.config:

<alias>
    <!--为类指定一个别名-->
    <typeAlias alias="Person" type="IbatisNetModel.PersonModel,IbatisNetModel"/>
    <typeAlias alias="PersonCountry" type="IbatisNetModel.PersonCountryModel,IbatisNetModel"/>
  </alias>

  <sqlMaps>
    <!--这个是指定映射文件的位置-->
    <sqlMap resource="Person.xml" />   
    <sqlMap resource="PersonCountry.xml" />
  </sqlMaps>

在PersonDAO类下添加方法:

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

Main:

  static void Main(string[] args)
        {
            PersonDAO dao = new PersonDAO();

            IList<PersonCountryModel> ListPC = dao.GetList();
            foreach (PersonCountryModel p in ListPC)
            {
                Console.WriteLine(p.Id + p.Name + p.CountryName);
            }
            Console.ReadKey();
        }

输出如下:

二、AS转换的方式

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

<resultMaps>
    <resultMap id="PersonModel" Class="Person">
      <!--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,Country.CountryName AS Name FROM Person INNER JOIN Country ON Person.CountryId = Country.CountryId
    </select>
  </statements>

我们依旧使用Person类,Person还是与Person表对应,只有两个字段,Id和Name,不用特别定制。

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

三、关联实体类

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

在Person类中加入一个CountryModel类型的属性Country:

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

xml配置文件:

<?xml version="1.0" encoding="utf-8" ?>
<sqlMap namespace="PersonCountry" xmlns="http://ibatis.apache.org/mapping" xmlns:xls="http://www.w3.org/2001/XMLSchema-instance">

  <resultMaps>
    <resultMap id="PersonModel" Class="Person">
      <result property="PersonId" column="Id"/>
      <result property="PersonName" column="Name"/>
      <result property="Country" resultMapping="PersonCountry.CountryModel"/>
      <!--PersonCountry是关联类所在xml文件的namespace,Country是下面resultMap的id-->
    </resultMap>
    <resultMap id="CountryModel" Class="Country">
      <result property="CountryId" column="CountryId"/>
      <result property="CountryName" column="CountryName"/>
    </resultMap>
  </resultMaps>

  <statements>
    <select id="SelectPersonWithCountryName" resultMap="PersonModel">
      SELECT * FROM Person INNER JOIN Country ON Person.CountryId = Country.CountryId
    </select>
  </statements>

</sqlMap>

在PersonDAO类下添加方法:

 public IList<PersonModel> SelectPersonWithCountryName()
        {
            IList<PersonModel> ListPC = mapper.QueryForList<PersonModel>("SelectPersonWithCountryName", null);
            return ListPC;
        }

Main:

static void Main(string[] args)
        {
            PersonDAO dao = new PersonDAO();

            IList<PersonModel> ListPC = dao.SelectPersonWithCountryName();
            foreach (PersonModel p in ListPC)
            {
                Console.WriteLine(p.PersonId + p.PersonName + p.Country.CountryName);
            }
            Console.ReadKey();
        }

输出结果:

参考:http://www.cnblogs.com/caoyc/category/873268.html

原文地址:https://www.cnblogs.com/vanblog/p/8693762.html