Mybatis中多对一的处理

Mybatis框架中,如果bean对象中成员变量名称和数据库中的字段一一对应则可以完成自动注入,如果Java Bean对象中有复杂的数据类型(自定义对象类型),则需要在xml文件中配置相关的resultMap进行映射。例如一位老师会对应多名学生(先只考虑多对一的关系)数据库中的student表和teacher表写入简单的数据如下:

这两个表之间通过tid进行关联,两个bean对象的定义如下:

public class Student {
    private int id;
    private String name;
    //学生要关联一个老师
    private Teacher teacher;
}
public class Teacher {
    private int id;
    private String name;
}

在mapper文件中的首先这样定义:

 <select id="getStudent" resultMap="StudentTeacher">
        select * from student
 </select>

这里使用resultMap对结果进行映射,定义如下,由于Teacher是一个复杂数据类型,通过association标签进行映射,property:表示要注入到对象哪个属性中去,javaType:结果封装为相应对象,select:下一条要执行的sql语句:

<resultMap id="StudentTeacher" type="com.it.pojo.Student">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <association property="teacher" column="tid" javaType="com.it.pojo.Teacher" select="getTeacher"/>
</resultMap>

select表示下一条执行的sql语句,根据相应的tid查询相应的老师:

<select id="getTeacher" resultType="com.it.pojo.Teacher">
        select * from teacher where id=#{tid}
</select>

总的xml语句可以如下:

<select id="getStudent" resultMap="StudentTeacher">
        select * from student;
    </select>
    <resultMap id="StudentTeacher" type="com.it.pojo.Student">
        <result property="id" column="id"/>
        <association property="teacher" column="tid" javaType="com.it.pojo.Teacher" select="getTeacher"/>
    </resultMap>
    <select id="getTeacher" resultType="com.it.pojo.Teacher">
        select * from teacher where id=#{tid}
    </select>

写一个简单的测试类进行测试:

    @Test
    public void testStudent(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> studentList = mapper.getStudent();
        for (Student student : studentList) {
            System.out.println(student);
        }
        sqlSession.close();
    }

结果如下:

下面考虑通过老师查询对应的学生(一对多的插叙)

给老师中加一个属性

public class Teacher {
    private int id;
    private String name;
    private List<Student> students;
}

 此时的mapper.xml文件应该如下:

<select id="getTeacher" resultMap="TeacherStudent">
        select s.id as sid,s.name as sname,t.name as tname,t.id as tid
        from student s,teacher t
        where s.tid=t.id and t.id=#{tid}
    </select>
    <resultMap id="TeacherStudent" type="com.it.pojo.Teacher">
        <result property="id" column="tid"/>
        <result property="name" column="tname"/>
<!--        javaType指定的属性类型-->
<!--        集合中的泛型信息我们使用ofType获取-->
        <collection property="students" ofType="com.it.pojo.Student">
            <result property="id" column="sid"/>
            <result property="name" column="sname"/>
            <result property="tid" column="tid"/>
        </collection>
    </resultMap>

输出结果如下:

Teacher(id=1, name=秦老师, students=[Student(id=1, name=小明, tid=1), Student(id=2, name=小红, tid=1), Student(id=3, name=小张, tid=1), Student(id=4, name=小李, tid=1), Student(id=5, name=小王, tid=1)])

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值