案例   查询国家的同时,查询出国家下的省会信息!

01.使用单表的连接查询

创建对应的实体类 和数据库表

/**
*
*国家的实体类
*/
public class Country { private Integer cId; //国家的编号
private String cName; //国家的名称
//关联省会的属性
private Set<Provincial> provincials;
public Integer getcId() {
return cId;
}
public void setcId(Integer cId) {
this.cId = cId;
}
public String getcName() {
return cName;
}
public void setcName(String cName) {
this.cName = cName;
}
public Set<Provincial> getProvincials() {
return provincials;
}
public void setProvincials(Set<Provincial> provincials) {
this.provincials = provincials;
}
public Country(Integer cId, String cName, Set<Provincial> provincials) {
super();
this.cId = cId;
this.cName = cName;
this.provincials = provincials;
}
public Country() {
super();
}
@Override
public String toString() {
return "Country [cId=" + cId + ", cName=" + cName + ", provincials="
+ provincials + "]";
} }
/**
*
*省会对应的实体类
*/
public class Provincial {
private Integer pId; //省会的编号
private String pName; //省会名称 public Integer getpId() {
return pId;
}
public void setpId(Integer pId) {
this.pId = pId;
}
public String getpName() {
return pName;
}
public void setpName(String pName) {
this.pName = pName;
}
public Provincial(Integer pId, String pName) {
super();
this.pId = pId;
this.pName = pName;
}
public Provincial() {
super();
}
@Override
public String toString() {
return "Provincial [pId=" + pId + ", pName=" + pName + "]";
} }

创建对应的dao和mapper文件

public interface CountryDao {
/**
* 根据国家的id查询出国家的信息 以及国家下面的省会信息
*/
Country selectCountryById(Integer cId);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-Mapper.dtd">
<mapper namespace="cn.bdqn.dao.CountryDao"> <!-- 这里的resultMap和之前使用的不一样,哪怕属性和字段一致 也要书写
因为mybatis在底层封装的时候,是根据我们resultMap中写的属性来的 -->
<resultMap type="Country" id="countryMap">
<id property="cId" column="cid"/>
<result property="cName" column="cname"/>
<!-- 设置关联的集合属性 -->
<collection property="provincials" ofType="Provincial">
<id property="pId" column="pid"/>
<result property="pName" column="pname"/>
</collection>
</resultMap>
<!-- 这是单表的关联查询 不经常使用 因为 不能使用延迟加载 -->
<select id="selectCountryById" resultMap="countryMap">
select cid,cname,pid,pname from country,provincial
where cid=countryid and cid=#{xxx} <!--#{xxx} 参数的占位符 -->
</select>
</mapper>

在MyBatis.xml文件中 管理Mapper文件

<!-- 加载映射文件信息 -->
<mappers>
<mapper resource="cn/bdqn/dao/CountryMapper.xml" />
</mappers>

测试类代码

public class CountryTest {
CountryDao dao;
SqlSession session; @Before
public void before() {
// 因为需要关闭session 需要把session提取出去
session = SessionUtil.getSession();
dao = session.getMapper(CountryDao.class);
} @After
public void after() {
if (session != null) {
session.close();
}
} /**
* 根据国家的id查询出国家的信息 以及国家下面的省会信息
*/
@Test
public void test1() {
Country country = dao.selectCountryById(1);
System.out.println(country);
} }

02.使用多表的查询

修改mapper.xml文件中代码即可    其他代码不变

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-Mapper.dtd">
<mapper namespace="cn.bdqn.dao.CountryDao"> <select id="selectProvincialByCountryId" resultType="Provincial">
select pid,pname from provincial
where countryid=#{xxx}
<!--#{xxx} 对应的就是resultMap中 collection节点下面的column -->
</select> <resultMap type="Country" id="countryMap">
<id property="cId" column="cid"/>
<result property="cName" column="cname"/>
<!--设置关联的集合属性
select:需要关联的查询语句
column: select关联语句中需要的参数 -->
<collection property="provincials" ofType="Provincial"
select="selectProvincialByCountryId"
column="cid"/>
</resultMap> <!-- 多表的查询 经常使用 可以使用延迟加载策略 -->
<select id="selectCountryById" resultMap="countryMap">
select cid,cname from country where cid=#{xxx}
<!--#{xxx} 用户传递过来的ID -->
</select> </mapper>

mybatis07--关联查询一对多的更多相关文章

  1. mybatis实战教程二:多对一关联查询(一对多)

