我在项目中将MyBatis用于持久层。

我在mySql db中有两个表,paytransaction和paymenttype,定义如下:

PayTransaction          PaymentType

Field                   Field
-------------           -------
id
payment_state
creation_date
update_date
payment_type ----FK---->id
payment_id              description


因此,paytransaction通过payment_type列(外键)关联了一个付款类型。

我的映射器的定义如下:

PayTransaction

<resultMap id="payTransactionPaymentTypeResultMap" type="com.XXX.payplatform.model.to.impl.TOPayTransaction" >
    <id     column="id"                     property="id"                   javaType="long"/>
    <result column="payment_state"          property="payment_state"        javaType="string"/>
    <result column="creation_date"          property="creation_date"        javaType="date"/>
    <result column="update_date"            property="update_date"          javaType="date"/>
    <result column="payment_id"             property="payment_id"           javaType="long"/>
    <association column="payment_type" property="paymentType" javaType="com.XXX.payplatform.model.to.impl.TOPaymentType" resultMap="com.XXX.payplatform.model.dao.interfaces.IDAOPaymentType.paymentTypeResultMap"/>
</resultMap>


付款方式

<resultMap id="paymentTypeResultMap" type="com.XXX.payplatform.model.to.impl.TOPaymentType" >
    <id     column="id"                     property="id"                   javaType="long"/>
    <result column="description"            property="description"          javaType="string"/>
</resultMap>


SQL JOIN语句是:

<sql id="joinPayTransactionPaymentType">
    select paytransaction.id, paytransaction.payment_state, paytransaction.creation_date,paytransaction.update_date, paytransaction.payment_id,
    paymenttype.id, paymenttype.description
    from paytransaction
    INNER JOIN paymenttype ON paymenttype.id = paytransaction.payment_type
</sql>


该问题与paytransaction和Paymenttype表中名为“ id”的列有关。映射对象时,ID字段具有相同的值,即,如果paytransaction的ID为39,则映射后的Java对象中paytype的ID在db中为3时也将为39。

我试过写别名,例如:

<sql id="joinPayTransactionPaymentType">
    select paytransaction.id AS "paytransaction.id", paytransaction.payment_state as payment_state, paytransaction.creation_date as creation_date,paytransaction.update_date as update_date, paytransaction.payment_id as payment_id,
    paymenttype.id AS "paymenttype.id", paymenttype.description as description
    from paytransaction
    INNER JOIN paymenttype ON paymenttype.id = paytransaction.payment_type
</sql>


但是没有结果。我认为唯一可行的解​​决方案是更改db中列的名称,例如“ paymenttype_id”和“ paytransaction_id”。

有什么解决方案,或者这是mybatis错误?

谢谢!

最佳答案

只要您能够将确定的列(或列别名)映射到这些属性,对于MyBatis来说,拥有相同的属性名称就不是问题。为结果列指定别名时,也应为paymentTypeResultMap指定相同的列名。那应该可以了。

<resultMap id="payTransactionPaymentTypeResultMap" type="com.XXX.payplatform.model.to.impl.TOPayTransaction" >
    <id     column="id"                     property="id"                   javaType="long"/>
    <result column="payment_state"          property="payment_state"        javaType="string"/>
    <result column="creation_date"          property="creation_date"        javaType="date"/>
    <result column="update_date"            property="update_date"          javaType="date"/>
    <result column="payment_id"             property="payment_id"           javaType="long"/>
    <association column="payment_type" property="paymentType" javaType="com.XXX.payplatform.model.to.impl.TOPaymentType" resultMap="com.XXX.payplatform.model.dao.interfaces.IDAOPaymentType.paymentTypeResultMap"/>
</resultMap>


...

<resultMap id="paymentTypeResultMap" type="com.XXX.payplatform.model.to.impl.TOPaymentType" >
    <id     column="paymenttype_id"                     property="id"                   javaType="long"/>
    <result column="description"            property="description"          javaType="string"/>
</resultMap>


...

<sql id="joinPayTransactionPaymentType">
    select paytransaction.id, paytransaction.payment_state as payment_state, paytransaction.creation_date as creation_date,paytransaction.update_date as update_date, paytransaction.payment_id as payment_id,
    paymenttype.id AS paymenttype_id, paymenttype.description as description
    from paytransaction
    INNER JOIN paymenttype ON paymenttype.id = paytransaction.payment_type
</sql>

07-27 18:47