mybatis中动态sql常遇到的问题,以及连表查询时常遇到的问题

本文详细解析了MyBatis中动态SQL的正确使用方式,特别是where和if标签的组合,以及在多表联查时如何通过resultMap避免字段冲突,确保查询结果准确无误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

mybatis中动态sql常遇到的问题,以及连表查询时常遇到的问题

1、mybatis中注意下一使用where 和if标签以及set 和if 标签时,如果有多个if,那么请按下面格式书写(即将连接符号 and写在前面),防止出错
<where>
    <if test="provider != null and provider.providerCode">provider_code like "%"{provider.providerCode}"%" </if>
    <if test="provider != null and provider.providerName">and  provider_name like "%"#{provider.providerName}"%"</if>
    <if test="provider != null and provider.contacts">and  contacts like "%"#{provider.contacts}"%"</if>
    <if test="provider != null and provider.mobile">and  mobile like "%"#{provider.mobile}"%"</if>
    <if test="provider != null and provider.fax">and  fax like "%"#{provider.fax}"%" </if>
    <if test="provider != null and provider.createDate">and  create_date=#{provider.createDate}</if>
</where>
2、在mybatis中多表联查时要注意尤其类字段名和数据库中不一样时,查询的结果需要使用resultMap进行影射时,直接写column里的字段直接写数据库中的字段就可以了,不需要进行其他的操作,但有一点要注意如果涉及到多张表中有相同的字段时,记得给这些字段起一个别名,否则可能有一个字段的值为null,或者映射错误!
<resultMap id="billMap" type="bill">
    <id property="bid" column="bid"></id>
    <result property="productName" column="product_name"></result>
    <result property="price" column="price"></result>
    <result property="payment" column="payment"></result>
    <result property="createTime" column="create_time"></result>
    <result property="proId" column="pro_id"></result>
    <result property="total" column="total"></result>
    <result property="unit" column="unit"></result>
    <association property="provider" javaType="provider">
        <id property="id" column="id"></id>
        <result property="providerCode" column="provider_code"></result>
        <result property="providerName" column="provider_name"></result>
        <result property="contacts" column="contacts"></result>
        <result property="mobile" column="mobile"></result>
        <result property="fax" column="fax"></result>
        <result property="createDate" column="create_date"></result>
    </association>
</resultMap>
<sql id="select_bill">
    b.bid,b.product_name,b.price,b.payment,b.create_time,b.pro_id,b.total,b.unit,
    p.id,p.provider_code,p.provider_name,p.contacts,p.mobile,p.fax,p.create_date
</sql>
<select id="selectAllBill" resultMap="billMap" resultSets="java.util.List">
    select <include refid="select_bill"></include> from bill b
    inner join provider p on b.pro_id=p.id
    <where>
        <if test="bill != null and bill.productName !=null">b.product_name like "%"#{bill.productName}"%"</if>
        <if test="bill != null and bill.payment !=null">and b.payment=#{bill.payment }</if>
        <if test="bill != null and bill.proId !=null">and b.pro_id=#{bill.proId}</if>
    </where>
    <if test="pageSize != null">limit #{pageNum},#{pageSize}</if>
</select>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值