Mybatis多对一关联查询

有两张表:

学生表:
在这里插入图片描述
教师表:
在这里插入图片描述
其关联关系可表示为:
在这里插入图片描述

在一对多查询时(多个学生对应一个老师),可想到方法:

  • 对结果集进行嵌套查询

将所需的教室和学生类构建好后,将mybatis核心配置完成后(所需的类和核心配置文件在文章末尾)
TeacherMapper.xml文件可进行查询:

<!--    结果嵌套查询-->
    <select id="getTeacher" resultMap="TeacherStudent">
        select s.id sid, s.name sname,t.name tname, t.id tid
        from mybatis.student s,mybatis.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"/>

<!--        此处包含的一个集合-->
        <collection property="students" ofType="Student" >
            <result property="id" column="sid"></result>
            <result property="name" column="sname"></result>
            <result property="tid" column="tid"></result>
        </collection>
    </resultMap>    

对比一对多和多对一可了解到 :

  • 在一对多时候用 association

  • 在多对一时候用 collection

核心配置文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--核心配置文件-->
<configuration>

<!--    配置文件的实现-->
    <properties resource="db.properties" />


    <settings>
<!--        标准的日志 -->
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>

<!--    别名扫描包-->
    <typeAliases>
        <package name="com.tang.pojo"/>
    </typeAliases>
    
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>

    <mappers>
        <package name="com.tang.dao" />
    </mappers>
</configuration>

核心封装类:

public class MybatisUtils {
    private static SqlSessionFactory sqlSessionFactory;

    static {
        try {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
           sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static SqlSession getSqlSession() {
        return  sqlSessionFactory.openSession();
    }
}

Student类:

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

    //一个老师拥有多个学生
    private int tid;
}

Teacher类:

@Data
@ToString
public class Teacher {
    private int id;
    private String name;
    //一个对应多个学生
    private List<Student> students;
}

Tacher接口:

public interface TeacherMapper {
 //获取老师
   // List<Teacher> getTeacher();

    //获取指定老师下的所有学生及老师信息
    Teacher getTeacher(@Param("tid") int id);

}

测试类:

public class TeacherTest {
    public static void main(String[] args) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
        Teacher teacher = mapper.getTeacher(1);
        System.out.println(teacher);
        sqlSession.close();
    }
}

测试结果:

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
发出的红包

打赏作者

风儿吹吹吹

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值