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 mapper
PUBLIC "-//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 mapper
PUBLIC "-//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>

Mybatis中的collection和association一关系的更多相关文章

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

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

  2. Mybatis中使用collection进行多对多双向关联示例(含XML版与注解版)

    Mybatis中使用collection进行多对多双向关联示例(含XML版与注解版) XML版本: 实体类: @Data @NoArgsConstructor public class Course ...

  3. 谈一下思考,关于mybatis中<foreach collection="list">中list得来的原因 没看到官方说明

    <foreach> 是在sql语句中进行多个id查询 时用到的,因为mybatis代替jdbc和hibernate, 使用 在xml文件中编写sql语句,这是一个标签文件.然后在 dao层 ...

  4. mybatis 中 foreach collection的三种用法(转)

    文章转自 https://blog.csdn.net/qq_24084925/article/details/53790287 oreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集 ...

  5. mybatis 中 foreach collection的三种用法

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

  6. Mybatis 中 foreach collection 的三种用法

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

  7. mybatis中foreach collection的三种用法

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

  8. MyBatis中jdbcType和javaType的映射关系

    JDBC Type Java Type CHAR String VARCHAR String LONGVARCHAR String NUMERIC java.math.BigDecimal DECIM ...

  9. mybatis中collection和association的作用以及用法

    deptDaoMapper.xml 部门对应员工(1对多的关系) <resultMap type="com.hw.entity.Dept" id="deptinfo ...

随机推荐

  1. ASP.NET Page执行顺序(ASP.NET生命周期)

    此部分说明的生命周期只有部分: ---引用MSDN 阶段 说明 页请求 页请求发生在页生命周期开始之前.用户请求页时,ASP.NET 将确定是否需要分析和编译页(从而开始页的生命周期),或者是否可以在 ...

  2. 2-5 利用RestTemplateCore简化调用Consul中的服务

    1.必须要安装RestTemplateCore包 2.请求服务,必须要知道 a Consul服务器的地址:b 请求的服务名 ;c 具体请求的api接口 利用RestTemplateCore简化调用Co ...

  3. WPF的消息机制(三)- WPF内部的5个窗口之处理激活和关闭的消息窗口以及系统资源通知窗口

    原文:WPF的消息机制(三)- WPF内部的5个窗口之处理激活和关闭的消息窗口以及系统资源通知窗口 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/p ...

  4. MIPS之路在何方?

    目前市场上还有谁想要MIPS?MIPS接下来将何去何从?如果有一家公司希望能好好地经营MIPS,应该用什么策略呢?   MIPS仍然有营收来源.它还拥有ARM所没有的多执行绪技术.有人说,只要想到半导 ...

  5. HDU4421 Bit Magic 【2-sat】

    叙述性说明: 这给出了一个矩阵,原来的请求a排列 2-sat称号.对于每一位跑步边,跑31位可 详细的施工方 注意N=1的情况特判,还有检查对称元素是否同样 #include <stdio.h& ...

  6. libev整体设计

    转自:http://m.blog.csdn.NET/blog/weiqubo/16355653 libev是Marc Lehmann用C写的高性能事件循环库.通过libev,可以灵活地把各种事件组织管 ...

  7. WPF 调用资源图片

    原文:WPF 调用资源图片 最近做的wpf项目中,在开发的时候,把图片放到了bin下面,采用了imagePath =System.IO.Directory.GetCurrentDirectory()+ ...

  8. QT实现鼠标钩子(使用SetWindowsHookEx安装mouseProc函数)

    HHOOK mouseHook=NULL; LRESULT CALLBACK mouseProc(int nCode,WPARAM wParam,LPARAM lParam ) { if(nCode ...

  9. WPF Datagrid with some read-only rows - Stack Overflow

    原文:WPF Datagrid with some read-only rows - Stack Overflow up vote 21 down vote accepted I had the sa ...

  10. Angular 请求另一服务的api(请求代理)

    1.edit "start" of your package.json to look below 定义一个叫做start的新命令 "start": " ...