Mybatis中的collection和association一关系
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一关系的更多相关文章
- Mybatis中的collection、association来处理结果映射
前不久的项目时间紧张,为了尽快完成原型开发,写了一段效率相当低的代码. 最近几天闲下来,主动把之前的代码优化了一下:) 标签:Java.Mybatis.MySQL 概况:本地系统从另外一个系统得到 ...
- Mybatis中使用collection进行多对多双向关联示例(含XML版与注解版)
Mybatis中使用collection进行多对多双向关联示例(含XML版与注解版) XML版本: 实体类: @Data @NoArgsConstructor public class Course ...
- 谈一下思考,关于mybatis中<foreach collection="list">中list得来的原因 没看到官方说明
<foreach> 是在sql语句中进行多个id查询 时用到的,因为mybatis代替jdbc和hibernate, 使用 在xml文件中编写sql语句,这是一个标签文件.然后在 dao层 ...
- mybatis 中 foreach collection的三种用法(转)
文章转自 https://blog.csdn.net/qq_24084925/article/details/53790287 oreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集 ...
- mybatis 中 foreach collection的三种用法
foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合. foreach元素的属性主要有 item,index,collection,open,separator,close. ...
- Mybatis 中 foreach collection 的三种用法
foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合. foreach元素的属性主要有 item,index,collection,open,separator,close. ...
- mybatis中foreach collection的三种用法
foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合. foreach元素的属性主要有 item,index,collection,open,separator,close. ...
- MyBatis中jdbcType和javaType的映射关系
JDBC Type Java Type CHAR String VARCHAR String LONGVARCHAR String NUMERIC java.math.BigDecimal DECIM ...
- mybatis中collection和association的作用以及用法
deptDaoMapper.xml 部门对应员工(1对多的关系) <resultMap type="com.hw.entity.Dept" id="deptinfo ...
随机推荐
- VS2015编译环境下CUDA安装配置
CUDA下载 CUDA是NVIDIA推出的通用并行计算架构,该架构使GPU能够解决复杂的计算问题,CUDA只支持NVIDIA自家的显卡,过旧的版本型号也不被支持. 下载地址:https://devel ...
- python 教程 第十二章、 标准库
第十二章. 标准库 See Python Manuals ? The Python Standard Library ? 1) sys模块 import sys if len(sys.argv) ...
- WPF使用MediaElement显示gif图片
原文:WPF使用MediaElement显示gif图片 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/SANYUNI/article/details ...
- 用curl访问HTTPS站点并登录(对HTTP返回的结果特别清楚)
开发网站,少不了测试.现在的网站为了加强安全性,都启用了HTTPS协议.所谓HTTPS,也就是HTTP文本在SSL协议中传输.用curl命令行来测试HTTPS站点是个很有用的功能,写点脚本,就可以做功 ...
- ubuntu 16.04快速安装ceph集群
准备工作 假设集群: 选一台作管理机 注意: ceph集群引用hostname,而非ip. 172.17.4.16 test16 #hostname必须是test16 172.17.4.17 test ...
- JS 扩展方法prototype
通过类对象的prototype设置扩展方法,下面为String对象增加quote(两边加字符)方法 <script type="text/javascript"> St ...
- MyBatis 问题 & 解决
# 问题 Invalid bound statement (not found) # 解决 <mappers> 标签的包括的是 SQL 语句存在的地方,此外 <mapper> ...
- Selenium-actions
一.鼠标事件 清单 1. 鼠标左键点击 Actions action = new Actions(driver);action.click();// 鼠标左键在当前停留的位置做单击操作 action. ...
- NPOI在无Office环境下,对Office文件的操作
在做项目的时候,经常会遇到对 Office的操作,但有时候会没有Office环境,因此给大家介绍一个思路,在没有Office环境下,对Office的处理. NPOI,顾名思义,就是POI的.NET版本 ...
- [原译]实现IEnumerable接口&理解yield关键字
原文:[原译]实现IEnumerable接口&理解yield关键字 著作权声明:本文由http://leaver.me 翻译,欢迎转载分享.请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢 ...