在开发中遇以下错误,
在使用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>