    多对一关联查询 一.数据库关系.article表和user表示多对一的关系 CREATE TABLE `article` ( `id` ) NOT NULL AUTO_INCREMENT, `user ...

  2. Mybatis夺标关联查询一对多实例

    <?xml version="1.0" encoding="UTF-8" ?>  <!DOCTYPE mapper PUBLIC " ...

  3. MyBatis 多表关联查询

    多表关联查询 一对多 单条SQL实现. //根据部门编号查询出部门和部门成员姓名public dept selectAll() thorws Excatipon; //接口的抽象方法 下面是对应接口的 ...

  4. Mybatis多表关联查询字段值覆盖问题

    一.错误展示 1.首先向大家展示多表关联查询的返回结果集 <resultMap id="specialdayAndWorktimeMap type="com.hierway. ...

  5. [NHibernate]一对多关系(关联查询)

    目录 写在前面 文档与系列文章 一对多查询 总结 写在前面 上篇文章介绍了nhibernate的一对多关系如何配置,以及级联删除,级联添加数据的内容.这篇文章我们将学习nhibernate中的一对多关 ...

  6. 7.Mybatis关联表查询(这里主要讲的是一对一和一对多的关联查询)

    在Mybatis中的管理表查询这里主要介绍的是一对一和一对多的关联查询的resultMap的管理配置查询,当然你也可以用包装类来实现.不过这里不说,做关联查询的步骤可以简单的总结为以下的几步: 1.分 ...

  7. 7.mybatis一对多关联查询

    和第5节一对一查询类似,但是不同的是,一对一使用的是association,而一对多使用collection. 实例: 1个班级Class,对应1个老师Teacher,对应多个学生Student 1. ...

  8. [转]NHibernate之旅(10):探索父子(一对多)关联查询

    本节内容 关联查询引入 一对多关联查询 1.原生SQL关联查询 2.HQL关联查询 3.Criteria API关联查询 结语 关联查询引入 在NHibernate中提供了三种查询方式给我们选择:NH ...

  9. MyBitis(iBitis)系列随笔之五:多表(一对多关联查询)

    MyBitis(iBitis)系列随笔之一:MyBitis入门实例 MyBitis(iBitis)系列随笔之二:类型别名(typeAliases)与表-对象映射(ORM) MyBitis(iBitis ...

  10. MyBatis从入门到放弃四:一对多关联查询

    前言 上篇学习了一对一关联查询,这篇我们学习一对多关联查询.一对多关联查询关键点则依然是配置resultMap,在resultMap中配置collection属性,别忽略了ofType属性. 搭建开发 ...

随机推荐

  1. Node.js文件编码格式的转换

    项目很多 lua 文件不是 utf-8格式,使用 EditPlus 查看的时候,显示为ASCII.还有的是带BOM的,带BOM倒好处理,之前写过,有一定规律. ASCII编码就比较蛋疼,通过搜索网上资 ...

  2. eclipse实现代码块折叠-类似于VS中的#region……#endregion

    背 景 刚才在写代码的时候,写了十几行可以说是重复的代码: 如果整个方法或类中代码多了,感觉它们太TM占地方了,给读者在阅读代码上造成很大的困难,于是想到能不能把他们“浓缩”成一行,脑子里第一个闪现出 ...

  3. import tensorflow 报错: tf.estimator package not installed.

    import tensorflow 报错: tf.estimator package not installed. 解决方案1: 安装 pip install tensorflow-estimator ...

  4. Linux:“awk”命令的妙用

    awk是一个强大的文本分析工具,简单来说awk就是把文件逐行读入,(空格,制表符)为默认分隔符将每行切片,切开的部分再进行各种分析处理. 0.基本用法 awk是一个强大的文本分析工具,简单来说awk就 ...

  5. iOS NSDictionary 转Json 去掉换行去掉空格

    //dic 转json 如果用系统自带的会出现空格. + (NSString *)returnJSONStringWithDictionary:(NSDictionary *)dictionary{ ...

  6. C#调用系统打印机和收银钱箱

    打印示例: StringBuilder builder = new StringBuilder();builder.AppendLine("--------------打印测试------- ...

  7. 无法解析依赖项。“Microsoft.Net.Http 2.2.29”与 'Microsoft.Net.Http.zh-Hans

    无法解析依赖项.“Microsoft.Net.Http 2.2.29”与 'Microsoft.Net.Http.zh-Hans 2.0.20710 约束: Microsoft.Net.Http (= ...

  8. 雅克比迭代算法(Jacobi Iterative Methods) -- [ mpi , c++]

    雅克比迭代,一般用来对线性方程组,进行求解.形如: \(a_{11}*x_{1} + a_{12}*x_{2} + a_{13}*x_{3} = b_{1}\) \(a_{21}*x_{1} + a_ ...

  9. Rabbit五种消息队列学习(二) – 简单队列

    队列结构图 P:消息的生产者 C:消息的消费者 红色:队列 生产者将消息发送到队列,消费者从队列中获取消息. 测试 1.连接MQ public static Connection getConnect ...

  10. 修改Egret引擎代码的方法

    某些情况下,我们需要修改Egret引擎的源码,我们可以在源码目录(一般如下:xxx\Egret\engine\x.x.x\src\egret)下直接修改ts代码. 在对应的项目下打开CMD命令行,输入 ...