我在项目中使用MyBatis作为持久层。
我在mySql db,paytransaction和paymenttype中有两个表,定义如下:
PayTransaction PaymentType
Field Field
------------- -------
id
payment_state
creation_date
update_date
payment_type ----FK---->id
payment_id description
因此,付款交易通过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>
PaymentType
<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”的列。映射对象时,相同的值将转到ids字段,即如果paytransaction的id为39,则映射后java对象中的paymenttype的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错误?
谢谢!
答案 0 :(得分:0)
具有相同的属性名称对于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>