sql
-- 表a ta (id,aname,bid) -- 表b tb (id,bname) SELECT T1.*, T2.BNAME FROM TA T1 LEFT JOIN TB T2 ON T1.BID = T2.ID
xml
<resultMap id="BaseResultMap" type="com.zxg.pojo.Ta" > <id column="ID" property="id" jdbcType="CHAR" /> <result column="ANAME" property="aName" jdbcType="VARCHAR" /> <association column="BID" property="bName" select="com.zxg.dao.TbMapper.getTbNameById"/> </resultMap>
<select id="getTbNameById" resultType="java.lang.String" parameterType="java.lang.String" > SELECT BNAME FROM TB WHERE ID = #{id} </select>
- 内部调用不需要在mapper接口中定义(getTbNameById 只写到mapper.xml 中即可)
- 注:resultType or resultMap 和 association的property类型保持一致。
association 传多个参数
<association column="{key1=COL1,key2=COL2}" property="pro" select="mapper.method"/>
- parameterType="java.util.Map"
- #{key1,jdbcType=VARCHAR}
- #{key2,jdbcType=VARCHAR}