Mybatis中collection和association的使用区别详解

所属分类: 软件编程 / java 阅读数: 42
收藏 0 赞 0 分享

最近一直把collection和association弄混,所以为了增强自己的记忆,就撸一个关系出来算是总结罢了

1. 关联-association
2. 集合-collection

比如同时有User.java和Card.java两个类

User.java如下:

public class User{

private Card card_one;

private List<Card> card_many;

}

在映射card_one属性时用association标签, 映射card_many时用collection标签.

所以association是用于一对一和多对一,而collection是用于一对多的关系

下面就用一些例子解释下吧

association-一对一

人和身份证的关系

下面是pojo

public class Card implements Serializable{
 private Integer id;
 private String code;
//省略set和get方法.
}
public class Person implements Serializable{
 private Integer id;
 private String name;
 private String sex;
 private Integer age;
 //人和身份证是一对一的关系
 private Card card;
//省略set/get方法.
}

下面是mapper和实现的接口

package com.glj.mapper;

import com.glj.poji.Card;

public interface CardMapper {
 Card selectCardById(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.CardMapper">
 <select id="selectCardById" parameterType="int" resultType="com.glj.poji.Card">
 select * from tb_card where id = #{id} 
 </select>
</mapper>
package com.glj.mapper;
 
import com.glj.poji.Person;
 
public interface PersonMapper {
 Person selectPersonById(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.PersonMapper">
 <resultMap type="com.glj.poji.Person" id="personMapper">
 <id property="id" column="id"/>
 <result property="name" column="name"/>
 <result property="sex" column="sex"/>
 <result property="age" column="age"/>
 <association property="card" column="card_id" 
  select="com.glj.mapper.CardMapper.selectCardById"
  javaType="com.glj.poji.Card">
 </association>
 </resultMap>
 <select id="selectPersonById" parameterType="int" resultMap="personMapper">
 select * from tb_person where id = #{id}
 </select>
</mapper>

PersonMapper.xml 还使用association的分步查询。

同理多对一,也是一样

只要那个pojo出现private Card card_one;

即使用association

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

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

pojo类

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方法
}
<?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>
package com.glj.mapper;

import com.glj.pojo.Clazz;

public interface ClazzMapper {
 Clazz selectClazzById(Integer id);
}

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

<?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>
package com.glj.mapper;

import com.glj.pojo.Student;

public interface StudentMapper {
 Student selectStudentById(Integer id); 
}

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

嗯,希望我以后又不记得二者的关系时,能感谢现在总结的自己

附上一张mybatis的类型别名图

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

java实现背单词程序

这篇文章主要为大家详细介绍了java实现背单词程序,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

java实现单词查询小程序

这篇文章主要为大家详细介绍了java实现单词查询小程序,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Java程序开发环境配置图文教程

这篇文章主要为大家详细介绍了Java程序开发环境配置图文教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

详解ssh框架原理及流程

在本文中小编给大家整理的是关于ssh框架原理及流程的相关知识点内容,有此需要的朋友们可以学习下。
收藏 0 赞 0 分享

Java实现弹窗效果的基本操作

这篇文章主要为大家详细介绍了Java实现弹窗效果的基本操作,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

详解springmvc常用5种注解

在本篇里我们给大家总结了关于springmvc常用5种注解相关知识点以及实例代码,需要的朋友们参考下。
收藏 0 赞 0 分享

Java实现弹窗效果的基本操作(2)

这篇文章主要为大家详细介绍了Java实现弹窗效果的基本操作第二篇,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Spring Boot假死诊断实战记录

这篇文章主要给大家介绍了关于Spring Boot假死诊断的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Spring Boot具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

Java计时新姿势StopWatch详解

这篇文章主要介绍了Java计时新姿势StopWatch,最近公司来了个大佬,从他那里学到不少东西,其中一个就是计时的新姿势「StopWatch」,需要的朋友可以参考下
收藏 0 赞 0 分享

java实现点击按钮弹出新窗体功能

这篇文章主要为大家详细介绍了java实现点击按钮弹出新窗体功能,旧窗体不进行操作,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多