mybatis的嵌套查询:是通过select标签中的id属性绑定对应的接口方法名,resultMap属性绑定嵌套查询的结果,写上要查询的sql语句
mybatis的嵌套结果是指返回的结果是一个对象里面包含另一个对象,mybaits需要先通过resultMap标签声明外面的对象属性,里面的对象属性通过association标签声明里面的标签。
<!--嵌套结果 只查一次--><resultMap id="ordersResultMap" type="Orders"><id property="id" column="id"/><result property="user_id" column="user_id"/><result property="number" column="number"/><result property="createtime" column="createtime"/><result property="note" column="note"/><!--<association property="user" javaType="User">--><!--<id property="id" column="user_id"/>--><!--<result property="username" column="username"/>--><!--<result property="address" column="address"/>--><!--</association>--><!--嵌套查询 查2次--><association property="user" select="mapper.UserMapper.mySelectByPrimary2" column="user_id"></association></resultMap><select id="selectByPrimary2" resultMap="ordersResultMap">select o.*, u.username, u.addressfrom orders o,user uwhere o.user_id = u.idand o.id = #{id}</select>
