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

网友投稿 275 2023-01-18


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

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

1. 关联-association

2. 集合-collection

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

User.java如下:

public class User{

private Card card_one;

private List 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);

}

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

select * from tb_card where id = #{id}

package com.glj.mapper;

import com.glj.poji.Person;

public interface PersonMapper {

Person selectPersonById(Integer id);

}

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

select="com.glj.mapper.CardMapper.selectCardById"

javaType="com.glj.poji.Card">

select="com.glj.mapper.CardMapper.selectCardById"

javaType="com.glj.poji.Card">

select * from tb_person where id = #{id}

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 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方法

}

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd"&glxwHamsEIt;

select * from tb_clazz where id = #{id}

column="id" javaType="ArrayList"

fetchType="lazy" select="com.glj.mapper.StudentMapper.selectStudentByClazzId">

column="id" javaType="ArrayList"

fetchType="lazy" select="com.glj.mapper.StudentMapper.selectStudentByClazzId">

package com.glj.mapper;

import com.glj.pojo.Clazz;

public interface ClazzMapper {

Clazz selectClazzById(Integer id);

}

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

PUBLIC "-//myhttp://batis.orlxwHamsEIg//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

select * from tb_clazz c,tb_student s where c.id = s.id and s.id = #{id}

select * from tb_student where clazz_id = #{id}

</mapper>

package com.glj.mapper;

import com.glj.pojo.Student;

public interface StudentMapper {

Student selectStudentById(Integer id);

}

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

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

附上一张mybatis的类型别名图


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:SpringBoot 使用hibernate validator校验
下一篇:浅谈JAVA工作流的优雅实现方式
相关文章

 发表评论

暂时没有评论,来抢沙发吧~