最关键的是明白 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