IBatis.Net之多表查询

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

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

  

建立一个Model类:

  1. public class PersonCountryModel
  2. {
  3. public int Id { get; set; }
  4.  
  5. public string Name { get; set; }
  6.  
  7. public string CountryName { get; set; }
  8. }

在bin\Debug下建立一个PersonCountry.xml文件

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <sqlMap namespace="Ibatis" xmlns="http://ibatis.apache.org/mapping" xmlns:xls="http://www.w3.org/2001/XMLSchema-instance">
  3.  
  4. <resultMaps>
  5. <resultMap id="PersonCountry" Class="PersonCountry">
  6. <!--id会被statements节点所用,Class 实体类所在位置,这里使用别名-->
  7. <result property="Id" column="Id"/>
  8. <!--property实体类的属性名,column对应的列名-->
  9. <result property="Name" column="Name"/>
  10. <result property="CountryName" column="CountryName"/>
  11. </resultMap>
  12. </resultMaps>
  13.  
  14. <statements>
  15. <select id="SelectAllPersonWithCountry" resultMap="PersonCountry">
  16. SELECT p.Id,p.Name,c.CountryName FROM Person p INNER JOIN Country c ON p.CountryId = c.CountryId
  17. </select>
  18. </statements>
  19.  
  20. </sqlMap>

SqlMap.config:

  1. <alias>
  2. <!--为类指定一个别名-->
  3. <typeAlias alias="Person" type="IbatisNetModel.PersonModel,IbatisNetModel"/>
  4. <typeAlias alias="PersonCountry" type="IbatisNetModel.PersonCountryModel,IbatisNetModel"/>
  5. </alias>
  6.  
  7. <sqlMaps>
  8. <!--这个是指定映射文件的位置-->
  9. <sqlMap resource="Person.xml" />
  10. <sqlMap resource="PersonCountry.xml" />
  11. </sqlMaps>

在PersonDAO类下添加方法:

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

Main:

  1. static void Main(string[] args)
  2. {
  3. PersonDAO dao = new PersonDAO();
  4.  
  5. IList<PersonCountryModel> ListPC = dao.GetList();
  6. foreach (PersonCountryModel p in ListPC)
  7. {
  8. Console.WriteLine(p.Id + p.Name + p.CountryName);
  9. }
  10. Console.ReadKey();
  11. }

输出如下:

二、AS转换的方式

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

  1. <resultMaps>
  2. <resultMap id="PersonModel" Class="Person">
  3. <!--id会被statements节点所用,Class 实体类所在位置-->
  4. <result property="Id" column="Id"/>
  5. <!--property实体类的属性名,column对应的列名-->
  6. <result property="Name" column="Name"/>
  7. </resultMap>
  8. </resultMaps>
  9. <statements>
  10. <select id="SelectPersonWithCountryName" ResultMap="Person">
  11.    SELECT Person.Id,Country.CountryName AS Name FROM Person INNER JOIN Country ON Person.CountryId = Country.CountryId
  12. </select>
  13. </statements>

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

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

三、关联实体类

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

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

  1. public class PersonModel
  2. {
  3. public int PersonId { get; set; }
  4. public string PersonName { get; set; }
  5. //在Person之中加入一个CountryModel
  6. public CountryModel Country { get; set; }
  7. }

xml配置文件:

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <sqlMap namespace="PersonCountry" xmlns="http://ibatis.apache.org/mapping" xmlns:xls="http://www.w3.org/2001/XMLSchema-instance">
  3.  
  4. <resultMaps>
  5. <resultMap id="PersonModel" Class="Person">
  6. <result property="PersonId" column="Id"/>
  7. <result property="PersonName" column="Name"/>
  8. <result property="Country" resultMapping="PersonCountry.CountryModel"/>
  9. <!--PersonCountry是关联类所在xml文件的namespace,Country是下面resultMap的id-->
  10. </resultMap>
  11. <resultMap id="CountryModel" Class="Country">
  12. <result property="CountryId" column="CountryId"/>
  13. <result property="CountryName" column="CountryName"/>
  14. </resultMap>
  15. </resultMaps>
  16.  
  17. <statements>
  18. <select id="SelectPersonWithCountryName" resultMap="PersonModel">
  19. SELECT * FROM Person INNER JOIN Country ON Person.CountryId = Country.CountryId
  20. </select>
  21. </statements>
  22.  
  23. </sqlMap>

在PersonDAO类下添加方法:

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

Main:

  1. static void Main(string[] args)
  2. {
  3. PersonDAO dao = new PersonDAO();
  4.  
  5. IList<PersonModel> ListPC = dao.SelectPersonWithCountryName();
  6. foreach (PersonModel p in ListPC)
  7. {
  8. Console.WriteLine(p.PersonId + p.PersonName + p.Country.CountryName);
  9. }
  10. Console.ReadKey();
  11. }

输出结果:

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

