使用resultType进行输出映射,只有查询出来的列名和pojo中的属性名一致,该列才可以映射成功。
如果查询出来的列名和pojo的属性名不一致,通过定义一个resultMap对列名和pojo属性名之间作一个映射关系。
举例:
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.hand.tms.orderstatus.mapper.EdiIftstaMapper"> <resultMap id="BaseResultMap" type="com.hand.tms.orderstatus.dto.EdiIftsta"> <result column="ID" property="id" jdbcType="VARCHAR" /> <result column="DOCUMENT_NO" property="documentNo" jdbcType="VARCHAR" /> <result column="CREAT_DATE" property="creatDate" jdbcType="VARCHAR" /> <result column="SELECT_DATE" property="selectDate" jdbcType="VARCHAR" /> <result column="SELECT_STATUS" property="selectStatus" jdbcType="VARCHAR" /> <result column="CZ_ID" property="czId" jdbcType="VARCHAR" /> <result column="CZ_NAME" property="czName" jdbcType="VARCHAR" /> <result column="CZ_ADDR1" property="czAddr1" jdbcType="VARCHAR" /> <result column="CZ_ADDR2" property="czAddr2" jdbcType="VARCHAR" /> <result column="CZ_ADDR3" property="czAddr3" jdbcType="VARCHAR" /> <result column="CZ_CITY" property="czCity" jdbcType="VARCHAR" /> <result column="CZ_POST" property="czPost" jdbcType="VARCHAR" /> <result column="CZ_CODE" property="czCode" jdbcType="VARCHAR" /> <result column="CN_ID" property="cnId" jdbcType="VARCHAR" /> <result column="CN_NAME" property="cnName" jdbcType="VARCHAR" /> <result column="CN_ADDR1" property="cnAddr1" jdbcType="VARCHAR" /> <result column="CN_ADDR2" property="cnAddr2" jdbcType="VARCHAR" /> <result column="CN_ADDR3" property="cnAddr3" jdbcType="VARCHAR" /> <result column="CN_CITY" property="cnCity" jdbcType="VARCHAR" /> <result column="CN_POST" property="cnPost" jdbcType="VARCHAR" /> <result column="CN_CODE" property="cnCode" jdbcType="VARCHAR" /> <result column="DP_ID" property="dpId" jdbcType="VARCHAR" /> <result column="DP_NAME" property="dpName" jdbcType="VARCHAR" /> <result column="DP_ADDR1" property="dpAddr1" jdbcType="VARCHAR" /> <result column="DP_ADDR2" property="dpAddr2" jdbcType="VARCHAR" /> <result column="DP_ADDR3" property="dpAddr3" jdbcType="VARCHAR" /> <result column="DP_CITY" property="dpCity" jdbcType="VARCHAR" /> <result column="DP_POST" property="dpPost" jdbcType="VARCHAR" /> <result column="DP_CODE" property="dpCode" jdbcType="VARCHAR" /> <result column="OP_ID" property="opId" jdbcType="VARCHAR" /> <result column="OP_NAME" property="opName" jdbcType="VARCHAR" /> <result column="OP_ADDR1" property="opAddr1" jdbcType="VARCHAR" /> <result column="OP_ADDR2" property="opAddr2" jdbcType="VARCHAR" /> <result column="OP_ADDR3" property="opAddr3" jdbcType="VARCHAR" /> <result column="OP_CITY" property="opCity" jdbcType="VARCHAR" /> <result column="OP_POST" property="opPost" jdbcType="VARCHAR" /> <result column="OP_CODE" property="opCode" jdbcType="VARCHAR" /> <result column="FW_ID" property="fwId" jdbcType="VARCHAR" /> <result column="FW_NAME" property="fwName" jdbcType="VARCHAR" /> <result column="FW_ADDR1" property="fwAddr1" jdbcType="VARCHAR" /> <result column="FW_ADDR2" property="fwAddr2" jdbcType="VARCHAR" /> <result column="FW_ADDR3" property="fwAddr3" jdbcType="VARCHAR" /> <result column="FW_CITY" property="fwCity" jdbcType="VARCHAR" /> <result column="FW_POST" property="fwPost" jdbcType="VARCHAR" /> <result column="FW_CODE" property="fwCode" jdbcType="VARCHAR" /> <result column="TOTAL_WEIGHT" property="totalWeight" jdbcType="DECIMAL" /> <result column="WEIGHT_CODE" property="weightCode" jdbcType="VARCHAR" /> <result column="TOTAL_VOLUME" property="totalVolume" jdbcType="DECIMAL" /> <result column="VOLUME_CODE" property="volumeCode" jdbcType="VARCHAR" /> <result column="TOTAL_QTY" property="totalQty" jdbcType="DECIMAL" /> <result column="QTY_CODE" property="qtyCode" jdbcType="VARCHAR" /> <result column="MESSAGE_NO" property="messageNo" jdbcType="VARCHAR" /> <result column="STATUS1_CODE" property="status1Code" jdbcType="VARCHAR" /> <result column="STATUS2_CODE" property="status2Code" jdbcType="VARCHAR" /> <result column="STATUS_DESCRIBE" property="statusDescribe" jdbcType="VARCHAR" /> <result column="CONSIGNMENT_5NO" property="consignment5no" jdbcType="VARCHAR" /> <result column="CONSIGNMENT_NO" property="consignmentNo" jdbcType="VARCHAR" /> <result column="STATUS_DATE" property="statusDate" jdbcType="VARCHAR" /> <result column="AP_ID" property="apId" jdbcType="VARCHAR" /> <result column="DOMAIN" property="domain" jdbcType="VARCHAR" /> <result column="CREATOR" property="creator" jdbcType="VARCHAR" /> <result column="CREATED_DATE" property="createdDate" jdbcType="VARCHAR" /> <result column="MODIFIER" property="modifier" jdbcType="VARCHAR" /> <result column="UPDATED_DATE" property="updatedDate" jdbcType="VARCHAR" /> <result column="PW_CITY" property="pwCity" jdbcType="VARCHAR" /> </resultMap>
<!--Excel导出模板--> <select id = "queryExport" parameterType="com.hand.tms.orderstatus.dto.EdiIftsta" resultMap="BaseResultMap"> select distinct T.CONSIGNMENT_NO, T.STATUS1_CODE, T.STATUS2_CODE, T.OP_CITY, T.STATUS_DATE, T.OP_NAME, T.CREAT_DATE, decode(T.STATUS_DESCRIBE,'到达城市名称',T.STATUS_DESCRIBE||'/'||T.OP_CITY,T.STATUS_DESCRIBE) FROM T_EDI_IFTSTA T where rownum = 1 <if test = "consignmentNo !=null"> and T.CONSIGNMENT_NO = #{consignmentNo ,jdbcType = VARCHAR} </if> <if test="selectStatus !=null"> and T.SELECT_STATUS = #{selectStatus,jdbcType=VARCHAR} </if> </select>
resultMap 用一个集合包裹了dto传过来的值,column="OP_CITY"是数据库字段,property="opCity"是dto对应字段,通过result Map做一个映射关系。而restType则是,查出来是什么,如果和dto对应上,那么就有这个对象,如果查出来没有和dto对应,那么则无效。