Mybatis—一对一和一对多查询


重新记录mybatis表查询

Mybatis的查询

一对一查询

连接词:association

数据表:作者表author 文章表article

描述:一个作者发表一篇文章

作者表
文章表

  1. xml:
<mapper namespace="com.liwang.mybatis02.mapper.AuthorMapper">

<resultMap id="AuthorMap" type="com.liwang.mybatis02.model.Author">  
    <id property="id" column="id"/>  
    <result property="name" column="name"/>  
    <result property="age" column="age"/>  
</resultMap>  

<select id="getAuthorById" resultMap="AuthorMap">  
    select * from author where id = #{id};  
</select>  

</mapper>
<mapper namespace="com.liwang.mybatis02.mapper.ArticleMapper">

<resultMap id="BaseArticleMap" type="com.liwang.mybatis02.model.Article">
        <id column="id" property="id"/>
        <result column="title" property="title"/>
        <result column="content" property="content"/>
</resultMap>

<resultMap id="ArticleMap" type="com.liwang.mybatis02.model.Article" extends="BaseArticleMap">
        <association property="author" javaType="com.liwang.mybatis02.model.Author" columnPrefix="author_"
        resultMap="com.liwang.mybatis02.mapper.AuthorMapper.AuthorMap">
<!--            <id property="id" column="id"/>-->
<!--            <result property="name" column="name"/>-->
<!--            <result property="age" column="age"/> 替换成上面的 resultMap中的AuthorMap-->
        </association>
</resultMap>

<select id="getArticleById3" resultMap="ArticleMap">
        SELECT a.*, au.id as author_id,au.name as author_name, au.age as author_age
        from article a, author au
        WHERE a.aid = au.id and a.id = #{id};
</select>

</mapper>
  1. 结果(查询文章及其作者)
    查询文章及其作者

一对多查询

连接词:collection

数据表:用户表user 角色表role 中间表user_role

描述:一个用户可以有多个角色,一个角色可以对应多个用户

用户表

角色表

用户-角色中间表

  1. xml:
<mapper namespace="com.liwang.mybatis02.mapper.RoleMapper">  

    <resultMap id="BaseRoleMap" type="com.liwang.mybatis02.model.Role">  
        <id property="id" column="id"/>  
        <result property="name" column="name"/>  
    </resultMap> 

</mapper>
<mapper namespace="com.liwang.mybatis02.mapper.UserMapper">

    <resultMap id="BaseUserMap" type="com.liwang.mybatis02.model.User">
        <id property="id" column="id"/>
        <result property="username" column="username"/>
        <result property="email" column="email"/>
    </resultMap>

    <resultMap id="UserMapWithRole" type="com.liwang.mybatis02.model.User" extends="BaseUserMap">
        <collection property="roles" ofType="com.liwang.mybatis02.model.Role" columnPrefix="role_"
                    resultMap="com.liwang.mybatis02.mapper.RoleMapper.BaseRoleMap">
        </collection>
    </resultMap>

    <select id="getUserWithRole" resultMap="UserMapWithRole">
        select u.*,r.id as role_id,r.name as role_name
        from user as u
        left join user_role ur on u.id = ur.uid
        left join role r on ur.rid = r.id;
    </select>
</mapper>
  1. mapper:
public interface UserMapper {
    // 获取用户信息及角色信息
    List getUserWithRole();
}
  1. test
@Test
public void getUserWithRole() {
    List<User> users = userMapper.getUserWithRole();
        for (User user : users) {
            System.out.println("user = " + user);
        }
}
  1. 结果(查询用户及其对应的角色)

结果
association和collection的使用就暂时记录这些,后续在继续添加。

可以看看我的学习——基于Hexo的个人博客:
网站:https://liwangc.gitee.io/
—————————————————————————
作为初学者,很多知识都没有掌握,见谅,如有错误请指出,以期进步,感谢!。后续有新的学习,继续补充上来。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值