Mybatis中collection和association的使用区别

1. 关联-association

用于一对一和多对一

示例:

@Data
public class ZSJC1 {
    private int id;
    private String name;
    @TableField(exist = false)
    private ZSJC2 zsjc2;
}
@Data
public class ZSJC2 {
    private int id;
    private String name;
    private String type;
    @TableField("c1_id")
    private int c1Id;
}



    
    
    
    
    
        
        
        
        
    


2. 集合-collection

用于一对多的关系

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

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

}

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">

column="id" javaType="ArrayList"

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

 

你可能感兴趣的:(mybatis)