便于在association中自动映射。需要加一个columnPrefix。
columnPrefix: 在resultMap的元素中加自动加前缀,便于重用resultmap。
例:
1.在每个字段上加author_
<resultMap id="authorResult" type="Author"> <id property="id" column="author_id"/> <result property="username" column="author_username"/> <result property="password" column="author_password"/> <result property="email" column="author_email"/> <result property="bio" column="author_bio"/> </resultMap>
2.association中使用
<resultMap id="blogResult" type="Blog"> <id property="id" column="blog_id" /> <result property="title" column="blog_title"/> <association property="author" resultMap="authorResult" /> <association property="coAuthor" resultMap="authorResult" columnPrefix="co_" /> </resultMap>
3.执行sql
<select id="selectBlog" resultMap="blogResult"> select B.id as blog_id, B.title as blog_title, A.id as author_id, A.username as author_username, A.password as author_password, A.email as author_email, A.bio as author_bio, CA.id as co_author_id, CA.username as co_author_username, CA.password as co_author_password, CA.email as co_author_email, CA.bio as co_author_bio from Blog B left outer join Author A on B.author_id = A.id left outer join Author CA on B.co_author_id = CA.id where B.id = #{id} </select>
改造结果:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.ruoyi.ruoyiwx.mapper.WxMassMapper"> <resultMap id="MassandsendResult" type="WxMass" autoMapping="true" > <!-- <id property="id" column="id"/>--> <!-- <result property="sysUnitId" column="sys_unit_id" />--> <!-- <result property="name" column="name" />--> <!-- <result property="type" column="type" />--> <!-- <result property="mediaId" column="media_id" />--> <!-- <result property="picurl" column="picurl" />--> <!-- <result property="scope" column="scope" />--> <!-- <result property="content" column="content" />--> <!-- <result property="status" column="status" />--> <!-- <result property="wxid" column="wxid" />--> <!-- <result property="opBy" column="opBy" />--> <!-- <result property="opAt" column="opAt" />--> <!-- <result property="delFlag" column="delFlag" />--> <association property="massSend" resultMap="wxMassSendResult" columnPrefix="ms_" > </association> </resultMap> <resultMap id="wxMassSendResult" type="WxMassSend" > <id property="id" column="ms_id" /> <result property="sysUnitId" column="sys_unit_id" /> <result property="massId" column="massId" /> <result property="receivers" column="receivers" /> <result property="status" column="status" /> <result property="msgId" column="msgId" /> <result property="errCode" column="errCode" /> <result property="errMsg" column="errMsg" /> <result property="wxid" column="wxid" /> <result property="opBy" column="opBy" /> <result property="opAt" column="opAt" /> <result property="delFlag" column="delFlag" /> </resultMap> <select id="getWxMassandsend" resultMap="MassandsendResult" > select m.*, ms.id ms_id, ms.sys_unit_id ms_sys_unit_id, ms.massId ms_massId, ms.receivers ms_receivers, ms.status ms_status, ms.msgId ms_msgId, ms.errCode ms_errCode, ms.errMsg ms_errMsg, ms.wxid ms_wxid, ms.opBy ms_opBy, ms.wxid ms_wxid, ms.opAt ms_opAt, ms.delFlag ms_delFlag from wx_mass m,wx_mass_send ms where m.id=ms.massId and m.wxid=ms.wxid and m.wxid=#{1} </select> </mapper>
参考: