Mybatis_一对多和多对一处理


在这里插入图片描述

关联 association
集合 collection
所以association用于一对一和多对一,而collection适用于一对多的关系

1.多对一(按查询嵌套处理)

一对多和多对一大致是一样的,除了配置文件中由association改为了collection,其他都一样实现即可,所以这里只以多对一为例子

1.搭建mybatis环境

搭建mybatis环境

2.mybatis各标签解读

2.mybatis各标签解读

以众多学生分一个老师

3.分别建一个学生和老师表

学生表:
在这里插入图片描述
教师表:
在这里插入图片描述
创建表时候:使用外键将两个表连接起来

4.创建实体类

student:

package cn.bobo.UserDao;
import lombok.Data;
@Data
public class Student {
    private int id;
    private String name;
    private Teacher teacher;
}

teacher:

package cn.bobo.UserDao;
import lombok.Data;
@Data
public class Teacher {
    private int id;
    private String name;
}

分别实现getset方法和tostring方法

5.接口

package cn.bobo.IUserDao;
import cn.bobo.UserDao.Student;
import java.util.List;
public interface StudentMapper {
//    获取所有学生及对应老师的信息
     List<Student> getStudents();
}

6.配置文件

思路:
1.获取所有学生信息
2.根据获取学生信息的老师的id来获取老师的信息
使用 association 来处理关联查询
association关联属性 property属性名 javeType属性类型 column指的是多的一方的表中的列名
4.配置文件

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

<!--首先搜索所有的同学-->
    <select id="getStudents" resultMap="StudentTeacher">
        select * from student
    </select>
    <resultMap id="StudentTeacher" type="cn.bobo.UserDao.Student">
        <association property="teacher" column="tid" javaType="cn.bobo.UserDao.Teacher" select="getTeacher"/>
    </resultMap>
    <select id="getTeacher" resultType="cn.bobo.UserDao.Teacher">
        select * from teacher where id=#{id};
    </select>
</mapper>

7.测试类

可以通过写两个工具方法,通过@Before和@After初始和末尾先执行

package cn.bobo;
import cn.bobo.IUserDao.StudentMapper;
import cn.bobo.IUserDao.TeacherMapper;
import cn.bobo.UserDao.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class test {
    public InputStream in;
    public SqlSession sqlSession;
    public SqlSessionFactory factory;
    public StudentMapper studentMapper;
    @Before
    public void init() throws IOException {
//        1.读取配置文件,生成字节输入流
        in = Resources.getResourceAsStream("mybatis.xml");
//        2. 获取SqlSwssionFactory
        factory = new SqlSessionFactoryBuilder().build(in);
//        3.获取SqlSession对象
        sqlSession = factory.openSession();
//        4. 获取dao的代理对象
//        5.查询执行所有方法
        studentMapper = sqlSession.getMapper(StudentMapper.class);
    }
    @After
//        6. 释放资源
    public void destroy() throws IOException {
        //    提交事务
        sqlSession.commit();
        sqlSession.close();
        in.close();
    }
    @Test
    public void testGetStudents(){
        List<Student> students = studentMapper.getStudents();
        for (Student student:students){
            System.out.println("学生名:"+student.getName()+"\t老师"+student.getTeacher().getName());
        }
    }
}

结果:
在这里插入图片描述

2.多对一(按结果嵌套处理)

前几步骤都是一样的

1.接口

package cn.bobo.IUserDao;
import cn.bobo.UserDao.Student;
import java.util.List;

public interface StudentMapper {
//    获取所有学生及对应老师的信息
     List<Student> getStudents();
     List<Student> getStudents2();
}

2.配置文件

思路:
1.直接查询结果
2.进行结果集的映射

  <select id="getStudents2" 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="cn.bobo.UserDao.Student">
        <id property="id" column="sid"/>
        <result property="name" column="sname"/>
        <association property="teacher" javaType="cn.bobo.UserDao.Teacher">
                <result property="name" column="tname"/>
        </association>
    </resultMap>

3.测试类

package cn.bobo;
import cn.bobo.IUserDao.StudentMapper;
import cn.bobo.IUserDao.TeacherMapper;
import cn.bobo.UserDao.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class test {
    public InputStream in;
    public SqlSession sqlSession;
    public SqlSessionFactory factory;
    public StudentMapper studentMapper;
    @Before
    public void init() throws IOException {
//        1.读取配置文件,生成字节输入流
        in = Resources.getResourceAsStream("mybatis.xml");
//        2. 获取SqlSwssionFactory
        factory = new SqlSessionFactoryBuilder().build(in);
//        3.获取SqlSession对象
        sqlSession = factory.openSession();
//        4. 获取dao的代理对象
//        5.查询执行所有方法
        studentMapper = sqlSession.getMapper(StudentMapper.class);
    }
    @After
//        6. 释放资源
    public void destroy() throws IOException {
        //    提交事务
        sqlSession.commit();
        sqlSession.close();
        in.close();
    }
    @Test
    public void testGetStudents2(){
        List<Student> students = studentMapper.getStudents2();
        for(Student student:students){
            System.out.println("学生名:"+student.getName()+"\t老师"+student.getTeacher().getName());
        }
    }
}

结果:
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

万物皆可der

感谢支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值