简体   繁体   English

访问MyBatis中的私有超类字段

[英]Accessing a private super class field in MyBatis

Consider the following POJOs that will be used for holding the parameters that will be passed to a query. 考虑以下POJO,这些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;

        }

Consider the CustomerMapper interface for MyBatis 考虑MyBatis的CustomerMapper界面

public interface CustomerMapper {

        int deleteByPrimaryKey(@Param("custRecord") Customer key);
    }

Consider a snippet from the CustomerMapper.xml file (QUery 1) 考虑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>

The above query works perfectly fine. 上面的查询工作正常。 Modifying the above query with the following if-test works fine as well (Query 2) 使用以下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>

Modifying the query as follows causes a runtime exception (Query 3) 如下修改查询会导致运行时异常(查询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>

I get a org.apache.ibatis.ognl.NoSuchPropertyException at runtime for query number 3. I am unable to understand why this happens. 我在运行时获取了查询号3的org.apache.ibatis.ognl.NoSuchPropertyException。我无法理解为什么会这样。 If I can access the rgnId field from custRecord.rgn in query 2, I should technically be able to access the cntryId field from custRecord.rgn.country in query number 3. 如果我可以从查询2中的custRecord.rgn访问rgnId字段,从技术上讲,我应该能够从查询号3中的custRecord.rgn.country访问cntryId字段。

MyBatis expects (as most frameworks do) that "properties" follow the Java Bean specs , so that a property foo is mapped to a getter getFoo() and (optionally) a setter setFoo() (the name of the private field can be different -it can even not exist!- but very often it has same as the property). MyBatis期望(就像大多数框架一样),“属性”遵循Java Bean规范 ,以便将属性foo映射到getter getFoo()和(可选)setter setFoo() (私有字段的名称可以不同) -它甚至可能不存在!-但通常与该属性相同)。

So, in you example you should have 因此,在您的示例中,您应该具有

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

and so on. 等等。 Java IDEs (eg Eclipse) understand this convention, and allow you to generate these getters/setters for you, so you don't have to type them. Java IDE(例如Eclipse)了解此约定,并允许您为您生成这些getter / setter,因此您不必键入它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM