mybatis关联映射一对多

实际项目中也存在很多的一对多的情况,下面看看这个简单的例子

table.sql

 CREATE TABLE tb_clazz(
id INT PRIMARY KEY AUTO_INCREMENT,
CODE VARCHAR(18),
NAME VARCHAR(18)
);

INSERT INTO tb_clazz(CODE,NAME) VALUES('w5','五年级');

CREATE TABLE tb_student(
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(18),
sex VARCHAR(18),
age INT,
clazz_id INT,
FOREIGN KEY (clazz_id) REFERENCES tb_clazz(id)
);

INSERT INTO tb_student(NAME,sex,age,clazz_id) VALUES('张三','男',13,1);
INSERT INTO tb_student(NAME,sex,age,clazz_id) VALUES('李四','女',14,1);
INSERT INTO tb_student(NAME,sex,age,clazz_id) VALUES('王五','男',13,1);
INSERT INTO tb_student(NAME,sex,age,clazz_id) VALUES('赵六','女',12,1);

创建一个Clazz对象和一个Student对象分别映射tb_clazz和tb_student表。班级和学生是一对多的关系,即一个班级可以有很多个学生,在Clazz类定义了一个student属性,该属性是一个List集合,用来映射一对多的关联关系,表示一个班级有多个学生
学生和班级是多对一的关系,即一个学生只属于一个班级,在student类当中定义一个clazz属性,该属性是一个Clazz类型,用来映射 多对一的关联关系,表示该学生所属的班级
Clazz.java

public class Clazz implements Serializable {

    private Integer id; // 班级id,主键
    private String code; // 班级编号
    private String name; // 班级名称

    // 班级和学生是一对多的关系,即一个班级可以有多个学生
    private List students;
}

Student.java

public class Student implements Serializable {
    private Integer id; // 学生id,主键
    private String name; // 姓名
    private String sex;  // 性别
    private Integer age; // 年龄

    // 学生和班级是多对一的关系,即一个学生只属于一个班级
    private Clazz clazz;
}

xml文件的配置
ClazzMapper.xml






    
      
      
       
    
        
        
        
        
        
        
        
        
        
      
    
    
    
  

StudentMapper.xml






    
  
  
  
  
  
   
    
        
        
        
        
        
        
            
            
            
        
    

mybatis-config.xml



  

    
    
        
        
        
        
    
    
    
    
    
      
      
      
        
        
        
        
      
    
  
  
  
    
    
  

ClazzMapp.java

public interface ClazzMapper {

    // 根据id查询班级信息
    Clazz selectClazzById(Integer id);
    
}

StudentMapper.java

public interface StudentMapper {

    // 根据id查询学生信息
    Student selectStudentById(Integer id);
    
}

测试代码
OneToManyTest.java

public class OneToManyTest {

    public static void main(String[] args) throws Exception {
        // 读取mybatis-config.xml文件
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        // 初始化mybatis,创建SqlSessionFactory类的实例
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
                .build(inputStream);
        // 创建Session实例
        SqlSession session = sqlSessionFactory.openSession();
        
        OneToManyTest t = new OneToManyTest();
        
        t.testSelectClazzById(session);
//      t.testSelectStudentById(session);
        
        // 提交事务
        session.commit();
        // 关闭Session
        session.close();
    }
    
    // 测试一对多,查询班级Clazz(一)的时候级联查询学生Student(多)  
    public void testSelectClazzById(SqlSession session){
        // 获得ClazzMapper接口的代理对象
        ClazzMapper cm = session.getMapper(ClazzMapper.class);
        // 调用selectClazzById方法
        Clazz clazz = cm.selectClazzById(1);
        System.out.println(clazz);
        // 查看查询到的clazz对象信息
        System.out.println(clazz.getId() + " "+ clazz.getCode() + " "+clazz.getName());
        // 查看clazz对象关联的学生信息
        List students = clazz.getStudents();
        for(Student stu : students){
            System.out.println(stu);
        }
    }
    
    // 测试多对一,查询学生Student(多)的时候级联查询 班级Clazz(一)
    public void testSelectStudentById(SqlSession session){
        // 获得StudentMapper接口的代理对象
        StudentMapper sm = session.getMapper(StudentMapper.class);
        // 调用selectStudentById方法
        Student stu = sm.selectStudentById(1);
        // 查看查询到的Student对象信息
        System.out.println(stu);
        // 查看Student对象关联的班级信息
        System.out.println(stu.getClazz());
    }

}

至此一对多关系关系映射测试完成

你可能感兴趣的:(mybatis关联映射一对多)