数据库E-R关系

实体类

public class City {
Long id;
String name;
Long countryId;
Date lastUpdate;
}
public class Country {
Long id;
String name;
Date lastUpdate;
}
public class CityPlus {
Long id;
String name;
Long countryId;
Date lastUpdate;
Country country;
}
public class CountryPlus {
Long id;
String name;
Date lastUpdate;
List<City> cityList;
}

一对一关联查询

  一对一关联查询可采用的方式有:

  1. 单步查询,通过级联属性赋值

    • result标签级联属性赋值
    • association标签级联属性赋值
  2. 分步查询

单步查询

  • 数据模型:一个实体Bean中包含另外一个实体Bean
  • SQL查询:关联SQL 查询语句,如inner join、left join、right join
  • 具体实现方式:
    • 为级联属性赋值
    • association标签

采用相同的select标签

    <select id="selectCityPlusById" resultMap="cityPlusResultMap">
select city_id,city,city.country_id as country_id,city.last_update as last_update,
country.country_id as country_country_id,country,country.last_update as country_last_update
from city,country
where city.country_id = country.country_id and city_id=#{id}
</select>

result标签级联属性赋值

        <id column="city_id" property="id"/>
<result column="city" property="name"/>
<result column="country_id" property="countryId"/>
<result column="last_update" property="lastUpdate"/>
<result column="country_country_id" property="country.id"/>
<result column="country" property="country.name"/>
<result column="country_last_update" property="country.lastUpdate"/>
</resultMap>

 association标签级联属性赋值

  • 需要指定级联实体Bean在上级Bean中的属性名称,即association标签的property属性;
  • 需要指定级联实体Bean的类型,即association标签的javaType属性;
  • association标签的内部和resultMap标签内部具有相同的结构;
  • association标签也可以嵌套association标签;
    <resultMap id="cityPlusResultMap" type="canger.study.chapter04.bean.CityPlus">
<id column="city_id" property="id"/>
<result column="city" property="name"/>
<result column="country_id" property="countryId"/>
<result column="last_update" property="lastUpdate"/>
<association property="country" javaType="canger.study.chapter04.bean.Country">
<id column="country_country_id" property="id"/>
<result column="country" property="name"/>
<result column="country_last_update" property="lastUpdate"/>
</association>
</resultMap>

分步查询

  分步查询是指通过两次(或更多次)的查询,来为一个一对一关系的实体Bean赋值。

  • 数据模型:一个实体Bean中包含另外一个实体Bean
  • SQL查询:简单SQL语句,不存在关联查询
  • 只能通过association标签实现;

第一步查询

    <select id="selectCityPlusByIdUnderStep" resultMap="cityPlusResultMapStep">
select city_id,city,country_id
from city
where city_id=#{id}
</select>

association标签:

  • 需要指定级联实体Bean在上级Bean中的属性名称,即association标签的property属性;
  • 需要指定下一步查询需要使用的select语句,即association标签的select属性,该属性值为Mapper接口查询方法的全限定名;
  • 需要使用column属性,用于指定第二步查询的输入参数,第二步查询只有一个输入参数时,使用第一步查询结果的column名称即可;
    <resultMap id="cityPlusResultMapStep" type="canger.study.chapter04.bean.CityPlus">
<id column="city_id" property="id"/>
<result column="city" property="name"/>
<result column="country_id" property="countryId"/>
<result column="last_update" property="lastUpdate"/>
<association property="country"
select="canger.study.chapter04.mapper.CountryMapper.selectCountryById"
column="country_id">
</association>
</resultMap>

第二步查询

    <select id="selectCountryById" resultMap="countryResultMap">
select *
from country
where country_id=#{id}
</select>
    <resultMap id="countryResultMap" type="canger.study.chapter04.bean.Country">
<id column="country_id" property="id"/>
<result column="country" property="name"/>
<result column="last_update" property="lastUpdate"/>
</resultMap>

分步查询中,第二步查询需要多个参数

当第二步查询需要多个参数时,column属性的设置方式为  {param1=column1,param2=column2},其中param1,param2...为第二步查询的mapper映射文件的SQL语句中所使用的引用参数名称,column1,column2...为第一步查询返回的列名称,示例如下:

第一步

    <select id="selectCityPlusByIdUnderStep" resultMap="cityPlusResultMapStep">
select city_id,city,country_id, "Spain" as countryName
from city
where city_id=#{id}
</select>
    <resultMap id="cityPlusResultMapStep" type="canger.study.chapter04.bean.CityPlus">
<id column="city_id" property="id"/>
<result column="city" property="name"/>
<result column="country_id" property="countryId"/>
<result column="last_update" property="lastUpdate"/>
<association property="country"
select="canger.study.chapter04.mapper.CountryMapper.selectCountryByIdAndName"
column="{id=country_id,name=countryName}">
</association>
</resultMap>

第二步

    <select id="selectCountryByIdAndName" resultMap="countryResultMap">
select *
from country
where country_id=#{id} and country=#{name}
</select>
    <resultMap id="countryResultMap" type="canger.study.chapter04.bean.Country">
<id column="country_id" property="id"/>
<result column="country" property="name"/>
<result column="last_update" property="lastUpdate"/>
</resultMap>

