知识点:JavaType和ofType都是用来指定对象类型的,但是JavaType是用来指定pojo中属性的类型,而ofType指定的是映射到list集合属性中pojo的类型。

本次mybatis的练习是使用的springboot+mybatis

第一步:在mysql 数据库创建students 和cards表并插入数据

create table cards(
cid int(5) primary key,
cnum varchar(10)
); create table students(
sid int(5) primary key,
sname varchar(10),
scid int(5),
constraint scid_fk foreign key(scid) references cards(cid)
); insert into cards(cid,cnum) values(1,'111');
insert into students(sid,sname,scid) values(1,'哈哈',1); select * from cards;
select * from students;

第二步:配置springboot配置文件application.properties并在该文件添加以下配置信息

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/diamond?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver  

第三步:创建Domain: Student.java 和Card.java

import lombok.Data;

@Data
public class Card {
private Integer id;
private String num;
}

 

@Data
public class Student {
private Integer id;
private String name;
private Card card;
}

第四步:创建StudentCardMapper接口 

import com.longteng.diamond.domain.Student;
import org.apache.ibatis.annotations.Mapper; @Mapper
public interface StudentCardMapper {
/**
* 查询1号学生的信息
* @param id 表示学生的编号
*/
Student selectById(Integer id);
}

第五步:创建StudentCardMapper.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="com.longteng.diamond.dao.StudentCardMapper">
<resultMap id="studentCardMap" type="com.longteng.diamond.domain.Student">
<id property="id" column="sid" jdbcType="INTEGER"></id>
<result property="name" column="sname" jdbcType="VARCHAR"></result>
<!-- 引入CardMapper.xml文件中的映射信息
property表示Student类的关联属性
resultMap表示引入CardMapper.xml文件的映射类型
-->
</resultMap>
<select id="selectById" parameterType="java.lang.Integer" resultMap="studentCardMap">
select s.sid,s.sname,s.scid,c.cnum,c.cid
from students s inner join cards c
on s.scid= c.cid and s.sid=#{xx}
</select> </mapper>

第六步:创建Controller

import com.longteng.diamond.dao.StudentCardMapper;
import com.longteng.diamond.domain.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; @Controller
public class StudentCardController {
@Resource
StudentCardMapper studentCardMapper;
@RequestMapping("/testMapper")
public @ResponseBody String test(){
Student student = studentCardMapper.selectById(1);
return student.toString();
}
}

第七步:调用接口http://localhost/testMapper执行结果如下:

Student(id=1, name=哈哈, card=null)

其中card = null 是由于在第五步操作中resultMap 中没有关联card表里面的属性,而在select 查询中却查询了card表的属性值,解决办法有2种

方法1:直接将collection集合元素的属性写为collection的子标签

<?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="com.longteng.diamond.dao.StudentCardMapper">
<resultMap id="studentCardMap" type="com.longteng.diamond.domain.Student">
<id property="id" column="sid" jdbcType="INTEGER"></id>
<result property="name" column="sname" jdbcType="VARCHAR"></result>
<result property="cid" column="scid" jdbcType="INTEGER"></result>
<!-- 引入CardMapper.xml文件中的映射信息
property表示Student类的关联属性
resultMap表示引入CardMapper.xml文件的映射类型
-->
<collection property="card" ofType="com.longteng.diamond.domain.Card">
<id property="id" column="cid" ></id>
<result property="num" column="cnum" jdbcType="VARCHAR"></result>
</collection>
</resultMap> <select id="selectById" parameterType="java.lang.Integer" resultMap="studentCardMap">
select s.sid,s.sname,s.scid,c.cnum,c.cid
from students s inner join cards c
on s.scid= c.cid and s.sid=#{xx}
</select> </mapper> 

调用接口http://localhost/testMapper执行结果如下:

Student(id=1, name=哈哈, card=Card(id=1, num=111))

方法2:通过在collection标签中引用别的mapper的查询方法

第一步创建CardMapper接口

import com.longteng.diamond.domain.Card;
import org.apache.ibatis.annotations.Mapper; @Mapper
public interface CardMapper {
Card findCard(Integer id);
}

第二步创建CardMapper.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="com.longteng.diamond.dao.CardMapper">
<resultMap id="cardMapper" type="com.longteng.diamond.domain.Card">
<id property="id" column="cid" jdbcType="INTEGER"></id>
<result property="num" column="cnum" jdbcType="VARCHAR"></result>
</resultMap>
<select id="findCard" resultMap="cardMapper">
SELECT * from cards where cid=#{0};
</select>
</mapper>

第三步在StudentCardMapper.xml文件里面引入CardMapper.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="com.longteng.diamond.dao.StudentCardMapper">
<resultMap id="studentCardMap" type="com.longteng.diamond.domain.Student">
<id property="id" column="sid" jdbcType="INTEGER"></id>
<result property="name" column="sname" jdbcType="VARCHAR"></result>
<result property="cid" column="scid" jdbcType="INTEGER"></result>
<!-- 方法2:
引入CardMapper.xml文件中的映射信息
property表示Student类的关联属性 -->
<collection property="card" column="scid" jdbcType="INTEGER" select="com.longteng.diamond.dao.CardMapper.findCard"/>
</resultMap> <select id="selectById" parameterType="java.lang.Integer" resultMap="studentCardMap">
select s.sid,s.sname,s.scid,c.cnum,c.cid
from students s inner join cards c
on s.scid= c.cid and s.sid=#{xx}
</select> </mapper>

