Mybatis处理一对多和多对一

多对一处理

create table teacher(
  id int primary key,
  name varchar(30) default null
);
insert into teacher values(1,'双双');
create table student(
	id int primary key,
  name varchar(10) default null,
  tid int,
  foreign key(tid) references teacher(id)
);
insert into student values(1,'小米',1),
						  (2,'小红',1),
						  (3,'小张',1),
						  (4,'小李',1),
						  (5,'小王',1);

环境的搭建:

  • 导入lombok
  • 新建实体类teacher,student
  • 建立mapper接口
  • 建立Mapper.xml核心配置文件
  • 在核心配置文件中绑定注册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.zss.dao.StudentMapper">
    <!--思路:
    1.查询所有的学生
    2.根据查询出来的学生的tid查找对应的老师
    -->
    <select id="getStudent" resultMap="StudentTeacher">
         select * from student;
    </select>
    <resultMap id="StudentTeacher" type="com.zss.pojo.Student">
        <id property="id" column="id"/>
        <id property="name" column="name"/>
        <!--复杂的属性-->
        <!--
        对象用association
        集合用collection
        -->
        <association property="teacher" column="tid" javaType="com.zss.pojo.Teacher" select="getTeacher"/>
    </resultMap>
    <select id="getTeacher" resultType="com.zss.pojo.Teacher">
        select * from teacher where id=#{id};
    </select>
</mapper>

按照结果嵌套处理

<!--思路2:按照结果嵌套查询-->
    <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="com.zss.pojo.Student">
        <id property="id" column="sid"></id>
        <id property="name" column="sname"></id>
        <!--复杂的属性-->
        <association property="teacher"  javaType="com.zss.pojo.Teacher" >
            <result property="name" column="tname"></result>
        </association>
    </resultMap>

多对一使用association标签

一对多处理

<?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.zss.dao.TeacherMapper">
    <select id="getTeacher" resultMap="teacherStudent">
        select * from teacher;
    </select>
 <!--使用嵌套结果查询查询老师下面的所有学生-->
    <select id="getTeacher2" resultMap="teacherStudent">
        select s.id sid,s.name sname,t.id tid,t.name tname
        from student s,teacher t
        where s.tid=t.id and t.id=#{tid};
    </select>
    <resultMap id="teacherStudent" type="com.zss.pojo.Teacher">
        <id property="id" column="tid"/>
        <id property="name" column="tname"/>
        <collection property="students" ofType="com.zss.pojo.Student">
            <result property="id" column="sid"/>
            <result property="name" column="sname"/>
            <result property="id" column="tid"/>
        </collection>
    </resultMap>
    <!--使用子查询查询老师下面的所有学生-->
    <select id="getTeacher3" resultMap="teacherStudent2">
        select * from teacher where id=#{tid};
    </select>
    <resultMap id="teacherStudent2" type="com.zss.pojo.Teacher">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <collection property="students" javaType="ArrayList" ofType="com.zss.pojo.Student" select="getStudentByTeacher" column="id"/>

    </resultMap>
    <select id="getStudentByTeacher" resultType="com.zss.pojo.Student">
        select * from student where tid=#{tid};
    </select>
</mapper>

多对一使用Collection标签

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值