← 返回首页
Mybatis基础教程(二十)
发表时间:2022-03-13 14:15:06
一对多(基于注解)

1.一对多 在关系映射的一对多案例的基础上,我们基于注解方式实现一个班级对应一个学生的一对多关系映射。

项目结构图如下:

1.在com.dao包下修改StudentsMapper和新建ClassRoomMapper

//StudentsMapper.java
package com.dao;

import com.entity.IdCard;
import com.entity.Students;
import org.apache.ibatis.annotations.*;
import java.util.List;

public interface StudentsMapper {

    @Select("select * from students where sid=#{sid}")
    @Results(@Result(column = "cid",property = "card",javaType = IdCard.class, one = @One(select = "com.dao.IdCardMapper.getIdCardById")))
    public Students getStudentBySid(Integer sid);


    @Select("select s.*,c.code from students s,idcard c where s.cid = c.id and s.sid=#{sid}")
    @Results({
            @Result(property = "card.id",column = "cid" ),
            @Result(property = "card.code",column = "code")
    })
    public Students getStudentBySid2(Integer sid);

    //根据班级编号查询学生集合
    @Select("select * from students where class_room_id=#{classRoomId}")
    public List<Students> getStudentsByClassRoomId(Integer classRoomId);

    //根据学生编号查询带班级信息的学生资料
    @Select("select s.*,c.code,cr.cname from students s,idcard c,classroom cr where s.cid = c.id and s.class_room_id=cr.cid and s.sid=#{sid}")
    @Results({
            @Result(property = "card.id",column = "cid" ),
            @Result(property = "card.code",column = "code"),
            @Result(property = "classRoom.cid",column = "class_room_id"),
            @Result(property = "classRoom.cname",column = "cname")
    })
    public Students getStudentWithClassRoomBySid(Integer sid);
}

//ClassRoomMapper.java
package com.dao;

import com.entity.ClassRoom;
import org.apache.ibatis.annotations.*;

public interface ClassRoomMapper {

    //根据班级编号查询班级信息
    @Select("select * from classroom where cid = #{cid}")
    public ClassRoom getClassRoomByCid(Integer cid);

    //根据班级编号查询带学生集合信息的班级资料
    @Select("select * from classroom where cid = #{cid}")
    @Results({
          @Result(property = "cid",column = "cid"),
          @Result(property = "stus",column = "cid",javaType = java.util.Set.class,many=@Many(select = "com.dao.StudentsMapper.getStudentsByClassRoomId"))
    })
    public ClassRoom getClassRoomWithStudentsByCid(Integer cid);
}

2.在mybatis-config.xml添加映射接口。

    ...
    <!--映射文件-->
    <mappers>
        <mapper class="com.dao.StudentsMapper"/>
        <mapper class="com.dao.IdCardMapper"/>
        <mapper class="com.dao.ClassRoomMapper"/>
   </mappers>

3.编写测试类

 //StudentsMapperTest.java
    ...
    @Test
    public void testQueryStudentWithClassRoomBySid(){
        SqlSession session = null;
        Students s = null;
        try {
            session = SqlSessionFactoryUtil.openSqlSession();
            StudentsMapper studentsMapper =  session.getMapper(StudentsMapper.class);
            s = studentsMapper.getStudentWithClassRoomBySid(3);
            System.out.println(s);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }


//ClassRoomMapperTest.java

    @Test
    public void testQueryClassRoomWithStudentsByCid(){
        SqlSession session = null;
        ClassRoom c =null;
        try {
            session = SqlSessionFactoryUtil.openSqlSession();
            ClassRoomMapper classRoomMapper = session.getMapper(ClassRoomMapper.class);
            c = classRoomMapper.getClassRoomWithStudentsByCid(1);
            System.out.println(c);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }