mybatis一对多传递多个参数和传递主表未有的字段到子查询

mybatis一对多传递多个参数和传递主表未有的字段到子查询

引用:https://blog.csdn.net/shushan452/article/details/85228194

mybatis n+1查询的时候有时需要跟子表关联几个字段联合查询。
一对多需要用到mybatis标签。这个标签的各种参数的含义、以及怎么传递多个值到子查询、传递主表中没有的字段的参数到子查询的解决办法都在下面做了讲解。

mapper.xml

<?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="com.shushan.dao.UserMultipleTwoDao">
    <resultMap id="BaseResultMapTwo" type="com.shushan.entity.Teacher">
        <id column="id" jdbcType="INTEGER" property="id" />
        <result column="name" jdbcType="VARCHAR" property="name" />
        <result column="age" jdbcType="INTEGER" property="age" />
        <result column="address" jdbcType="VARCHAR" property="address"/>
        <!--一对多,方式一-->
        <collection property="stuList"
                    column="{id=id, phone=phone}"
                    ofType="com.shushan.entity.Teacher"
                    javaType="ArrayList"
                    select="com.shushan.dao.UserMultipleTwoDao.getStudent" />
    </resultMap>

    <resultMap id="StudentMap" type="com.shushan.entity.Student">
        <id column="stu_id" jdbcType="INTEGER" property="id" />
        <result column="ter_id" jdbcType="INTEGER" property="terId" />
        <result column="name" jdbcType="VARCHAR" property="name" />
        <result column="phone" jdbcType="VARCHAR" property="phone" />
        <result column="address" jdbcType="VARCHAR" property="address"/>
        <result column="birthDay" jdbcType="TIMESTAMP" property="birthDay"/>
    </resultMap>

    <!--方式二-->
    <select id="getTeacherTwoList" parameterType="com.shushan.entity.Teacher"
            resultMap="BaseResultMapTwo">
        select id, name,
                age, address,
                case when ('${phone}' != '') then '${phone}' else '' end as phone
          from teacher
        where 1=1
        <if test="id != null">
            and id = #{id}
        </if>
    </select>

    <select id="getStudent" resultMap="StudentMap" parameterType="java.util.Map" >
        select id, ter_id, name, phone, address, birthday
            from student
        where 1=1
            and ter_id = #{id}
            <if test="phone != null">
                and phone = #{phone}
            </if>
    </select>
</mapper>

collection参数含义讲解:

<!--property:对应实体类中的参数名称-->
<!--column:需要往子表传递的字段-->
<!--javaType:该参数类型-->
<!--ofType:该参数泛型-->
<!--select:子查询id名称-->
<collection property="" column="" ofType="" javaType="" select="" />

上面配置文件关键知识点讲解:
1.多值传递使用这种方式column=“{id=id, phone=phone}”,一定要注意子查询入参类型应该是Map集合parameterType=“java.util.Map”。

2.注意colunm传递的字段必须是两个表都有的字段。实际开发中可能我们需要传递一个参数到子查询中,当做子表列的参数,但是该字段只有子查询的子表中存在,那么按正常的传递程序是会提示该列不存在。
解决办法:我们可以在主表中临时动态创建跟子表相同的列,达到主表跟子表对应查询的目的。

case when ('${phone}' != '') then '${phone}' else '' end as phone
  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值