Mybatis按结果嵌套处理实现多对一处理和一对多处理

1.多对一

实体类

//这里用到了lombok
@Data
public class Student {
    private int id;
    private String name;

    //学生需要关联一个老师!

    private Teacher teacher;
}
@Data
public class Teacher {
    private int id;
    private String name;
}

Mapper配置文件

<?xml version="1.0" encoding="UTF-8" ?>
        <!DOCTYPE mapper
                PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
                "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.StudentMapper">

        <select id="getStudent2" resultMap="StudentTeacher2">
                select s.id sid,s.name sname,t.name tname,t.id tid
                from student s,teacher t
                where s.tid = t.id;
        </select>

        <resultMap id="StudentTeacher2" type="student">
                <result property="id" column="sid"/>
                <result property="name" column="sname"/>
                <association property="teacher" javaType="teacher">
                        <result property="id" column="tid"/>
                        <result property="name" column="tname"/>
                </association>
        </resultMap>
</mapper>

测试

package com.you.dao;

import com.you.utils.MybatisUtils;
import com.you.vo.Student;
import com.you.vo.Teacher;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

public class TeacherMapperTest {

    @Test
    public void getStudent(){
        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();
    }
}

2.一对多

实体类

@Data
public class Student {

    private int id;
    private String name;
    private int tid;

}
@Data
public class Teacher {
    private int id;
    private String name;

    //一个老师拥有多个学生
    private List<Student> students;
}

Mapper配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.you.dao.TeacherMapper">

<!--    按结果嵌套查询-->
    <select id="getTeacherByid" resultMap="TeacherStudent">
        select s.id sid,s.name sname,t.name tname,t.id tid
        from student s,teacher t
        where s.tid = t.id and t.id = #{tid}
    </select>

    <resultMap id="TeacherStudent" type="teacher">
        <result property="id" column="tid"/>
        <result property="name" column="tname"/>

        <!--复杂的属性,我们需要单独处理 对象:association 集合:collection
        JavaType=“” 指定属性的类型!
        集合中的泛型信息,我们使用ofType
        -->
        <collection property="students" ofType="student">
            <result property="id" column="sid"/>
            <result property="name" column="sname"/>
            <result property="tid" column="tid"/>
        </collection>
    </resultMap>

</mapper>

测试

package com.you.dao;

import com.you.utils.MybatisUtils;
import com.you.vo.Teacher;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

public class getMapperTeat {
   
    @Test
    public void getTeacherByid(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);

        Teacher teacherByid = mapper.getTeacherByid(1);
        System.out.println(teacherByid);
        sqlSession.close();
    }
}

相关推荐