MyBatis处理一对多,多对一的sql问题

本文详细介绍了在MyBatis中处理一对多和多对一关系的方法,包括实体类注解、Mapper接口与XML配置。通过查询嵌套和结果嵌套两种方式,展示了如何在实际操作中实现数据的关联查询。同时,文中给出了具体的示例代码,包括Lombok的使用,以及查询结果映射的配置,帮助读者理解MyBatis的关联映射机制。
摘要由CSDN通过智能技术生成

mybatis处理一对多,多对一

多对一的处理

搭建测试环境

1.安装Lombok插件

2.导入maven依赖

<dependencies>
    <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.10</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>RELEASE</version>
        <scope>compile</scope>
    </dependency>
​
</dependencies>

3、在实体类中增加注解,可以不用写get,set,有参,无参

4.编写实体类对应的接口

5.编写接口多用的xml文件

按查询嵌套处理

1、给StudentMapper接口增加方法

public List<Student> getStudent();

2、编写对应的Mapper文件

    <!--先查学生表,多的那个表;然后再查老师表,利用子查询-->
    <select id="getStudent" resultMap="StudentTeacher" >
        select * from student;
    </select>
    
    <!--property中的数据对应实体类中定义的变量,column对应多的数据库中的列名-->
    <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>
    
    <!--子sql中要把id传给父-->
    <select id="getTeacher" resultType="Teacher">
        select * from teacher where id = #{tid}
    </select>

3.在配置文件中注册Mapper

4.注意点说明

 column="{id=tid,name=tid}"
<!--
这里传递过来的id,只有一个属性的时候,下面可以写任何值
association中column多参数配置:
column="{key=value,key=value}"
其实就是键值对的形式,key是传给下个sql的取值名称,value是片段一中sql查询的字段名。
-->
​
<select id="getTeacher" resultType="teacher">
    select * from teacher where id = #{id} and name = #{name}
</select>

5.编写测试

按查询结果嵌套处理
<!--按查询结果嵌套处理-->
​
<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="Student">
    <result property="id" column="sid"/>
    <result property="name" column="sname"/>
​
    <association property="teacher" javaType="Teacher">
        <result property="name" column="tname"/>
    </association>
</resultMap>
​

按照查询进行嵌套处理就像SQL中的子查询

按照结果进行嵌套处理就像SQL中的联表查询

一对多的处理

结果嵌套处理
1. 从学生表和老师表中查出学生id,学生姓名,老师姓名
2. 对查询出来的操作做结果集映射

 <select id="getTeacher" 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>

查询嵌套处理
<select id="getTeacher2" resultMap="TeacherStudent2">
        select * from  teacher where id = #{tid}
    </select>
    
    <resultMap id="TeacherStudent2" type="Teacher">
        <!--column是一对多的外键-->
        <collection property="students" javaType="ArrayList" ofType="Student" select="getStudentByTeacherId" column="id"/>
    </resultMap>


    <select id="getStudentByTeacherId" resultType="Student">
        select * from student where tid = #{tid}
    </select>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值