mybatis的mapper.xml编写

一、resultMap的编写,该类型最大的用处就是用于定义查询返回的类型,代码如下所示:

resultMap标签中的type属性为整个实体对应文件,若该实体包含对象类型属性则使用association标签;若包含list属性则使用collection标签。collection中属性过多,可以使用例子中的查询方式。

<resultMap
		type="org.test.settle.national.pension.PensionTransBill"
		id="PensionTransBillResultMap">
		<id column="BAZ463" property="id" />
		<result column="BAZ002" property="baz002" />
		<result column="AAB299" property="tgtAgencyState" />
		<result column="AAE270" property="contactNoteNumber" />
		<result column="AAE405" property="transBillNumber" />
		<result column="BAC999" property="formerPersonNumber" />
		<result column="AAE035" property="transferDate" />
		<result column="AIC113" property="pensionInsTransDirection" />
		<result column="AAE173" property="transferType" />
		<result column="AAB004" property="srcUnitName" />
		<result column="AAB305" property="firstJoinlandPersonalPaytime" />
		<result column="AAC049" property="firstPersonalPaytime" />
		<result column="AAC032" property="personActCreateTime" />
		<result column="AAE030" property="paymentBeginTime" />
		<result column="AAE031" property="paymentEndTime" />
		<result column="AAE201" property="actualPaymentMonths" />
		<result column="AIC093" property="oldPersonalActSum" />
		<result column="AIC089" property="theNewPersonalActSum" />
		<result column="AIC082" property="transYearInPersonalAct" />
		<result column="AIC083" property="personalActTransSum" />
		<result column="AIC084" property="regionFundTransferSum" />
		<result column="AIC102" property="transferFundSum" />
		<result column="BAE787" property="repeatFees" />
		<result column="BAE829" property="repeatFeeFlag" />
		<result column="BAE828" property="giveupAddFee" />
		<result column="AAD019" property="fundTransferBankNumber" />
		<result column="AAB300" property="transferInAgencyName" />
		<result column="AAA146" property="transferOutAgencyName" />
		<result column="AAZ198" property="nationAgencyId" />
		<result column="BAE062" property="transEventId" />
		<result column="BAE037" property="onlineDownload" />
		<result column="BAE038" property="isConfirm" />
		<result column="BAE022" property="uploadStatus" />
		<result column="BZE111" property="uploadOperator" />
		<result column="BZE136" property="uploadOperateTime" />
		<result column="AAE013" property="remark" />
		<result column="BAE681" property="transBeforeEventId" />
		<result column="AAC067" property="contactPhone" />
		<result column="BAE039" property="financeStatus" />
		<!-- 六经字段 -->
		<result column="AAE011" property="aae011" />
		<result column="AAE036" property="aae036" />
		<result column="AAB034" property="aab034" />
		<result column="AAA027" property="aaa027" />
		<result column="BZE011" property="bze011" />
		<result column="BZE036" property="bze036" />
		<result column="BZE034" property="bze034" />
		<association property="pensionTransBillPK"
			javaType="org.test.settle.national.pension.PensionTransBillPK">
			<result column="AAZ341" property="sysTraceId" />
			<result column="AAB301" property="srcAgencyState" />
		</association>
		<association property="transferInfo"
			javaType="org.test.settle.common.settleinfo.TransferInfo">
			<result column="AAC001" property="personId" />
			<result column="AAC999" property="personNumber" />
			<result column="AAC002" property="socialEnsureNumber" />
			<result column="AAC003" property="name" />
			<result column="AAC004" property="sex" />
			<result column="AAC006" property="birthday" />
			<result column="AAC028" property="migrantWorkers" />
			<result column="AAC010" property="householdAddress" />
			<result column="AAE473" property="transferAccountType" />
			<result column="AAC007" property="workDate" />
		</association>
		<!-- 用于一对多关系配置,在保存时没有对应字段保存 -->
		<collection property="pensionTransBillItems"
			column="BAZ463"
			ofType="org.test.settle.national.pension.PensionTransBillItem"
			select="org.test.settle.national.pension.PensionTransBillItemDAO.findTransBillItemByTransBillId">
		</collection>
 
	</resultMap>

