一. Mybatis关联映射

1 讲数据库中关联关系,主键表与外键表

一对多,多对一,主键表表示一 与外键表表示多

学生与学校的关系,工人与工厂,人员与部门的关系,。。。。

多        一                多         一      多        一

2 多表查询

多表查询 方法一

select a.*,b.* from student a,school b  where a.t_sid=b.t_id

select a.*,b.t_name from student a,school b  where a.t_sid=b.t_id

select a.*,b.t_name from student a,school b  where a.t_sid=b.t_id and b.t_id=1

多表查询 方法二

select student.*,school.* from student inner join school on student.t_sid=school.t_id

select student.*,school.t_name from student inner join school on student.t_sid=school.t_id

select student.*,school.t_name from student inner join school on student.t_sid=school.t_id and school.t_id=1

3  mybatis多对一

要查询多个表中信息:有三种方法:

方法一:

<resultMap type="com.softjx.model.Student" id="StudentMap1">
<result column="t_id" property="id"/>
<result column="t_name" property="studentName" />
<result column="t_age" property="age"/>
<result column="t_enterdate" property="enterDate"/>
<result column="t_sid" property="sid"/>
<!—关联字段-->
<result column="t_id1" property="school.id"/>
<result column="t_name1" property="school.schoolName"/> </resultMap> <!-- 根据id查询数据表中的一条记录 -->
<select id="getStudentId1" resultMap="StudentMap1">
select a.t_id,a.t_name,a.t_age,a.t_enterdate,a.t_sid,b.t_id as t_id1,b.t_name as t_name1 from student a,school b where a.t_sid=b.t_id and a.t_id=#{id};
</select>

方法二:使用association定义关联的单个对象

  <resultMap type="com.softjx.model.Student" id="StudentMap2">
<result column="t_id" property="id"/>
<result column="t_name" property="studentName" />
<result column="t_age" property="age"/>
<result column="t_enterdate" property="enterDate"/>
<result column="t_sid" property="sid"/>
<!-- association可以指定联合的javaBean对象
property="school":指定哪个属性是联合的对象
javaType:指定这个属性对象的类型[不能省略]
-->
<association property="school" javaType="com.softjx.model.School">
<id column="t_id" property="id"/>
<result column="t_name" property="schoolName"/>
</association>
</resultMap> <!-- 根据id查询数据表中的一条记录 -->
<select id="getStudentId2" resultMap="StudentMap2">
select a.t_id,a.t_name,a.t_age,a.t_enterdate,a.t_sid,b.t_id as t_id1,b.t_name as t_name1 from student a,school b where a.t_sid=b.t_id and a.t_id=#{id};
</select>

方法三:使用association进行分步查询

1)、先按照t_id查询学生信息

2)、根据查询学生信息中的t_sid值去学校表查出学校的信息

3)、学校信息设置到学生中;

1、先按照t_id查询学生信息
2、根据查询学生信息中的t_sid值去学校表查出学校的信息
3、学校信息设置到学生中; <resultMap type="com.softjx.model.Student" id="StudentMap3">
<result column="t_id" property="id"/>
<result column="t_name" property="studentName" />
<result column="t_age" property="age"/>
<result column="t_enterdate" property="enterDate"/>
<result column="t_sid" property="sid"/>
<!-- association定义关联对象的封装
select:表明当前属性再去调用哪个方法查出的结果
column:指定根据当前表中哪一列的值传给这个方法
流程:使用select指定的方法查出对象,并封装给property指定的属性
-->
<association property="school"
select="com.softjx.dao.SchoolMapper.getSchoolId"
column="t_sid">
</association> </resultMap>

使用association进行分步查询,可以是立即加载,延迟加载;

可以使用延迟加载(懒加载);(按需加载)

Student==>School:

我们每次查询Student对象的时候,都将一起查询出来,(立即加载)。

学校信息在我们使用的时候再去查询(延迟加载);

分段查询的基础之上(懒加载)加上两个配置在全局配置文件中config.xml:

<settings>

<setting name="lazyLoadingEnabled" value="true"/>

<setting name="aggressiveLazyLoading" value="false"/>

</settings>

4  mybatis一对多

方法一:使用collection标签

1)在学校中有一个集合类型,指向多个学生

private List<Student> students;

    public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}

2)在SchoolMapper.xml文件中配置collection标签,标签中ofType:指定集合里面元素的类型,property:指定bean的属性名。

