最關鍵的是明白 resultMap 的兩個最重要的做用:-1 :當你在 select 中,
resultType
是一個 Entity(或 Model等 pojo 時),若是你的 sql 結果集字段與你的pojo
屬性名不一致時,這時能夠經過resultMap
起到 從新映射成你pojo
中名字的結果集java-2 : 第二個做用應該是使用量最多的一種,就是涉及到多對一的結果集映射或者一對多的結果集映射時git
association
用於一對一
和多對一
的狀況
collection
用於一對一
和一對多
的狀況github
<!--created by util.you.com@gmail.com search--> <select id="search" parameterType="java.util.Map" resultMap="reFundList"> SELECT csr.id, csr.strategy_name, csr.remark, csr.`status`, sut.username, DATE_FORMAT(IFNULL(csr.insert_time, ''), '%Y-%m-%d %H:%i:%s') AS insert_time FROM `CL_STRATEGY_REFUND` AS csr LEFT JOIN `SYS_USER_TBL` AS sut ON sut.user_id = csr.insert_user_id WHERE csr.`status` = 1 <if test="strategyName != null and strategyName != ''"> AND csr.strategy_name LIKE CONCAT('%',#{strategyName}, '%') </if> ORDER BY csr.id DESC <if test="offset != null and limit != null"> limit #{offset}, #{limit} </if> ; </select> <!--created by util.you.com@gmail.com search 由於這裏返回類型(即封裝類型)是 Map,因此 property 的值就是 Map 封裝的 key 名稱--> <resultMap id="reFundList" type="java.util.Map"> <id column="id" property="id"/> <result column="strategy_name" property="strategyName"/> <result column="remark" property="remark"/> <result column="username" property="userName"/> <result column="insert_time" property="insertTime"/> <collection property="ruleList" javaType="ArrayList" column="id" select="selectReFundInfo"/> </resultMap> <!--created by util.you.com@gmail.com search--> <select id="selectReFundInfo" parameterType="java.lang.Integer" resultType="java.util.Map"> select csrr.id, csrr.strategy_id, csrr.time_limit, csrr.fee_type, csrr.fee_value from `CL_STRATEGY_REFUND_REL` as csrr where csrr.`status` = 1 and csrr.strategy_id = #{id} </select>
注意點請看下圖
![]()
![]()
原創手敲不易,轉載請註明出處,謝謝。我是
拉丁小毛,歡迎你們關注我哦,一塊兒交流,共同進步。有問題能夠
郵我哦(util.you.com@gmail.com)
推薦一波個人新我的博客哦,歡迎你們訪問哦新我的博客哦sql