最近在使用mybatis进行项目开发,关于mapper.xml进行如下总结,希望对跟我一样的初学者有一些帮助:
一、resultMap的编写,该类型最大的用处就是用于定义查询返回的类型,代码如下所示:
resultMap标签中的type属性为整个实体对应文件,若该实体包含对象类型属性则使用association标签;若包含list属性则使用collection标签。collection中属性过多,可以使用例子中的查询方式。
二、编写查询方法,动态拼接查询条件,入参类型为map,查询结果以上面定义的resultMap形式展现:
and BAZ463=#{id}
and BAZ002=#{baz002}
and AAB299=#{tgtAgencyState}
and AAE270=#{contactNoteNumber}
and AAE405=#{transBillNumber}
and BAC999=#{formerPersonNumber}
and AAE035=#{transferDate}
and AIC113=#{pensionInsTransDirection}
and AAE173=#{transferType}
and AAB004=#{srcUnitName}
and AAB305=#{firstJoinlandPersonalPaytime}
and AAC049=#{firstPersonalPaytime}
and AAC032=#{personActCreateTime}
and AAE030=#{paymentBeginTime}
and AAE031=#{paymentEndTime}
and AAE201=#{actualPaymentMonths}
and AIC093=#{oldPersonalActSum}
and AIC089=#{theNewPersonalActSum}
and AIC082=#{transYearInPersonalAct}
and AIC083=#{personalActTransSum}
and AIC084=#{regionFundTransferSum}
and AIC102=#{transferFundSum}
and BAE787=#{repeatFees}
and BAE829=#{repeatFeeFlag}
and BAE828=#{giveupAddFee}
and AAD019=#{fundTransferBankNumber}
and AAB300=#{transferInAgencyName}
and AAA146=#{transferOutAgencyName}
and AAZ198=#{nationAgencyId}
and BAE062=#{transEventId}
and BAE037=#{onlineDownload}
and BAE038=#{isConfirm}
and BAE022=#{uploadStatus}
and BZE111=#{uploadOperator}
and BZE136=#{uploadOperateTime}
and AAE013=#{remark}
and BAE681=#{transBeforeEventId}
and AAC067=#{contactPhone}
and BAE039=#{financeStatus}
and AAE011=#{aae011}
and AAE036=#{aae036}
and AAB034=#{aab034}
and AAA027=#{aaa027}
and BZE011=#{bze011}
and BZE036=#{bze036}
and BZE034=#{bze034}
and AAZ341=#{sysTraceId}
and AAB301=#{srcAgencyState}
and AAC001=#{personId}
and AAC999=#{personNumber}
and AAC002=#{socialEnsureNumber}
and AAC003=#{name}
and AAC004=#{sex}
and AAC006=#{birthday}
and AAC028=#{migrantWorkers}
and AAC010=#{householdAddress}
and AAE473=#{transferAccountType}
and AAC007=#{workDate}
三、编写更新方法,动态进行value补充,入参类型为实体型:
UPDATE test
BAZ002=#{baz002},
AAB299=#{tgtAgencyState},
AAE270=#{contactNoteNumber},
AAE405=#{transBillNumber},
BAC999=#{formerPersonNumber},
AAE035=#{transferDate},
AIC113=#{pensionInsTransDirection},
AAE173=#{transferType},
AAB004=#{srcUnitName},
AAB305=#{firstJoinlandPersonalPaytime},
AAC049=#{firstPersonalPaytime},
AAC032=#{personActCreateTime},
AAE030=#{paymentBeginTime},
AAE031=#{paymentEndTime},
AAE201=#{actualPaymentMonths},
AIC093=#{oldPersonalActSum},
AIC089=#{theNewPersonalActSum},
AIC082=#{transYearInPersonalAct},
AIC083=#{personalActTransSum},
AIC084=#{regionFundTransferSum},
AIC102=#{transferFundSum},
BAE787=#{repeatFees},
BAE829=#{repeatFeeFlag},
BAE828=#{giveupAddFee},
AAD019=#{fundTransferBankNumber},
AAB300=#{transferInAgencyName},
AAA146=#{transferOutAgencyName},
AAZ198=#{nationAgencyId},
BAE062=#{transEventId},
BAE037=#{onlineDownload},
BAE038=#{isConfirm},
BAE022=#{uploadStatus},
BZE111=#{uploadOperator},
BZE136=#{uploadOperateTime},
AAE013=#{remark},
BAE681=#{transBeforeEventId},
AAC067=#{contactPhone},
BAE039=#{financeStatus},
AAE011=#{aae011},
AAE036=#{aae036},
AAB034=#{aab034},
AAA027=#{aaa027},
BZE011=#{bze011},
BZE036=#{bze036},
BZE034=#{bze034},
AAZ341=#{sysTraceId},
AAB301=#{srcAgencyState},
AAC001=#{personId},
AAC999=#{personNumber},
AAC002=#{socialEnsureNumber},
AAC003=#{name},
AAC004=#{sex},
AAC006=#{birthday},
AAC028=#{migrantWorkers},
AAC010=#{householdAddress},
AAE473=#{transferAccountType},
AAC007=#{workDate},
WHERE BAZ463=#{id}
四、编写保存方法,入参类型为实体型:
insert into test
(BAZ463,
BAZ002,
AAB299,
AAE270,
AAE405,
BAC999,
AAE035,
AIC113,
AAE173,
AAB004,
AAB305,
AAC049,
AAC032,
AAE030,
AAE031,
AAE201,
AIC093,
AIC089,
AIC082,
AIC083,
AIC084,
AIC102,
BAE787,
BAE829,
BAE828,
AAD019,
AAB300,
AAA146,
AAZ198,
BAE062,
BAE037,
BAE038,
BAE022,
BZE111,
BZE136,
AAE013,
BAE681,
AAC067,
BAE039,
AAE011,
AAE036,
AAB034,
AAA027,
BZE011,
BZE036,
BZE034,
AAZ341,
AAB301,
AAC001,
AAC999,
AAC002,
AAC003,
AAC004,
AAC006,
AAC028,
AAC010,
AAE473,
AAC007)
values
(#{id,jdbcType=BIGINT},
#{baz002,jdbcType=BIGINT},
#{tgtAgencyState,jdbcType=VARCHAR},
#{contactNoteNumber,jdbcType=VARCHAR},
#{transBillNumber,jdbcType=VARCHAR},
#{formerPersonNumber,jdbcType=VARCHAR},
#{transferDate,jdbcType=BIGINT},
#{pensionInsTransDirection,jdbcType=VARCHAR},
#{transferType,jdbcType=VARCHAR},
#{srcUnitName,jdbcType=VARCHAR},
#{firstJoinlandPersonalPaytime,jdbcType=BIGINT},
#{firstPersonalPaytime,jdbcType=BIGINT},
#{personActCreateTime,jdbcType=BIGINT},
#{paymentBeginTime,jdbcType=BIGINT},
#{paymentEndTime,jdbcType=BIGINT},
#{actualPaymentMonths,jdbcType=BIGINT},
#{oldPersonalActSum,jdbcType=DOUBLE},
#{theNewPersonalActSum,jdbcType=DOUBLE},
#{transYearInPersonalAct,jdbcType=DOUBLE},
#{personalActTransSum,jdbcType=DOUBLE},
#{regionFundTransferSum,jdbcType=DOUBLE},
#{transferFundSum,jdbcType=DOUBLE},
#{repeatFees,jdbcType=DOUBLE},
#{repeatFeeFlag,jdbcType=VARCHAR},
#{giveupAddFee,jdbcType=VARCHAR},
#{fundTransferBankNumber,jdbcType=VARCHAR},
#{transferInAgencyName,jdbcType=VARCHAR},
#{transferOutAgencyName,jdbcType=VARCHAR},
#{nationAgencyId,jdbcType=BIGINT},
#{transEventId,jdbcType=BIGINT},
#{onlineDownload,jdbcType=VARCHAR},
#{isConfirm,jdbcType=VARCHAR},
#{uploadStatus,jdbcType=VARCHAR},
#{uploadOperator,jdbcType=VARCHAR},
#{uploadOperateTime,jdbcType=BIGINT},
#{remark,jdbcType=VARCHAR},
#{transBeforeEventId,jdbcType=BIGINT},
#{contactPhone,jdbcType=VARCHAR},
#{financeStatus,jdbcType=VARCHAR},
#{aae011,jdbcType=VARCHAR},
#{aae036,jdbcType=BIGINT},
#{aab034,jdbcType=VARCHAR},
#{aaa027,jdbcType=VARCHAR},
#{bze011,jdbcType=VARCHAR},
#{bze036,jdbcType=BIGINT},
#{bze034,jdbcType=VARCHAR},
#{pensionTransBillPK.sysTraceId,jdbcType=VARCHAR},
#{pensionTransBillPK.srcAgencyState,jdbcType=VARCHAR},
#{transferInfo.personId,jdbcType=BIGINT},
#{transferInfo.personNumber,jdbcType=VARCHAR},
#{transferInfo.socialEnsureNumber,jdbcType=VARCHAR},
#{transferInfo.name,jdbcType=VARCHAR},
#{transferInfo.sex,jdbcType=VARCHAR},
#{transferInfo.birthday,jdbcType=BIGINT},
#{transferInfo.migrantWorkers,jdbcType=VARCHAR},
#{transferInfo.householdAddress,jdbcType=VARCHAR},
#{transferInfo.transferAccountType,jdbcType=VARCHAR},
#{transferInfo.workDate,jdbcType=BIGINT}
)