java,mybatis 一对多级联查询,怎么给多的一方添加条件啊???

java,mybatis 一对多级联查询,怎么给多的一方添加条件啊???,第1张

把你的条件添加到select语句后面,然后传下去,例如:

<!-- 旅行社详情 -->

<resultMap type="com.demo.teacher"   id="teacherMap">

    <id property="teacherId" column="teacher_id"/>

    <result property="teacherName" column="teacher_name"/>

    <!--注意下面这个声明,只有column-->

    <result column="age"/>

    <collection property="student" column="{teacherId=teacher_id,age=age}" ofType="java.util.Map"  select="studentMap">

        <id property="studentId" column="student_id" />

        <result property="studentName" column="student_name"/>

        <result property="studentAge" column="student_age"/>

    </collection>

</resultMap>

<!--主-->

<select id="getTeacher" parameterType="java.util.Map" resultMap="teacherMap">

    select 

        teacher_id,

        teacher_name,

        #{age} as age <!--把你的参数这样写-->

    from 

        teachers

    where

        teacher_name = '大西瓜'

</select> <!--从-->

<select id="studentMap" parameterType="java.util.Map" resultType="java.util.Map">

    select 

        student_id,

        student_name,

        student_age

    from 

        students

    where

        teacher_id = #{teacherId}

    and

        age > #{age} <!--这两个参数是resultMap中column指定的key-->

</select>

<!--mybatis的一对多级联查询多方的参数只能是一方的column,所以你要想办法把你的参数做成column-->

一对多的查询方式

1.创建pojo类,在pojo类里面添加 “多” 的list

2. 在“一”的mapper文件中创建resultmap ,resultmap中添加Collection collection和“多”的实体类对应,但是主键不要用id,要用 result 或者将外面的id换成result

如果一对多出现只能查出一条数据来的情况,要先看resultmap 中是否有两个id,记住resultmap中,只能有一个id。还有一种情况就是可能两个表中的column完全一样,这样的可以用表明来避免。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11846815.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-19
下一篇2023-05-19

发表评论

登录后才能评论

评论列表(0条)

    保存