二、编写查询方法,动态拼接查询条件,入参类型为map,查询结果以上面定义的resultMap形式展现:

	<select id="listByAppIdAndDictCode" resultType="com.sugon.cloud.lowcode.entity.po.DictItem">
		<include refid="selectSql"/>
		WHERE
			dict.app_id = #{appId}
			AND dict.`dict_code` = #{dictCode}
    </select>

	<sql id="selectSql">
		SELECT
			dict_item.*
		FROM
			dict_item
			LEFT JOIN dict ON dict_item.parent_id = dict.id
	</sql>
</mapper>
<select id="selectEntityByMap"
		resultMap="PensionTransBillResultMap" parameterType="map">
		select * from test
		<where>
			<include refid="where_criteria"></include>
		</where>
		order by aae036 desc
	</select>
	<!-- 查询条件 -->
	<sql id="where_criteria">
		<if test="id!=null">and BAZ463=#{id}</if>
		<if test="baz002!=null">and BAZ002=#{baz002}</if>
		<if test="tgtAgencyState!=null">and AAB299=#{tgtAgencyState}</if>
		<if test="contactNoteNumber!=null">and AAE270=#{contactNoteNumber}</if>
		<if test="transBillNumber!=null">and AAE405=#{transBillNumber}</if>
		<if test="formerPersonNumber!=null">and BAC999=#{formerPersonNumber}</if>
		<if test="transferDate!=null">and AAE035=#{transferDate}</if>
	</sql>

三、编写更新方法,动态进行value补充,入参类型为实体型:

<update id="updateEntity"
		parameterType="org.test.settle.national.pension.PensionTransBill">
		UPDATE test
		<trim prefix="set" suffixOverrides=",">
			<if test="baz002!=null"> BAZ002=#{baz002},</if>
			<if test="tgtAgencyState!=null"> AAB299=#{tgtAgencyState},</if>
			<if test="contactNoteNumber!=null"> AAE270=#{contactNoteNumber},</if>
			<if test="transBillNumber!=null"> AAE405=#{transBillNumber},</if>
			<if test="formerPersonNumber!=null"> BAC999=#{formerPersonNumber},</if>
			<if test="transferDate!=null"> AAE035=#{transferDate},</if>
			<if test="pensionInsTransDirection!=null"> AIC113=#{pensionInsTransDirection},</if>
			<if test="transferType!=null"> AAE173=#{transferType},</if>
			<if test="srcUnitName!=null"> AAB004=#{srcUnitName},</if>
			<if test="firstJoinlandPersonalPaytime!=null"> AAB305=#{firstJoinlandPersonalPaytime},</if>
			<if test="firstPersonalPaytime!=null"> AAC049=#{firstPersonalPaytime},</if>
			<if test="personActCreateTime!=null"> AAC032=#{personActCreateTime},</if>
			<if test="paymentBeginTime!=null"> AAE030=#{paymentBeginTime},</if>
			<if test="paymentEndTime!=null"> AAE031=#{paymentEndTime},</if>
			<if test="actualPaymentMonths!=null"> AAE201=#{actualPaymentMonths},</if>
			<if test="oldPersonalActSum!=null"> AIC093=#{oldPersonalActSum},</if>
			<if test="theNewPersonalActSum!=null"> AIC089=#{theNewPersonalActSum},</if>
			<if test="transYearInPersonalAct!=null"> AIC082=#{transYearInPersonalAct},</if>
			<if test="personalActTransSum!=null"> AIC083=#{personalActTransSum},</if>
			<if test="regionFundTransferSum!=null"> AIC084=#{regionFundTransferSum},</if>
			<if test="transferFundSum!=null"> AIC102=#{transferFundSum},</if>
			<if test="repeatFees!=null"> BAE787=#{repeatFees},</if>
			<if test="repeatFeeFlag!=null"> BAE829=#{repeatFeeFlag},</if>
			<if test="giveupAddFee!=null"> BAE828=#{giveupAddFee},</if>
			<if test="fundTransferBankNumber!=null"> AAD019=#{fundTransferBankNumber},</if>
			<if test="transferInAgencyName!=null"> AAB300=#{transferInAgencyName},</if>
			<if test="transferOutAgencyName!=null"> AAA146=#{transferOutAgencyName},</if>
			<if test="nationAgencyId!=null"> AAZ198=#{nationAgencyId},</if>
			<if test="transEventId!=null"> BAE062=#{transEventId},</if>
			<if test="onlineDownload!=null"> BAE037=#{onlineDownload},</if>
			<if test="isConfirm!=null"> BAE038=#{isConfirm},</if>
			<if test="uploadStatus!=null"> BAE022=#{uploadStatus},</if>
			<if test="uploadOperator!=null"> BZE111=#{uploadOperator},</if>
			<if test="uploadOperateTime!=null"> BZE136=#{uploadOperateTime},</if>
			<if test="remark!=null"> AAE013=#{remark},</if>
			<if test="transBeforeEventId!=null"> BAE681=#{transBeforeEventId},</if>
			<if test="contactPhone!=null"> AAC067=#{contactPhone},</if>
			<if test="financeStatus!=null"> BAE039=#{financeStatus},</if>
			<if test="aae011!=null"> AAE011=#{aae011},</if>
			<if test="aae036!=null"> AAE036=#{aae036},</if>
			<if test="aab034!=null"> AAB034=#{aab034},</if>
			<if test="aaa027!=null"> AAA027=#{aaa027},</if>
			<if test="bze011!=null"> BZE011=#{bze011},</if>
			<if test="bze036!=null"> BZE036=#{bze036},</if>
			<if test="bze034!=null"> BZE034=#{bze034},</if>
			<if test="pensionTransBillPK!=null">
				<if test="sysTraceId!=null"> AAZ341=#{sysTraceId},</if>
				<if test="srcAgencyState!=null"> AAB301=#{srcAgencyState},</if>
			</if>
			<if test="transferInfo!=null">
				<if test="personId!=null"> AAC001=#{personId},</if>
				<if test="personNumber!=null"> AAC999=#{personNumber},</if>
				<if test="socialEnsureNumber!=null"> AAC002=#{socialEnsureNumber},</if>
				<if test="name!=null"> AAC003=#{name},</if>
				<if test="sex!=null"> AAC004=#{sex},</if>
				<if test="birthday!=null"> AAC006=#{birthday},</if>
				<if test="migrantWorkers!=null"> AAC028=#{migrantWorkers},</if>
				<if test="householdAddress!=null"> AAC010=#{householdAddress},</if>
				<if test="transferAccountType!=null"> AAE473=#{transferAccountType},</if>
				<if test="workDate!=null"> AAC007=#{workDate},</if>
			</if>
		</trim>
		WHERE BAZ463=#{id}
	</update>

四、编写保存方法,入参类型为实体型:

<insert id="saveEntity" parameterType="org.test.settle.national.pension.PensionTransBill">
		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}
		)
	</insert>

场景1
当Mapper接口定义了多个参数的时候就需要使用Param注解来给参数取个名字,然后在Mapper.xml文件中,使用Param注解中的值(名字),告诉Mybatis你使用的变量是哪一个,用在哪,此时insert就不用在声明参数类型了,因为有多个参数类型,而paramType只能声明一个,同理,update,delete,select,都是一样的。

场景2
当Mapper接口只有一个参数的时候,在Mapper.xml文件中需要声明其参数类型,此时我们可以使用任意名称的变量来获取传入的值,因为传递进来的参数只有一个。(包括集合)

场景3
当Mapper接口定义了一个集合参数和简单类型参数的时候也需要使用Param注解来给参数取个名字,然后在Mapper.xml文件中使用取得名字来使用它们。集合在foreach中的colloection标签中,也是写的参数的名字,如果接口方法中只有一个集合参数,那么foreach中的colloection标签随便写什么都是可以的。
mybatis的mapper.xml编写_第1张图片
mybatis的mapper.xml编写_第2张图片

你可能感兴趣的:(Java,Web,mybatis,xml,java)