<resultMap type="com.softjx.model.School" id="SchoolMap1">
<result column="t_id" property="id"/>
<result column="t_name" property="schoolName" />
<!--
collection定义关联集合类型的属性
ofType:指定集合里面元素的类型
-->
<collection property="students" ofType="com.softjx.model.Student">
<!-- 定义这个集合中元素的封装规则 -->
<result column="t_id1" property="id"/>
<result column="t_name1" property="studentName" />
<result column="t_age" property="age"/>
<result column="t_enterdate" property="enterDate"/>
<result column="t_sid" property="sid"/>
</collection>
</resultMap> <select id="getSchoolId1" resultMap="SchoolMap1">
select b.t_id as t_id,b.t_name as t_name ,a.t_id as t_id1 ,a.t_name as t_name1,a.t_age,a.t_enterdate, a.t_sid from school b, student a where b.t_id=a.t_sid and b.t_id=#{id}
</select>

方法二:使用collection分段:

1)首先要根据学校编号在学生表中查询所有学生:

<!-- 根据学生的学校id查询所有的学生 -->

public List<Student> getStudentsBySchoolId(Integer sid);

<select id="getStudentsBySchoolId" resultMap="StudentMap">
select * from student where t_sid=#{sid};
</select> 注意:这个sid是collection标签中要访问的。

2)Collection分段代码:

<!-- sid是方法的参数名,t_id是字段名 -->
<resultMap type="com.softjx.model.School" id="SchoolMap2">
<result column="t_id" property="id"/>
<result column="t_name" property="schoolName" />
<collection property="students"
select="com.softjx.dao.StudentMapper.getStudentsBySchoolId"
column="{sid=t_id}" fetchType="lazy">
</collection>
</resultMap> <select id="getSchoolId2" resultMap="SchoolMap2">
select * from school where t_id=#{id}
</select>

注意:

1)fetchType="lazy":表示使用延迟加载,默认是延迟;它的取值是 lazy:延迟加载,eager:立即加载。

2)多列的值传递:

column="{key1=column1,key2=column2}",key1是方法的参数名,column1是字段名, key2是方法的参数名,column2是字段名。

5  mybatis一对多,多对一(使用注解)

1)mybatis全局配置文件要应用接口类:

       <mappers>
<mapper class="com.softjx.dao.SchoolMapper"/>
<mapper class="com.softjx.dao.StudentMapper"/>
</mappers>

2)没有mapper.xml文件,只有接口文件:

多对一关系:

//根据id查询学生,包括学生的学校
//property="school",column="t_sid" column是当前表中关联字段名t_sid
//查询一个用户
@Select("select * from student where t_id=#{id}")
@Results({@Result(property="id",column="t_id")
,@Result(property="studentName",column="t_name")
,@Result(property="age",column="t_age")
,@Result(property="enterDate",column="t_enterdate")
,@Result(property="sid",column="t_sid")
,@Result(property="school",column="t_sid",one=@One(select="com.softjx.dao.SchoolMapper.getSchoolId",fetchType=FetchType.EAGER)) })
public Student getStudentId1(Integer id);

一对多的关系:

1.先要在多这一方查询数据。

//根据学生的学校id查询所有的学生
@Select("select * from student where t_sid=#{sid}")
@Results({@Result(property="id",column="t_id")
,@Result(property="studentName",column="t_name")
,@Result(property="age",column="t_age")
,@Result(property="enterDate",column="t_enterdate")
,@Result(property="sid",column="t_sid")
})
public List<Student> getStudentsBySchoolId(Integer sid); 2.
//根据id查询学校
//property="students",column="t_id" column是当前表中的主键字段名t_id
@Select("select * from school where t_id=#{id}")
@Results({@Result(property="id",column="t_id")
,@Result(property="schoolName",column="t_name")
,@Result(property="students",column="t_id",many=@Many(select="com.softjx.dao.StudentMapper.getStudentsBySchoolId",fetchType=FetchType.LAZY))
})
public School getSchoolId1(Integer id);

