Mybatis多对一处理。

Jammm
2020-05-01 / 0 评论 / 440 阅读 / 正在检测是否收录...

多对一:

  • 多个学生对应一个老师
  • 对于学生而言,关联 多个学生,关联一个老师【多对一】
  • 对于老师而言, 一个老师,有很多学生【一对多】

数据库

CREATE TABLE `teacher` (
    `id` INT(10) NOT NULL,
    `name` VARCHAR(30) DEFAULT NULL,
    PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;

INSERT INTO teacher(`id`, `name`) VALUES (1, '庞老师');

CREATE TABLE `student` (
    `id` INT(10) NOT NULL,
    `name` VARCHAR(30) DEFAULT NULL,
    `tid` INT(10) DEFAULT NULL,
    PRIMARY KEY (`id`),
    KEY `fktid` (`tid`),
    CONSTRAINT `fktid` FOREIGN KEY (`tid`) REFERENCES `teacher` (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;

INSERT INTO `student` (`id`, `name`, `tid`) VALUES ('1', '小明', '1');
INSERT INTO `student` (`id`, `name`, `tid`) VALUES ('2', '小红', '1');
INSERT INTO `student` (`id`, `name`, `tid`) VALUES ('3', '小张', '1');
INSERT INTO `student` (`id`, `name`, `tid`) VALUES ('4', '小李', '1');
INSERT INTO `student` (`id`, `name`, `tid`) VALUES ('5', '小王', '1');

Student类 (省略了构造函数、Setter/Getter)

public class Student {
    private int id;
    private String name;

    //学生需要关联一个老师
    private Teacher teacher;
}

Teacher类 (省略了构造函数、Setter/Getter)

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

测试环境搭建

  1. 新建实体类Teacher,Student
  2. 建立Mapper接口
  3. 建立Mapper.xml文件【在Resources目录下需要一级一级建立文件夹】
  4. 在核心配置中绑定注册我们的Mapper接口或者文件
  5. 测试查询是否能成功

StudentMapper接口(interface)

//查询所有学生信息,以及对于的老师的信息
    public List<Student> getStudent();

    //
    public List<Student> getStudent2();

按照查询嵌套处理

思路:
1.查询所有的学生信息
2.根据查询出来的学生的tid,寻找对应的老师 子查询

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

<resultMap id="StudentTeacher" type="Student">
    <result property="id" column="id"/>
    <result property="name" column="name"/>
    <!--复杂的属性
        对象:association
        集合:collection
    -->
    <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
</resultMap>

<select id="getTeacher" resultType="Teacher">
    select * from teacher where id = #{id}
</select>

按照结果嵌套处理

<!--按照结果处理-->
<select id="getStudent2" resultMap="StudentTeacher2">
    select s.id sid,s.name sname,t.name tname
    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="name" column="tname"/>
    </association>

</resultMap>

测试代码

@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();


    }

    @Test
    public void getStudent2(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();

        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> studentList = mapper.getStudent2();

        for (Student student : studentList) {
            System.out.println(student);
        }

        sqlSession.close();


    }


多对一掌握了?那么可以下一课了,来看看一对多吧!点击访问

本文共 267 个字数,平均阅读时长 ≈ 1分钟
0

打赏

海报

正在生成.....

评论 (0)

取消