mybatis中的collection嵌套sql查询

<resultMap id="UserPhotoMap" type="com.example.user.bean.UserPhotoMsg">
    <id column="id" property="id" />
    <result column="account" property="account" />
    <result column="name" property="name" />
    <result column="sex" property="sex" />
    //嵌套查询语句collection中的column属性,是需要传入嵌套sql中的参数,参数必须是外层中有的字段
    //可以多个参数,也可以单个参数
    //多参形式:如 photoUserId=id,photoName=name
    //参数相当于把id赋于photoUserId,name赋于photoName
    //在嵌套sql中就可以使用被赋予的属性名称#{photoUserId},#{photoName}
    // collection中的select属性是写嵌套sql的映射位置,位置是嵌套sql在项目中的mapper层的方法名称
    <collection property="photoMessage" select="com.example.user.mapper.UserMapper.showPhotos" column="id">
    </collection>
</resultMap>
<select id="showUserList" resultMap="UserPhotoMap">
    select u.id,u.account,u.sex from user u
</select>

<resultMap id="photoMap" type="com.example.photomessage.entity.PhotoMessage">
    <id column="id" property="id" />
    <result column="photoName" property="photoName" />
    <result column="photoUserId" property="photoUserId" />
</resultMap>
<select id="showPhotos" resultMap="photoMap">
    select p.photoName,p.photoUserId from photomessage p where p.photoUserId = #{id}
</select>

猜你喜欢

转载自blog.csdn.net/weixin_43831522/article/details/111635682
今日推荐