MyBatis关联查询,一对一关联查询的更多相关文章

  1. Mybatis学习4——一对一关联查询方法2------实体作为属性

    实体order和user采用resultMap order package pojo; import java.util.Date; public class Order { private Inte ...

  2. Mybatis学习4——一对一关联查询方法1--创建实体

    创建一个实体继承两个实体之一,另一个实体作为属性 实体1. order package pojo; import java.util.Date; public class Order { privat ...

  3. 阶段3 1.Mybatis_12.Mybatis注解开发_6 mybatis注解开发一对一的查询配置

    新建Account实体类 生成getter和setter还有toString方法 先创建dao类 全局的配置,这里要改成package 创建多对一的关系 在查询的时候输出user这个对象的内容 建立查 ...

  4. mybatis学习:mybatis注解开发一对一的查询配置

    实体类: public class Account { private Integer id; private Integer uid; private Double money; private U ...

  5. MyBatis从入门到放弃三:一对一关联查询

    前言 简单来说在mybatis.xml中实现关联查询实在是有些麻烦,正是因为起框架本质是实现orm的半自动化. 那么mybatis实现一对一的关联查询则是使用association属性和resultM ...

  6. MyBatis:一对一关联查询

    MyBatis从入门到放弃三:一对一关联查询 前言 简单来说在mybatis.xml中实现关联查询实在是有些麻烦,正是因为起框架本质是实现orm的半自动化. 那么mybatis实现一对一的关联查询则是 ...

  7. 【Mybatis】MyBatis之表的关联查询(五)

    本章介绍Mybatis之表的关联查询 一对一关联 查询员工信息以及员工的部门信息 1.准备表employee员工表,department部门表 CREATE TABLE `employee` ( `i ...

  8. mybatis的动态sql编写以及一对一关系查询和一对多的查询

    创建mybatis数据库,运行以下sql语句 /* SQLyog Ultimate v8.32 MySQL - 5.5.27 : Database - mybatis **************** ...

  9. Hibernate多对多单向关联和双向关联 --Hibernate框架

    Hibernate关联关系中相对比较特殊的就是多对多关联,多对多关联与一对一关联和一对多关联不同,多对多关联需要另外一张映射表用于保存多对多映射信息.本例介绍多对多单向关联和双向关联.单向关联 :指具 ...

  10. mybatis一对一关联查询——(八)

    1.需求 查询所有订单信息,关联查询下单用户信息. 注意: 因为一个订单信息只会是一个人下的订单,所以从查询订单信息出发关联查询用户信息为一对一查询.如果从用户信息出发查询用户下的订单信息则为一对多查 ...

随机推荐

  1. Centos7 安装Nodejs

    使用EPEL安装 EPEL(Extra Packages for Enterprise Linux)企业版Linux的额外软件包,是Fedora小组维护的一个软件仓库项目,为RHEL/CentOS提供 ...

  2. JS五星级评分效果(类似与淘宝打分效果)

    今天晚上研究下 五星级评分效果,类似于淘宝后台评分效果,如下图所示: 思路: 当鼠标移到一颗星的时候 判断当前的索引 当前及当前的索引前面的星星亮起来 每当移到任何一颗星星时候 下面跟随提示 mous ...

  3. Arduino入门笔记(9):蓝牙模块及第一辆蓝牙遥控小车

    转载请注明:@小五义 http://www.cnblogs.com/xiaowuyi 欢迎加入讨论群 64770604 一.本次实验所需器材 1.Arduino UNO板 https://item.t ...

  4. HDU 1421 搬寝室(经典DP,值得经常回顾)

    搬寝室 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status D ...

  5. 几个简单易懂的排序算法php

    几个简单易懂的排序算法.排序算法,在应用到解决实际问题的时候(由于不一定总是数字排序),重点要分析出什么时候该交换位置. <?php // 冒泡排序 function bubble_sort(a ...

  6. C语言中猴子吃桃问题

    猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个.第二天早上又将第一天剩下的桃子吃掉一半,有多吃了一个.以后每天早上都吃了前一天剩下的一半零一个.到第 10 天早上想再吃时 ...

  7. 数据结构与算法之Stack(栈)的应用——用stack实现一个计算器-/bin/calc.dart

    计算器的bin/calc.dart 可执行代码: import 'dart:io'; import 'package:data_struct/stack/sample/calculator.dart' ...

  8. Exp5:MSF基础应用

    Exp5:MSF基础应用 一.基础问题回答 (1)用自己的话解释什么是 exploit , payload , encode. exploit: 设相当于利用漏洞偷偷打开的管道,将做好的木马病毒等顺利 ...

  9. 20155237 第十一周java课堂程序

    20155237 第十一周java课堂程序 内容一:后缀表达式 abcde/-f+ 内容二:实现Linux下dc的功能,计算后缀表达式的值 填充下列代码: import java.util.Scann ...

  10. 20155238 Java第13周课堂实践

    类定义 实验内容及要求 设计并实现一个Book类,定义义成Book.java,Book 包含书名,作者,出版社和出版日期,这些数据都要定义getter和setter.定义至少三个构造方法,接收并初始化 ...