mybatis Mapper文件中resultType与resultMap的区别
resultType与resultMap都用来定义返回值的类型。
resultType, 可以是Java 代码中的vo、po、entity的任意一个,使用的时候要写全包名路径。如下图所示:
<select id="countClearingData" resultType="com.money56.amp.api.module.fm.po.ClearingPO" parameterType="com.money56.amp.api.module.fm.vo.ClearingVo">
select *
from fm_clearing
</select>
resultMap, 只能是当前mapper文件中定义的返回数据。如下图所示:
<resultMap id="BaseResultMap" type="com.money56.amp.api.module.fm.entity.Clearing" >
<id column="clearing_id" property="clearingId" jdbcType="CHAR" />
<result column="total_money" property="totalMoney" jdbcType="DECIMAL" />
<result column="line_no" property="lineNo" jdbcType="CHAR" />
<result column="clearing_status" property="clearingStatus" jdbcType="INTEGER" />
<result column="clearing_time" property="clearingTime" jdbcType="TIMESTAMP" />
<result column="remarks" property="remarks" jdbcType="VARCHAR" />
</resultMap>
<delete id="delete" parameterType="java.lang.String" >
delete from fm_clearing
where clearing_id = #{clearingId,jdbcType=CHAR}
</delete>
resultMap 与 resultType,根据实际情况分别使用。
注意:使用resultType,select后面必须加别名,且要与resultType的实体类的属性一一对应。
如下图所示:select 后面as的别名与FinanceApplyPO里的属性是对应的,否则会出现匹配不到的情况,造成空指针。
<select id="getClearingDetail" resultType="com.money56.amp.api.module.lms.po.FinanceApplyPO" parameterType="com.money56.amp.api.module.fm.entity.ClearingDetail" >
SELECT a.borrow_apply_id as borrowApplyId,a.cost_interest as costInterest,a.rate as rate,a.interest_date as interestDate,a.deadline_date as deadlineDate,(SELECT SUM(b.repayment_interest)
FROM lms_repayment_plan as b
WHERE b.borrow_apply_id = #{borrowApplyId,jdbcType=CHAR}) as suminterest
FROM lms_finance_apply as a
WHERE a.borrow_apply_id = #{borrowApplyId,jdbcType=CHAR}
</select>