欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-01 11:17:10
最近一直把collection和association弄混,所以为了增强自己的记忆,就撸一个关系出来算是总结罢了 1. 关联-association 2. 集合-col...

最近一直把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的类型别名图

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

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