collection 一对多和association的多对一关系

学生和班级的一对多的例子

班级类:

package com.glj.pojo;import java.io.Serializable;
import java.util.List;public class Clazz implements Serializable{private Integer id;private String code;private String name;//班级与学生是一对多的关系private List<Student> students;
//省略set/get方法
}

学生类:

package com.glj.pojo;import java.io.Serializable;public class Student implements Serializable {private Integer id;private String name;private String sex;private Integer age;//学生与班级是多对一的关系private Clazz clazz;
//省略set/get方法
}


ClazzMapper使用到了集合-collection 即为一对多,一个班级面对多个学生

package com.glj.mapper;import com.glj.pojo.Clazz;public interface ClazzMapper {Clazz selectClazzById(Integer id);
}

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.glj.mapper.ClazzMapper"><select id="selectClazzById" parameterType="int" resultMap="clazzResultMap">select * from tb_clazz where id = #{id}</select><resultMap type="com.glj.pojo.Clazz" id="clazzResultMap"><id property="id" column="id"/><result property="code" column="code"/><result property="name" column="name"/><!-- property: 指的是集合属性的值, ofType:指的是集合中元素的类型 --><collection property="students" ofType="com.glj.pojo.Student"column="id" javaType="ArrayList"fetchType="lazy" select="com.glj.mapper.StudentMapper.selectStudentByClazzId"><id property="id" column="id"/><result property="name" column="name"/><result property="sex" column="sex"/><result property="age" column="age"/></collection></resultMap>
</mapper>


StudentMapper则是与班级为多对一关系,所以使用了关联-association

package com.glj.mapper;import com.glj.pojo.Student;public interface StudentMapper {Student selectStudentById(Integer id);
}

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.glj.mapper.StudentMapper"><select id="selectStudentById" parameterType="int" resultMap="studentResultMap">select * from tb_clazz c,tb_student s where c.id = s.id and s.id = #{id}</select><select id="selectStudentByClazzId" parameterType="int" resultMap="studentResultMap">select * from tb_student where clazz_id = #{id}</select><resultMap type="com.glj.pojo.Student" id="studentResultMap"><id property="id" column="id"/><result property="name" column="name"/><result property="sex" column="sex"/><result property="age" column="age"/><association property="clazz" javaType="com.glj.pojo.Clazz"><id property="id" column="id"/><result property="code" column="code"/><result property="name" column="name"/></association></resultMap>
</mapper>

来自为知笔记(Wiz)

转载于:https://www.cnblogs.com/leccoo/p/11105417.html

Mybatis中的collection和association一关系相关推荐

  1. Mybatis中的collection、association来处理结果映射

    前不久的项目时间紧张,为了尽快完成原型开发,写了一段效率相当低的代码. 最近几天闲下来,主动把之前的代码优化了一下:) 标签:Java.Mybatis.MySQL 概况:本地系统从另外一个系统得到实体 ...

  2. mybatis 配置文件中,collection 和 association 的对应关系

    mybatis 配置文件中,collection 和 association 的对应关系  如下图所示:

  3. 一文理清Mybatis中resultType与resultMap之间的关系和使用场景

    点击蓝色"程序猿DD"关注我 回复"资源"获取独家整理的学习资料! 1.概述 Mybatis ORM半自动映射框架对java开发工程师来说应该是必会的框架之一. ...

  4. mybatis中,collection配置后查询只显示一条记录

    描述一下问题: 已知有两个表,一个是user表,一个是address,一(user)对多(address)的关系,在user的实体类里面写属性: private List<Address> ...

  5. mybatis 中 foreach collection的三种用法

    oreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合. foreach元素的属性主要有 item,index,collection,open,separator,close. i ...

  6. MyBatis中的collection使用方法

    demo: 实体:Position @TableName("tb_position") public class Position {private static final lo ...

  7. Mybatis中Collection集合标签的使用

    mybatis简单的CURD就不用多说了,网上相关博客文档一大堆.分析一下Mybatis里面的collection聚集查询.  假设一个班级有多名学生为例,通过班级号查询出该班级的信息,和班级里面的所 ...

  8. 关于mybatis中mapper文件resultMap中collection和association的使用

    mybatis mapper配置文件结果集映射resultMap中collection属性(一对多关系结果集映射)和association属性(多对一关系结果集映射)理解: collection的使用 ...

  9. mybatis collection标签_一对多的关系,在MyBatis中如何映射?

    # 使用collection标签 需求:根据用户id查询用户信息的同时获取用户拥有的角色,一个用户可以拥有1个或多个角色. 一般情况下,不建议直接修改数据库表对应的实体类. 所以这里我们延用之前博客中 ...

最新文章

  1. 玩音乐,敲架子鼓,一个被“耽误了”的机器学习高手
  2. 站立会议报告(12)
  3. 【Android应用开发】Android Studio - MAC 版 - 快捷键详解
  4. 【Python学习系列十五】pandas库DataFrame行列操作使用方法
  5. 一个FragmentActivity多个Fragment的生命周期事件记录
  6. okhttp3 请求html页面,OkHttp3源码详解(二) 整体流程
  7. 复习 Python 匿名函数 内建函数
  8. c语言取反运算详细步骤,C语言之位运算详解
  9. 【Paper】2021_Distributed sliding mode control for time-varying formation tracking of multi-UAV system
  10. 幅度调制(线性调制)原理
  11. Google Earth Engine(GEE)——ee.Reducer.percentile筛选影像百分比案例分析
  12. 经验贴: 如何选购相机, 电脑, 手机, 电脑配件?
  13. U盘修复“系统找不到指定文件”问题记录
  14. 如何看懂k线图:K线详细分析图解
  15. 详解HTML的相对路径写法
  16. 华为上机英文数字翻译
  17. very very good,Java面试宝典+Java核心知识集
  18. 55个常用的JavaScript网页脚本
  19. 嵌入式工程师“中年危机”应对策略上
  20. MySql系列06:数据库对象

热门文章

  1. 商户权限表mysql_MySQL 事务之 Yii2.0 商户提现
  2. 【云图】如何制作全国×××查询系统?
  3. # 再次尝试 连接失败_新一代高效连接池HikariCP设计简要分析
  4. (StreamReader.ReadLine()==null)还是(-1 != StreamReader.Peek())?
  5. 对象当前正在其他地方使用_2019 为什么我们还会继续使用 PHP ?
  6. 2020-08-21 Qt+MSVC 强制中文UTF-8编码
  7. nftables-howto-zh中文手册(不完整)
  8. 计算机专业课教学,计算机专业课教学的原则和方法
  9. Golang服务端开发及微服务架构
  10. php路径详解,详解与PHP路径相关的dirname,realpath,__FILE__函数