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; }
}
在bin\Debug下建立一个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
Ibatis.Net 表连接查询学习(五)的更多相关文章
- IBatis.Net 表连接查询(五)
IBatis.Net之多表查询: 一.定制实际对应类的方式 首先配置多表的测试数据库,IBatis.Net之Oracle表连接查询配置: 首先新建两张表如下: 为两张表建立外键: ALTER TABL ...
- MySQL之多表查询一 介绍 二 多表连接查询 三 符合条件连接查询 四 子查询 五 综合练习
MySQL之多表查询 阅读目录 一 介绍 二 多表连接查询 三 符合条件连接查询 四 子查询 五 综合练习 一 介绍 本节主题 多表连接查询 复合条件连接查询 子查询 首先说一下,我们写项目一般都会建 ...
- 5月10日 python学习总结 单表查询 和 多表连接查询
一. 单表查询 一 语法 select distinct 查询字段1,查询字段2,... from 表名 where 分组之前的过滤条件 group by 分组依据 having 分组之后的过滤条件 ...
- Mysql表连接查询
原文地址: https://www.cnblogs.com/qiuqiuqiu/p/6442791.html 1.内联接(典型的联接运算,使用像 = 或 <> 之类的比较运算符).包括相等 ...
- MyBatis数据持久化(七)多表连接查询
本节继续以多表连接查询的案例介绍使用resultMap的好处,对于两张以上的表进行关联查询,当我们有选择的从不同表查询所需字段时,使用resultMap是相当方便的.例如我们有两张表,分别为用户表Us ...
- MybatisPlus多表连接查询
一.序言 (一)背景内容 软件应用技术架构中DAO层最常见的选型组件为MyBatis,熟悉MyBatis的朋友都清楚,曾几何时MyBatis是多么的风光,使用XML文件解决了复杂的数据库访问的难题.时 ...
- SQL多表连接查询(详细实例)
转载博客:joeleo博客(http://www.xker.com/page/e2012/0708/117368.html) 本文主要列举两张和三张表来讲述多表连接查询. 新建两张表: 表1:stud ...
- SQL多表连接查询
SQL多表连接查询 本文主要列举两张和三张表来讲述多表连接查询. 新建两张表: 表1:student 截图如下: 表2:course 截图如下: (此时这样建表只是为了演示连接SQL语句,当然实际 ...
- oracle(sql)基础篇系列(二)——多表连接查询、子查询、视图
多表连接查询 内连接(inner join) 目的:将多张表中能通过链接谓词或者链接运算符连接起来的数据查询出来. 等值连接(join...on(...=...)) --选出雇员的名字和雇员所 ...
随机推荐
- 导入appiumlibrary显红
1.点击ride中的Tools的View RIDE Log 2.打开日志报如下错误,提示没有six这个模块,可能是最新包的要对python版本兼容 3.所以使用pip install six安装包后 ...
- BZOJ2744 HEOI2012朋友圈(二分图匹配)
先考虑B国.容易发现a xor b mod 2=0即二进制末位相同,那么可以据此将所有人分成两部分,每一部分各自是一个完全图.然后再将a or b有奇数个1的边连上,现在需要求的就是这样一个图里的最大 ...
- SSM 全局异常
转载: http://blog.csdn.net/m13321169565/article/details/7641978 废话不多,直接说重点. 一 创建异常拦截类 (这里将 webapi 和 ...
- Jamie's Contact Groups POJ - 2289(多重匹配 最大值最小化 最大流)
Jamie's Contact Groups Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 8567 Accepted: ...
- IntelliJ IDEA2017 修改缓存文件的路径
IDEA的缓存文件夹.IntelliJIdea2017.1,存放着IDEA的破解密码,各个项目的缓存,默认是在C盘的用户目录下,目前有1.5G大小.现在想要把它从C盘移出. 在IDEA的安装路径下中, ...
- MT【172】内外圆
$P,Q$是两个定点,M为平面内一个动点,且$\dfrac{|MP|}{|MQ|}=\lambda(\lambda>0,\lambda\ne1)$, 点M的轨迹围成的区域面积为S , 设$S=f ...
- 【bzoj4516】 Sdoi2016—生成魔咒
http://www.lydsy.com/JudgeOnline/problem.php?id=4516 (题目链接) 题意 依次向字符串末尾加上一个字符,每次求不同子串个数. Solution 如果 ...
- sql知识收集
在SQL Server里面有top关键字可以很方便的取出前N条记录,但是Oracle里面却没有top的使用,类似实现取出前N条记录的简单方法如下: 方法1:利用ROW_NUMBER函数 取出前5条记录 ...
- POJ 3159 Candies (图论,差分约束系统,最短路)
POJ 3159 Candies (图论,差分约束系统,最短路) Description During the kindergarten days, flymouse was the monitor ...
- 安装【Jenkins】
前言 jenkins的一款持续集成工具, 它可以做的事情很多,其中一个主要的功能就是简化部署流程 回想一下我们的发布流程: 1.本地把项目打包 ...