知识点: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. Java机器学习软件介绍

    Java机器学习软件介绍 编写程序是最好的学习机器学习的方法.你可以从头开始编写算法,但是如果你要取得更多的进展,建议你采用现有的开源库.在这篇文章中你会发现有关Java中机器学习的主要平台和开放源码 ...

  2. 吴裕雄--天生自然Linux操作系统:Linux 忘记密码解决方法

    忘记Linux系统的root密码,linux系统忘记root密码的情况该怎么办呢?重新安装系统吗?当然不用!进入单用户模式更改一下root密码即可. 步骤如下: 重启linux系统 3 秒之内要按一下 ...

  3. spring boot集成MyBatis 通用Mapper 使用总结

    spring boot集成MyBatis 通用Mapper 使用总结 2019年 参考资料: Spring boot集成 MyBatis 通用Mapper SpringBoot框架之通用mapper插 ...

  4. body书写总框架

    Body-reason 1:Topic sentence 2-n:解释or/and 举例 段内结构: 主题句+解释 主题句+举例 主题句+解释+举例:逐渐细化 不要每一段格式一致

  5. ZJNU 1205 - 侦探推理——高级

    双层枚举嫌疑犯与当日是星期几,统计真话与假话是否满足题意 注意 fake<=N&&fake+neutral>=N 即假话数量不大于N,假话加上没用的废话数量不小于N (注意 ...

  6. Java常见异常说明汇总

    1. java.lang.nullpointerexception 这个异常大家肯定都经常遇到,异常的解释是"程序遇上了空指针",简单地说就是调用了未经初始化的对象或者是不存在的对 ...

  7. TPO5-3 The Cambrian Explosion

    At one time, the animals present in these fossil beds were assigned to various modern animal groups, ...

  8. random mating

    随机交配种群 孟德尔分离(基于diploid and sexual)和随机交配(1.不因突变而改变的规律2.可计算的)是群体遗传学的基础. 随机交配(random mating)指群体中每一个成员与另 ...

  9. 用PrintStream向文件输入内容

    import java.io.*; public class Main { public static void main(String[] args) throws FileNotFoundExce ...

  10. 【转】Linux虚拟终端命令Screen用法详解

    转自 http://www.linuxidc.com/Linux/2013-07/87415.htm 在使用ssh或者telnet登录远程主机后,执行一些耗时的命令,如果此时ssh或者telnet中断 ...