MyBatis 关联查询

 

mybatis框架有两种关联查询方式,分别为嵌套查询,嵌套结果。嵌套结果只需查询一次所以更为常用。

一、一对一(association)

User基础类

public class User {
    private int id;
    private String name;
    private String password;
}

Article基础类,Article类中包含一个User对象的字段

public class Article {
    private int id;
    private String time;
    private User authorUser;
}

每个Article 中只有一个User字段,故以下为一对一嵌套查询

ArticleMapper.xml中通过嵌套结果查询所有Article

<resultMap id="articleResultMap" type="bean.Article">
    <id property="id" column="id"/>
    <result property="time" column="time"/>
    <association property="authorUser" javaType="User" resultMap="userResultMap"/>
 </resultMap>

 <resultMap id="userResultMap" type="bean.User">
    <id property="id" column="user_id"/>
    <result property="name" column="name"/>
    <result property="password" column="password"/>
 </resultMap>
<select id="selectArticle" resultMap="articleResultMap">
    select news.id, time, user_id, name, password from news ,user where user_id = user.id
</select>

ArticleMapper.xml中通过嵌套查询查询所有Article

    <!--嵌套查询-->
<resultMap id="articleResultMap" type="bean.Article">
    <id property="id" column="id"/>
    <result property="time" column="time"/>

    <association property="authorUser" column="user_id"             
         select="mapper.UserMapper.selectUserById">
    </association>

</resultMap>
<select id="selectArticle" resultMap="articleResultMap">
    select news.id, time, user_id, name, password from news ,user where user_id = user.id
</select>

二、一对多(collection)

User基础类

public class User {
    private int id;
    private String name;
    private String password;
}

Article基础类,Article类中包含多个User对象的字段

public class Article {
    private int id;
    private String time;
    private List<User> authorUser;
}

每个Article 中只有多个User,故以下为一对多嵌套查询

ArticleMapper.xml中通过嵌套结果查询所有Article

<resultMap id="articleResultMap" type="bean.Article">
    <id property="id" column="id"/>
    <result property="time" column="time"/>
    <collection property="authorUser" ofType="User" resultMap="userResultMap"/>
 </resultMap>

 <resultMap id="userResultMap" type="bean.User">
    <id property="id" column="user_id"/>
    <result property="name" column="name"/>
    <result property="password" column="password"/>
 </resultMap>
<select id="selectArticle" resultMap="articleResultMap">
    select news.id, time, user_id, name, password from news ,user where user_id = user.id
</select>

ArticleMapper.xml中通过嵌套查询查询所有Article

    <!--嵌套查询-->
<resultMap id="articleResultMap" type="bean.Article">
    <id property="id" column="id"/>
    <result property="time" column="time"/>

    <collection property="authorUser" column="user_id" ofType="User"             
         select="mapper.UserMapper.selectUserById">
    </collection>

</resultMap>
<select id="selectArticle" resultMap="articleResultMap">
    select news.id, time, user_id, name, password from news ,user where user_id = user.id
</select>

三、多对多也是collection,跟一对多相同

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值