mybatis使用重要内容多对多(中间表)

今天算是mybatis中,在应用中最为常用的部分,也是mybatis应用中,最后的一部分。

  两个表多对多的查询,多对多的查询需要借助第三张表做为介质来连接,在映射文件xml中的配置与一对多一样,

  使用collection oftype标签

  核心是sql语句的写法

  <resultMap id="roleAndUserMap" type="com.itheima.domain.Role">
<id property="role_id" column="id"></id>
<result property="role_name" column="role_name"></result>
<result property="role_desc" column="role_desc"></result>
<collection property="users" ofType="com.itheima.domain.User">
<id property="id" column="id"></id>
<result property="username" column="username"></result>
<result property="sex" column="sex"></result>
<result property="address" column="address"></result>
<result property="birthday" column="birthday"></result>
</collection>
</resultMap>

<select id="findAll" resultMap="roleAndUserMap">
SELECT r.*,u.`id`,u.username,u.sex,u.address,u.birthday FROM role AS r
LEFT OUTER JOIN user_role AS ur ON r.id = ur.rid
LEFT OUTER JOIN user as u ON ur.uid = u.id ;
</select>

posted @ 2021-02-01 14:40  煎饼侠是我  阅读(458)  评论(0)    收藏  举报