C军

不玩博客了!

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  496 随笔 :: 0 文章 :: 634 评论 :: 571万 阅读

  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>
复制代码

  运行输出如下:

  

posted on   逆心  阅读(2627)  评论(2编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示