考虑以下 POJO,它们将用于保存将传递给查询的参数。
public class RegionKey {
private BigDecimal rgnId;
private Country country;
//setters and getters.
}
public class Country {
private BigDecimal cntryId;
//setters and getters.
}
public class Region extends RegionKey {
private String rgnNm;
private String desc;
//setters and getters
}
public class Customer {
private BigDecimal custId;
private Region rgn;
}
考虑 MyBatis 的 CustomerMapper 接口
public interface CustomerMapper {
int deleteByPrimaryKey(@Param("custRecord") Customer key);
}
考虑来自 CustomerMapper.xml 文件的片段 (QUery 1)
<delete id="deleteByPrimaryKey">
delete from CUSTOMER
where CUST_ID = #{custRecord.custId,jdbcType=DECIMAL}
and RGN_ID =
cast(#{custRecord.rgn.rgnId,jdbcType=CHAR} as char(10))
</delete>
上面的查询工作得很好。使用以下 if-test 修改上述查询也可以正常工作(查询 2)
<delete id="deleteByPrimaryKey">
delete from CUSTOMER
where CUST_ID = #{custRecord.custId,jdbcType=DECIMAL}
<if test="custRecord.rgn.rgnId != null">
and RGN_ID = cast(#{custRecord.rgn.rgnId,jdbcType=CHAR} as
char(10))
</if>
</delete>
如下修改查询会导致运行时异常(查询 3)
<delete id="deleteByPrimaryKey">
delete from CUSTOMER
where CUST_ID = #{custRecord.custId,jdbcType=DECIMAL}
<if test="custRecord.rgn.country.cntryId != null">
and CNTRY_ID =
cast(#{custRecord.rgn.country.cntryId,jdbcType=CHAR} as
char(10))
</if>
</delete>
对于查询号 3,我在运行时收到 org.apache.ibatis.ognl.NoSuchPropertyException。我无法理解为什么会发生这种情况。如果我可以从查询 2 中的 custRecord.rgn 访问 rgnId 字段,从技术上讲,我应该能够从查询 3 中的 custRecord.rgn.country 访问 cntryId 字段。