使用Mybatis多条件非空验证查询错误

在使用Mybatis进行XML文件查询时,遇到因类A引用类B属性导致的查询错误。当尝试根据类B的属性进行非空条件查询时,如果没有对所有参数进行嵌套if判断,会出现OgnlException和IllegalArgumentException异常。解决方法是在where语句中正确添加嵌套if,避免对null值进行比较。

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

在开发中遇以下错误,

在使用xml文件where语句时,我类A引用类B,前台根据类B属性查询A的信息。

 

<select id="selectCommodityList" parameterType="com.zwyl.kks.commodity.entity.Commodity" resultMap="commodityMap">
    SELECT
    c.commodity_id ,c.commodity_name,
    c.commodity_order,
    c.commodity_status_code,c.upc,c.sku,
    c.inventory_type_code,
    c.factory_number,c.sell_price,
    c.commodity_order,
    b.brand_name,b.term_trade_codes,
    f.file_id,f.is_home
    FROM
    commodity c,
    brand b,
    commodity_file f
    <where>
        c.brand_id=b.brand_id AND c.commodity_id=f.commodity_id
        <if test="commodityName!=null and commodityName!=''">
            AND (c.commodity_name LIKE '%${commodityName}%'
            OR c.commodity_ename LIKE '%${commodityName}%')
        </if>
        <if test="factoryNumber!=null and factoryNumber!=''">
            and c.factory_number = #{factoryNumber}
        </if>
        <if test="sku!=null and sku!=''">
            and c.sku = #{sku}
        </if>
        <if test="upc!=null and upc!=''">
            and c.upc = #{upc}
        </if>
        <if test="inventoryTypeCode!=null and inventoryTypeCode!=''">
            and c.inventory_type_code = #{inventoryTypeCode}
        </if>
        <if test="brand!=null and brand!=''">
            <if test="brand.brandName!=null and brand.brandName!=''">
                and (b.brand_name LIKE '%${brand.brandName}%'
                OR b.brand_name_en LIKE '%${brand.brandName}%')
            </if>
            <if test="brand.termTradeCodes!=null and brand.termTradeCodes!=''">
                and b.term_trade_codes = #{brand.termTradeCodes}
            </if>
        </if>
    </where>
    ORDER BY c.commodity_order DESC,f.is_home DESC
</select>
 

注意这里

 

 

        <if test="brand!=null and brand!=''">
            <if test="brand.brandName!=null and brand.brandName!=''">
                and (b.brand_name LIKE '%${brand.brandName}%'
                OR b.brand_name_en LIKE '%${brand.brandName}%')
            </if>
            <if test="brand.termTradeCodes!=null and brand.termTradeCodes!=''">
                and b.term_trade_codes = #{brand.termTradeCodes}
            </if>
        </if>

之前没写嵌套if语句,上面两个参数存在即可正常运行,如不存在报如下异常

 

 

org.apache.ibatis.ognl.OgnlException: source is null for getProperty(null, "brandName")

 

之后加上嵌套if

 

 <if test="brand!=null and brand!=''">

如brand属性为空则正常。存在则报错

 

java.lang.IllegalArgumentException: invalid comparison: com.zwyl.kks.commodity.entity.Brand and java.lang.String

之后找了半天发现if语句中去掉and brand!='' 则正常运行!

下面正确嵌套

 

<if test="brand!=null">
    <if test="brand.brandName!=null and brand.brandName!=''">
        and (b.brand_name LIKE '%${brand.brandName}%'
        OR b.brand_name_en LIKE '%${brand.brandName}%')
    </if>
    <if test="brand.termTradeCodes!=null and brand.termTradeCodes!=''">
        and b.term_trade_codes = #{brand.termTradeCodes}
    </if>
</if>

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值