Ibatis.Net 表连接查询学习(五)的更多相关文章

  1. IBatis.Net 表连接查询(五)

    IBatis.Net之多表查询: 一.定制实际对应类的方式 首先配置多表的测试数据库,IBatis.Net之Oracle表连接查询配置: 首先新建两张表如下: 为两张表建立外键: ALTER TABL ...

  2. MySQL之多表查询一 介绍 二 多表连接查询 三 符合条件连接查询 四 子查询 五 综合练习

    MySQL之多表查询 阅读目录 一 介绍 二 多表连接查询 三 符合条件连接查询 四 子查询 五 综合练习 一 介绍 本节主题 多表连接查询 复合条件连接查询 子查询 首先说一下,我们写项目一般都会建 ...

  3. 5月10日 python学习总结 单表查询 和 多表连接查询

    一. 单表查询  一 语法 select distinct 查询字段1,查询字段2,... from 表名 where 分组之前的过滤条件 group by 分组依据 having 分组之后的过滤条件 ...

  4. Mysql表连接查询

    原文地址: https://www.cnblogs.com/qiuqiuqiu/p/6442791.html 1.内联接(典型的联接运算,使用像 = 或 <> 之类的比较运算符).包括相等 ...

  5. MyBatis数据持久化(七)多表连接查询

    本节继续以多表连接查询的案例介绍使用resultMap的好处,对于两张以上的表进行关联查询,当我们有选择的从不同表查询所需字段时,使用resultMap是相当方便的.例如我们有两张表,分别为用户表Us ...

  6. MybatisPlus多表连接查询

    一.序言 (一)背景内容 软件应用技术架构中DAO层最常见的选型组件为MyBatis,熟悉MyBatis的朋友都清楚,曾几何时MyBatis是多么的风光,使用XML文件解决了复杂的数据库访问的难题.时 ...

  7. SQL多表连接查询(详细实例)

    转载博客:joeleo博客(http://www.xker.com/page/e2012/0708/117368.html) 本文主要列举两张和三张表来讲述多表连接查询. 新建两张表: 表1:stud ...

  8. SQL多表连接查询

    SQL多表连接查询 本文主要列举两张和三张表来讲述多表连接查询. 新建两张表: 表1:student  截图如下: 表2:course  截图如下: (此时这样建表只是为了演示连接SQL语句,当然实际 ...

  9. oracle(sql)基础篇系列(二)——多表连接查询、子查询、视图

        多表连接查询 内连接(inner join) 目的:将多张表中能通过链接谓词或者链接运算符连接起来的数据查询出来. 等值连接(join...on(...=...)) --选出雇员的名字和雇员所 ...

随机推荐

  1. Django如何安装指定版本

      Django默认安装最新版本:pip install django Django后面接版本号就可以了:pip install django==1.11.7 如果使用pip install安装库比较 ...

  2. 数位DP复习小结

    转载请注明原文地址http://www.cnblogs.com/LadyLex/p/8490222.html 之前学数位dp的时候底子没打扎实 虚的要死 这次正好有时间……刷了刷之前没做的题目 感觉自 ...

  3. 【刷题】BZOJ 3653 谈笑风生

    Description 设T 为一棵有根树,我们做如下的定义: ? 设a和b为T 中的两个不同节点.如果a是b的祖先,那么称"a比b不知道 高明到哪里去了". ? 设a 和 b 为 ...

  4. 【刷题】BZOJ 3512 DZY Loves Math IV

    Description 给定n,m,求 模10^9+7的值. Input 仅一行,两个整数n,m. Output 仅一行答案. Sample Input 100000 1000000000 Sampl ...

  5. [BZOJ2957] [THU2013集训] 楼房重建

    套路套路套路套路套路套路套路套路套路套路... 我只能这么说:一道裸得只剩下套路的水题... 线段树维护单调栈,显然,能够看到的楼房一定是递增的,但不是按高度递增,而是按高度和坐标的比值递增 所以我们 ...

  6. 学习6__STM32--SPI外设之中断收发---

    <目标> STM32双机 SPI中断收发通信 <描述> # STM32双机配置为一主一从模式 # 采用主机中断发送,从机中断接收 # 收发机制采用不间断收发(发送为空就发送,接 ...

  7. 在Linux中将脚本做成系统服务

    有一些情况下,我们需要将某些脚本作为系统服务来运行.比如,在我使用workerman框架开发php程序时,需要使用管理员权限来运行,而且需要开机自行启动程序提供服务.这个时候将启动程序写成服务就可以很 ...

  8. 个推基于 Apache Pulsar 的优先级队列方案

    作者:个推平台研发工程师 祥子 一.业务背景在个推的推送场景中,消息队列在整个系统中占有非常重要的位置.当 APP 有推送需求的时候, 会向个推发送一条推送命令,接到推送需求后,我们会把APP要求推送 ...

  9. vue.js初识(一)

    vue.js安装 官网:http://cn.vuejs.org/ 官方安装介绍:http://cn.vuejs.org/v2/guide/installation.html MVVM框架:View.V ...

  10. LSTM介绍

    转自:https://blog.csdn.net/gzj_1101/article/details/79376798 LSTM网络 long short term memory,即我们所称呼的LSTM ...