MyBatis学习总结之一对一映射的更多相关文章

  1. mybatis学习笔记(10)-一对一查询

    mybatis学习笔记(10)-一对一查询 标签: mybatis mybatis学习笔记10-一对一查询 resultType实现 resultMap实现 resultType和resultMap实 ...

  2. mybatis学习笔记(7)-输出映射

    mybatis学习笔记(7)-输出映射 标签: mybatis mybatis学习笔记7-输出映射 resultType 输出简单类型 输出pojo对象和pojo列表 resultMap result ...

  3. 1.4(Mybatis学习笔记)关联映射

    一.一对一 mybatis处理一对一主要通过<resultMap>中的<association>元素来处理. <association>元素主要使用方方式有两种: ...

  4. 【MyBatis学习10】高级映射之多对多查询

    本文来总结一下mybatis中的多对多映射,从第8节的文章中可以看出,用户表和商品表示多对多关系,它们两的多对多是通过订单项和订单明细这两张表所关联起来的,那么这一节主要来总结一下用户表和商品表之间的 ...

  5. 【MyBatis学习06】输入映射和输出映射

    在前面几篇博文的例子中也可以看到mybatis中输入映射和输出映射的身影,但是没有系统的总结一下,这篇博客主要对这两个东东做一个总结.我们知道mybatis中输入映射和输出映射可以是基本数据类型.ha ...

  6. 【MyBatis学习08】高级映射之一对一查询

    从这一篇博文开始,将总结一下mybatis中的几个高级映射,即一对一.一对多.多对多查询,这篇先总结一下mybatis中的一对一查询.  为了模拟这些需求,事先要建立几个表,不同的表之间将对应上面提到 ...

  7. mybatis学习记录六——一对一、一对多和多对多查询

    9       订单商品数据模型 9.1     数据模型分析思路 1.每张表记录的数据内容 分模块对每张表记录的内容进行熟悉,相当 于你学习系统 需求(功能)的过程. 2.每张表重要的字段设置 非空 ...

  8. Mybatis学习(三)————— 映射文件详解

    前面说了全局配置文件中内容的详解,大家应该清楚了,现在来说说这映射文件,这章就对输入映射.输出映射.动态sql这几个知识点进行说明,其中高级映射(一对一,一对多,多对多映射)在下一章进行说明. 一.输 ...

  9. 【MyBatis学习09】高级映射之一对多查询

    上一篇博文总结了一下一对一的映射,本文主要总结一下一对多的映射,从上一篇文章中的映射关系图中可知,订单项和订单明细是一对多的关系,所以本文主要来查询订单表,然后关联订单明细表,这样就有一对多的问题出来 ...

随机推荐

  1. C盘满了解决办法之pagefile.sys文件

    pagefile.sys文件一般存在于C盘,只有点击了隐藏属性才能看见. 这个文件一般比较大,它是系统创建虚拟内存页面的文件.平时大家使用软件的时候对于产生大量的临时数据,这些数据需要占用大量内存,如 ...

  2. Linux进程的引入

    1.什么是进程? (1).进程是一个动态过程而不是静态实物 (2).进程就是程序的一次运行过程,一个静态的可执行程序a.out的一次运行过程(./a.out从运行到结束)就是一个进程. (3).进程控 ...

  3. python3下scrapy爬虫(第八卷:循环爬取网页多页数据)

    之前我们做的数据爬取都是单页的现在我们来讲讲多页的 一般方式有两种目标URL循环抓取 另一种在主页连接上找规律,现在我用的案例网址就是 通过点击下一页的方式获取多页资源 话不多说全在代码里(因为刚才写 ...

  4. 剑指offer【13】- 链表中倒数第k个结点

    输入一个链表,输出该链表中倒数第k个结点. /* public class ListNode { int val; ListNode next = null; ListNode(int val) { ...

  5. python中__call__方法

    在 Python 中提供了__call__ 方法,允许创建可调用的对象(实例).如果类中实现了 __call__ 方法,则可以像使用函数一样使用类. 例如简单的封装一个接口 get/post 方法: ...

  6. 001-rabbitmq和haproxy结合

    rabbitmq集群搭建 防火墙添加并重启 -A INPUT -p tcp -m multiport --dports 4369,25672,5672,15672 -j ACCEPT 安装 cd /u ...

  7. redhat下libreoffice 的安装

    1.第一次安装libreoffic时是用网络yum源安装的,但是装好之后不能用,找了好久没有找出问题,后来从官网下载安装包后安装就可以了. 下载地址:https://zh-cn.libreoffice ...

  8. 【转】高频使用的git清单

    侵删 作者: 阮一峰 链接: http://www.ruanyifeng.com/blog/2015/12/git-cheat-sheet.html 我每天使用 Git ,但是很多命令记不住. 一般来 ...

  9. 一、Shell脚本高级编程实战第一部

    Shell脚本语言是实现linux系统自动化管理的重要且必要的工具,几乎每一个合格的linux系统管理员或者高级运维工程师都要熟练shell脚本语言的编写,只有这样才能提升工作效率,解决工作中的重复劳 ...

  10. getopt|sys|open|print文件|main()|if __name__ == "__main__"|getline()

    #!/usr/bin/python import sys import getopt import re def compare(f1,f2,o1,o2,si_line): lines_count=0 ...