0

考虑以下 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 字段。

4

1 回答 1

4

MyBatis 期望(就像大多数框架一样)“属性”遵循Java Bean 规范,以便将属性foo映射到 gettergetFoo()和(可选)setter setFoo()(私有字段的名称可以不同 - 它甚至可以不存在! - 但通常它与属性相同)。

所以,在你的例子中,你应该有

public class RegionKey {
      private Country country;
      ...
      public Country getCountry() {
      ...
      }
}

等等。Java IDE(例如Eclipse)理解这个约定,并允许您为您生成这些getter/setter,因此您不必键入它们。

于 2013-04-23T14:08:21.233 回答