Mybatis(四)关联映射的更多相关文章

  1. 04—mybatis的关联映射

    mybatis的关联映射一对一一对多多对多 一.一对一(一个人只能有一个身份证号) 1.创建表创建表tb_card CREATE TABLE `tb_card` ( `id` int(11) NOT ...

  2. mybatis 一对一关联映射实例

    在实际项目开发中,经常存在一对一的关系,如一个人对应一张身份证信息,这就是一对一的关系.下面是一个简单的实例: 1.建表过程我就省略了,主要是一张Person表,一张IDCard表,其相关属性见步骤2 ...

  3. mybatis之关联映射

    ###mybatis使用之一对一关联映射 1)分析并画ER图.(特别是一对一.一对多.多对多的情况) 2)启动终端数据库,并建库建表,在表中插入值和字段,并查看结果.(后期把navicat用上) 3) ...

  4. Mybatis的关联映射案例

    主要是对之前学习的关联映射做一个案例,自己动手实践一下,可以理解的更好一点. 开发环境 开发工具:idea Java环境: jdk1.8.0_121 数据库:SQLServer 项目结构,里面包含了三 ...

  5. Mybatis的关联映射

    实际的开发中,对数据库的操作常常会涉及到多张表,这在面向对象中就涉及到了对象与对象之间的关联关系.针对多表之间的操作,MyBatis提供了关联映射, 通过关联映射就可以很好的处理对象与对象之间的关联关 ...

  6. Mybatis(四) 高级映射,一对一,一对多,多对多映射

    天气甚好,怎能不学习? 一.单向和双向 包括一对一,一对多,多对多这三种情况,但是每一种又分为单向和双向,在hibernate中我们就详细解析过这单向和双向是啥意思,在这里,在重复一遍,就拿一对多这种 ...

  7. MyBatis的关联映射和动态SQL

    CREATE TABLE tb_card ( id INT PRIMARY KEY AUTO_INCREMENT, CODE ) ); '); CREATE TABLE tb_person ( id ...

  8. 【Hibernate框架】关联映射(一对一关联映射)

    一.整理思路: 之前,小编总结过Mybatis的关联映射,接下来,再来总结一下hibernate的相关的关联映射,直接上图: 这张图,就是小编整理总结整个Hibernate的关联映射的一个大致思路. ...

  9. MyBatis学习(七)MyBatis关联映射之多对多映射

    对于数据库中的多对多关系建议使用一个中间表来维护关系. 1.创建四张表,分别为用户表,商品表,订单表,中间表. DROP TABLE IF EXISTS `t_user`; CREATE TABLE ...

随机推荐

  1. 关于tarjan算法的空间优化

    最近随着对tarjan算法理解的加深,我发现用另外一种途径实现tarjan的方法,且可以省去DFN数组,大大节省了空间.经过大量测试,已经无误.以下将分阶段阐述进行优化的过程. 第一阶段 下面来说一下 ...

  2. phalcon——调度控制器

    将侦听者绑定到组件上: use Phalcon\Mvc\Dispatcher as MvcDispatcher, Phalcon\Events\Manager as EventsManager; $d ...

  3. 《HelloGitHub》第 19 期

    前言 最近很少写博客了,工作上的事情太多(在做一些数据分析方面的工作,之前是 Web 开发),时间捉襟见肘.更多的时间都花在工作上,没有精力.时间积累整理知识.说来还是能力太差.效率有问题. 后面会好 ...

  4. 2_认识STM32库

    2_认识STM32库 STM32库是由ST公司针对STM32提供的函数接口API,开发者可以调用这些函数接口来配置STM32的寄存器,使得开发人员得以脱离最底层的寄存器操作,开发快速. 库是架设在寄存 ...

  5. YYHS-分数(二分+容斥)

    题目描述 KJDH是个十分善于探索的孩子,有一天他把分子分母小于等于n的最简分数列在了纸上,他想找到这些分数里第k小的数,这对于KJDH来说当然是非常轻易,但是KJDH最近多了很多妹子,他还要去找妹子 ...

  6. 蓝桥杯-算法训练--ALGO-6 安慰奶牛

    问题描述Farmer John变得非常懒,他不想再继续维护供奶牛之间供通行的道路.道路被用来连接N个牧场,牧场被连续地编号为1到N.每一个牧场都是一个奶牛的家.FJ计划除去P条道路中尽可能多的道路,但 ...

  7. ueditor 和 umeditor 粘贴过滤问题

    最近遇到需要将WORD WPS等复制的带有格式的内容粘贴到富文本编辑器里面去掉冗余的HTML,只保留最有用的部分. 第一步肯定是先查官方文档了. http://fex.baidu.com/uedito ...

  8. JavaScript系列----数据类型以及传值和传引用

    1.简单数据类型 在JavaScript中简单数据类型分为5种.分别为 Undefined, Null,Boolean,Number,String. Undefined类型Undefined类型只有一 ...

  9. 全排列Permutations

    描述 Given a collection of numbers, return all possible permutations.For example,[1,2,3] have the foll ...

  10. Python 直接赋值、浅拷贝和深度拷贝解析

    直接赋值:其实就是对象的引用(别名). 浅拷贝(copy):拷贝父对象,不会拷贝对象的内部的子对象. 深拷贝(deepcopy): copy 模块的 deepcopy 方法,完全拷贝了父对象及其子对象 ...