SSM框架下,mapper.xml 中 association 标签和 collection 标签的使用

当数据库中表与表之间有关联时,在对数据库进行操作时,就不只是针对某一张表了,需要联表查询

MyBatis中如何实现联表查询

1、首先新建两张表

学生表(student

  • ID:stu_id
  • 姓名:stu_name
  • 年龄:stu_age
  • 性别:stu_gender
  • 所在班级:g_id

班级表(grade

  • ID:g_id
  • 班级名称:g_name

学生表和班级表通过 g_id 进行关联,一个班级对应多个学生

2、创建相应的实体类和mapper接口

(1)创建 Student 类和 Grade 类(包名:com.bwlu.bean

(2)创建 StudentMapper 接口和 GradeMapper 接口和相应的 XML 文件(使用逆向生成可直接生成)

StudentMapper.java 接口

Student selectByPrimaryKey(Integer stuId);//按主键查询一条记录

StudentMapper.xml

<mapper namespace="com.bwlu.mapper.StudentMapper" >
<resultMap id="BaseResultMap" type="com.bwlu.bean.Student" >
<id column="stu_id" property="stuId" jdbcType="INTEGER" />
<result column="stu_name" property="stuName" jdbcType="VARCHAR" />
<result column="stu_age" property="stuAge" jdbcType="INTEGER" />
<result column="stu_gender" property="stuGender" jdbcType="INTEGER" />
<result column="g_id" property="gId" jdbcType="INTEGER" />
</resultMap>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select stu_id, stu_name, stu_age, stu_gender, g_id from student
where stu_id = #{stuId,jdbcType=INTEGER}
</select>
</mapper>

GradeMapper.java

Grade selectByPrimaryKey(Integer gId);//按主键查询一条记录

 GradeMapper.xml

<mapper namespace="com.bwlu.mapper.GradeMapper" >
<resultMap id="BaseResultMap" type="com.bwlu.bean.Grade" >
<id column="g_id" property="gId" jdbcType="INTEGER" />
<result column="g_name" property="gName" jdbcType="VARCHAR" />
</resultMap>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select g_id, g_name from grade
where g_id = #{gId,jdbcType=INTEGER}
</select>
</mapper>

3、在sql映射文件中写映射sql语句【联合查询:级联属性封装结果集】

3.1第一种

(1)在 Student.java 中将 g_id 属性换成班级类型(Grade),并添加相应的getter和setter方法

//private Integer g_id;
private Grade grade;
public Grade getGrade() {
return grade;
}
public void setGrade(Grade grade) {
this.grade = grade;
}

(2)在 xml 中封装结果集,并编写相应的 sql 语句

<resultMap id="BaseResultMap" type="com.bwlu.bean.Student" >
<id column="stu_id" property="stuId" jdbcType="INTEGER" />
<result column="stu_name" property="stuName" jdbcType="VARCHAR" />
<result column="stu_age" property="stuAge" jdbcType="INTEGER" />
<result column="stu_gender" property="stuGender" jdbcType="INTEGER" />
<result column="g_id" property="grade.gId" jdbcType="INTEGER" />
<result column="g_name" property="grade.gName" jdbcType="VARCHAR" />
</resultMap>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select stu_id, stu_name, stu_age, stu_gender, g.g_id, g_name
from student s,grade g
where s.g_id = g.g_id AND stu_id = #{stuId,jdbcType=INTEGER}
</select>

(3)测试

@Autowired
private StudentMapper studentMapper;
@Test
public void testAssociation() {
Student student = studentMapper.selectByPrimaryKey(6);
System.out.println(student);//Student [stuId=6, stuName=lixiang, stuAge=22, stuGender=1, grade=Grade [gId=3, gName=软件(3)班]]
}

3.2第二种

(1)在 Student.java 中将 g_id 属性换成班级类型(Grade),并添加相应的getter和setter方法,同 3.1 的(1)

(2)使用association来定义关联对象的规则【比较正规的,推荐的方式】

<resultMap id="BaseResultMap" type="com.bwlu.bean.Student" >
<id column="stu_id" property="stuId" jdbcType="INTEGER" />
<result column="stu_name" property="stuName" jdbcType="VARCHAR" />
<result column="stu_age" property="stuAge" jdbcType="INTEGER" />
<result column="stu_gender" property="stuGender" jdbcType="INTEGER" />
<!-- association可以指定联合的javaBean对象
property="grade":指定哪个属性是联合的对象
javaType:指定这个属性对象的类型【不能省略】-->
<association property="grade" javaType="com.bwlu.bean.Grade">
<id column="g_id" property="gId" jdbcType="INTEGER"/>
<result column="g_name" property="gName" jdbcType="VARCHAR"/>
</association>
</resultMap>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select stu_id, stu_name, stu_age, stu_gender, g.g_id, g_name
from student s,grade g
where s.g_id = g.g_id AND stu_id = #{stuId,jdbcType=INTEGER}
</select>

(3)测试(同 3.1 的(3),结果也一样)

3.3第三种

(1)在 Student.java 中将 g_id 属性换成班级类型(Grade),并添加相应的getter和setter方法,同 3.1 的(1)

(2)使用Association进行分步查询【上述结果相当于使用嵌套结果集的形式】

<resultMap id="BaseResultMap" type="com.bwlu.bean.Student" >
<id column="stu_id" property="stuId" jdbcType="INTEGER" />
<result column="stu_name" property="stuName" jdbcType="VARCHAR" />
<result column="stu_age" property="stuAge" jdbcType="INTEGER" />
<result column="stu_gender" property="stuGender" jdbcType="INTEGER" />
<!-- 使用association进行分步查询
1.先按照学生id查询学生信息
2.根据查询学生信息中g_id值去班级表查出班级信息
3.将班级信息设置到学生中:
association定义关联对象的封装规则
select:表明当前属性是调用指定的方法查出的结果
column:指定将哪一列的值传给这个方法
流程:使用select指定的方法(传入column指定的这列参数的值)查出对象,并封装给property指定的属性。-->
<association property="grade" select="getGradeById" column="g_id"></association>
</resultMap>
<select id="getGradeById"resultType="com.bwlu.bean.Grade" parameterType="java.lang.Integer" >
select g_id, g_name from grade
where g_id = #{gId,jdbcType=INTEGER}
</select>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select stu_id, stu_name, stu_age, stu_gender, g_id
from student
where stu_id = #{stuId,jdbcType=INTEGER}
</select>

注:使用 resultType 返回值类型进行接收,必须使用驼峰式命名,使数据库中的字段和实体类中的字段对应。

(3)测试(同 3.1 的(3),结果也一样)

(2)中也可以指定为 GradeMapper 下的方法。

<association property="grade" select="com.bwlu.mapper.GradeMapper.selectByPrimaryKey" column="g_id"></association>

懒加载机制【按需加载,也叫懒加载】
3.3 分步查询中,每次查询 Student 对象的时候,都将关联 Grade 的对象查询出来了。

使用延迟加载,可以在需要 班级 信息的时候,再去查询,不需要的时候就不用查询。

在 MyBatis 的全局配置文件中,加入两个配置

<settings>
<!-- 驼峰式命名 -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- 开启懒加载机制 ,默认值为true-->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- 开启的话,每个属性都会直接全部加载出来;禁用的话,只会按需加载出来 -->
<setting name="aggressiveLazyLoading" value="false"/>
</settings>

这样,当我们查询 Student 对象的时候,

如果只输出学生姓名,就不会执行查询班级信息的 sql 语句(只执行一条 sql 语句),

select stu_id, stu_name, stu_age, stu_gender, g_id from student where stu_id = ?   Parameters: 6(Integer)

当需要班级信息的时候才会执行,(执行两条 sql 语句)

select stu_id, stu_name, stu_age, stu_gender, g_id from student where stu_id = ?    Parameters: 6(Integer)

select g_id, g_name from grade where g_id = ?    Parameters: 3(Integer)

3.4 上述是在多端(学生)查询一端(班级)的信息,用 association,当我们在一端查询多端信息的时候,需要使用 collection,查出的是一个集合

(1)在班级类(Grade)类中添加一个属性 List<Student> stuList,并添加相应的 gettersetter 方法

private List<Student> stuList;
public List<Student> getStuList() {
return stuList;
}
public void setStuList(List<Student> stuList) {
this.stuList = stuList;
}

(2)使用collection标签定义关联的集合类型元素的封装规则【collection:嵌套结果集的方式】

<resultMap id="BaseResultMap" type="com.bwlu.bean.Grade" >
<id column="g_id" property="gId" jdbcType="INTEGER" />
<result column="g_name" property="gName" jdbcType="VARCHAR" />
<!--
collection:定义关联集合类型的属性的封装规则
ofType:指定集合里面元素的类型
-->
<collection property="stuList" ofType="com.bwlu.bean.Student">
<!-- 定义这个集合中元素的封装规则 -->
<id column="stu_id" property="stuId" jdbcType="INTEGER" />
<result column="stu_name" property="stuName" jdbcType="VARCHAR" />
<result column="stu_age" property="stuAge" jdbcType="INTEGER" />
<result column="stu_gender" property="stuGender" jdbcType="INTEGER" />
</collection>
</resultMap>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select g.g_id, g_name, stu_id, stu_name, stu_age, stu_gender
from grade g,student s
where g.g_id = s.g_id AND g.g_id = #{gId,jdbcType=INTEGER}
</select>

(3)测试

@Autowired
private GradeMapper gradeMapper;
@Test
public void testCollection() {
Grade grade = gradeMapper.selectByPrimaryKey(3);
List<Student> stuList = grade.getStuList();
for(Student stu:stuList){
System.out.println(stu);
//Student [stuId=3, stuName=fenghen, stuAge=12, stuGender=1, grade=null]
//Student [stuId=6, stuName=lixiang, stuAge=22, stuGender=1, grade=null]
}
}

注意:grade为null,因为 Student 和 Grade 相互嵌套,如果用 resultMap 进行接收的话,会相互嵌套,最终导致栈溢出,应用 resultType 进行接收,嵌套的类型为 null 值。

3.5 使用分步查询结果集的方式

(1)在班级类(Grade)类中添加一个属性 List<Student> stuList,并添加相应的 getter 和 setter 方法,同 3.4 中的(1)

(2)在 GradeMapper.xml 中添加如下方法

<resultMap id="BaseResultMap" type="com.bwlu.bean.Grade" >
<id column="g_id" property="gId" jdbcType="INTEGER" />
<result column="g_name" property="gName" jdbcType="VARCHAR" />
<collection property="stuList" select="com.bwlu.mapper.StudentMapper.selectByGId" column="g_id"></collection>
</resultMap>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select g_id, g_name from grade
where g_id = #{gId,jdbcType=INTEGER}
</select>

StudentMapper.xml 中添加如下方法 selectByGId :根据 g_id 查询学生集合

<select id="selectByGId" resultType="com.bwlu.bean.Student" parameterType="java.lang.Integer" >
select
stu_id, stu_name, stu_age, stu_gender,g_id
from student
where g_id = #{gId,jdbcType=INTEGER}
</select>

(3)测试(同 3.4 的(3),结果也一样)

SQL映射文件-----MySQL关系映射【1对1,1对多,多对多】的更多相关文章

  1. XML映射文件中关系映射

    映射(多)对一.(一)对一的关联关系 1).使用列的别名 ①.若不关联数据表,则可以得到关联对象的id属性 ②.若还希望得到关联对象的其它属性.则必须关联其它的数据表 1.创建表: 员工表: DROP ...

  2. 用MyGeneration模板生成NHibernate映射文件和关系

    用我的MyGeneration模板生成NHibernate映射文件和关系(one-to-one,one-to-many,many-to-many) MyGeneration的几个NHibernate模 ...

  3. Java-MyBatis-MyBatis3-XML映射文件:结果映射

    ylbtech-Java-MyBatis-MyBatis3-XML映射文件:结果映射 1.返回顶部 1. 结果映射 resultMap 元素是 MyBatis 中最重要最强大的元素.它可以让你从 90 ...

  4. Java-MyBatis-MyBatis3-XML映射文件:XML映射文件

    ylbtech-Java-MyBatis-MyBatis3-XML映射文件:XML映射文件 1. XML 映射文件 MyBatis 的真正强大在于它的映射语句,这是它的魔力所在.由于它的异常强大,映射 ...

  5. 框架学习系列 mybatis mapper映射文件之输出映射

    1: mapper映射文件输出映射(输入类型) 2:resultType的使用 3:resultMap的使用 3:总结&下节预告 本文是<凯哥陪你学系列-框架学习之mybatis框架学习 ...

  6. ORM映射(对象关系映射)

    ORM映射(对象关系映射)分创建表和操作表两个部分创建单表创建关联表(foreignKey) 一对一 一对多(重点) 多对多(重点) 创建表后加str方法把打印的地址转换成对应字符表的操作(增删改查) ...

  7. Java-MyBatis-MyBatis3-XML映射文件:自动映射

    ylbtech-Java-MyBatis-MyBatis3-XML映射文件:自动映射 1.返回顶部 1. 自动映射 正如你在前面一节看到的,在简单的场景下,MyBatis 可以为你自动映射查询结果.但 ...

  8. hibernate课程 初探单表映射1-9 创建关系映射文件

    创建关系映射文件:(把实体类映射成一个表) 1 右键src==>new==>other==>hibernate==>hbm.xml==>Student==>Fini ...

  9. MyGeneration模板生成NHibernate映射文件和关系(one-to-one,one-to-many,many-to-many)

    MyGeneration的几个NHibernate模板功能已经很强,但还是存在些问题.例如:Guid主键支持不佳,代码不易修改,不支持中文注释等等.所以我决定自己来改写此模版.我把一部分通用的函数提取 ...

随机推荐

  1. Content-Negotiation Header Quality Values

    HTTP: The Definitive Guide 17.3.2 Content-Negotiation Header Quality Values The HTTP protocol define ...

  2. 后台curl网络请求

    <?php //前端进行网络请求  ajax //后台进行网络请求用到两种方式  curl   socket //进行网络请求的步骤 //1.初始化一个curl //2.对curl进行配置 // ...

  3. 前端开发 - jsDom

    一.jsDom简介 jsDom = javascript document object model在JS中,所有的事物都是节点,元素.文本等都是节点.应用场景:可以通过节点进行DOM对象的增删改查 ...

  4. javascript 之 typeof 与 instanceof

    1.typeof:返回一个表达式的数据类型的字符串 返回结果为js的数据类型,包括number,boolean,string,object,undefined,function. var a = 1; ...

  5. Shiro框架简介

    Apache Shiro是Java的一个安全框架.对比另一个安全框架Spring Sercurity,它更简单和灵活. Shiro可以帮助我们完成:认证.授权.加密.会话管理.Web集成.缓存等. A ...

  6. 详解MySQL第一篇—MySQL简要介绍及DDL语句

    背景:近几年,开源数据库逐渐流行起来.由于具有免费使用.配置简单.稳定性好.性能优良等优点,开源数据库在中低端应用上占据了很大的市场份额,而 MySQL 正是开源数据库中的杰出代表.MySQL 数据库 ...

  7. Linux 超级用户和普通用户切换命令

    默认登录的是普通用户权限显示$符 tree@ubuntu:/usr$ 从普通用户切换超级用户权限: sudo su tree@ubuntu:/usr$ sudo su 输入密码 [sudo] pass ...

  8. 模块讲解----sys

    sys:跟python解释器相关的信息 #命令行参数list,第一个元素时程序本身路径 print(sys.argv) 注意:执行脚本时,可以传参数. #退出程序,正常退出时exit(0) sys.e ...

  9. keeplived + mysql双主复制部署 --原创

    环境: master 1: 192.168.100.10  oracle  linux 7.4  mysql 5.7.1 master 2: 192.168.100.11 oracle  linux ...

  10. [golang note] 函数定义

    普通函数定义 √ golang函数基本组成:关键字func.函数名.参数列表.返回值.函数体和返回语句. • 语法如下 func 函数名(参数列表) (返回值列表) { // 函数体 } • 示例如下 ...