数据库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. Perl 杂记

    1. Perl 变量: 创建变量是以 $ 开头,比如定义一个变量: $val = "Good job !" 2. Perl 控制流 if 语法: if ( ) {  },注意if ...

  2. JAVA框架 Mybaits 一对一、一对多

    一:阐述 我们在日常操作的时候,很多时候会遇到多表联合查询,由于参照物的不通 ,会出现一对一.一对多的情况.比如说:账号信息和订单表,从订单表角度和账号信息是一对一的情况(一个订单只能是一个用户的情况 ...

  3. (三)Lua脚本语言入门(数组)

    又要找工作了,变的忧虑了,唯有学习才让内心变得踏实,今天玩了一下午的王者荣耀,正事都忘了...... 如果认为所谓的毅力是每分每秒的“艰苦忍耐”式的奋斗,那这是一种很不足的心理状态.毅力是一种习惯,毅 ...

  4. 【本地服务器】利用openssl生成证书

    (一)下载openssl软件,解压,进入bin目录 下载地址 (二)1.在当前bin目录,按住shift键右击,选择"在此处打开命令窗口" 2.打开cmd命令窗口之后,在窗口中输入 ...

  5. Winniechen’s test1

    https://winniechen.cn/wp-content/uploads/2018/08/Winniechens_test_1.rar 放水练习赛,主要考察最短路,DP,状态压缩等知识点 题解 ...

  6. php web开发安全之sql注入和防范:(一)简单的select语句注入和防范

    sql注入主要是指通过在get.post请求参数中构造sql语句,以修改程序运行时所执行的sql语句,从而实现获取.修改信息甚至是删除数据的目的,sql被注入的原因主要是代码编写的有问题(有漏洞),只 ...

  7. Swoole Task 的应用

    目录 概述 代码 小结 扩展 参考文档 概述 Swoole 异步Task,主要实现调用异步任务的执行. 常用的场景:异步支付处理.异步订单处理.异步日志处理.异步发送邮件/短信等. Swoole 的实 ...

  8. 大数据入门第十七天——storm上游数据源 之kafka详解(三)其他问题

    一.kafka文件存储机制 1.topic存储 在Kafka文件存储中,同一个topic下有多个不同partition,每个partition为一个目录,partiton命名规则为topic名称+有序 ...

  9. 2017-2018-1 20155202 张旭 嵌入式C语言——时钟提取时分秒

    2017-2018-1 20155202 张旭 嵌入式C语言--时钟提取时分秒 任务要求: 在作业本上完成附图作业,要认真看题目要求. 提交作业截图 作弊本学期成绩清零(有雷同的,不管是给别人传答案, ...

  10. 20155301 Web基础

    20155301 Web基础 1.基础问题回答 (1)什么是表单 答: 表单是一个包含表单元素的区域. 表单元素是允许用户在表单中(比如:文本域.下拉列表.单选框.复选框等等)输入信息的元素 (2)浏 ...