Mybatis学习笔记四:关联查询

在上一章中,主要实现了使用Mybatis进行增删查改。在本章中,主要来看下如何使用mybatis进行关联查询。


1、创建Article表


-- ----------------------------
--  Table structure for `article`
-- ----------------------------
DROP TABLE IF EXISTS `article`;
CREATE TABLE `article` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `userid` int(11) NOT NULL,
  `title` varchar(100) NOT NULL,
  `content` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

-- ----------------------------
--  Records of `article`
-- ----------------------------
BEGIN;
INSERT INTO `article` VALUES ('1', '1', 'test_title', 'test_content'), ('2', '1', 'test_title_2', 'test_content_2'), ('3', '1', 'test_title_3', 'test_content_3'), ('4', '1', 'test_title_4', 'test_content_4');
COMMIT;

2、添加Article类


在model包中创建一个article类:
public class Article {
    private int id;
    private User user; //用户使用类进行定义
    private String title;
    private String content;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @Override
    public String toString() {
        return "Article{" +
                "id=" + id +
                ", user=" + user +
                ", title='" + title + '\'' +
                ", content='" + content + '\'' +
                '}';
    }
}
其中,User是我们在前三章中定义好的类。

3、修改相关配置文件


根据第一章所述,我们在定义了一个新的model文件之后,就需要定义他的别名,首先在mybatis-config.xml中定义Article的别名:
<typeAlias alias="Article" type="com.dfz.mybatis.model.Article"/>
在UserMapper接口中添加一个新的方法
public List<Article> getUserArticles(int userId);
这个方法从名字上就可以看出,我们想要使用userid来查出用户相关的文章。
配置User.xml,在其中添加getUserArticles方法:
<resultMap id="resultUserArticleList" type="Article">
        <id property="id" column="aid"/>
        <result property="content" column="content"/>
        <result property="title" column="title"/>
        <!-- -->
        <association property="user" javaType="User">
            <id property="id" column="id"/>
            <result property="username" column="username"/>
            <result property="password" column="password"/>
            <result property="address" column="address"/>
        </association>

    </resultMap>
    
    <select id="getUserArticles" parameterType="int" resultMap="resultUserArticleList">
         select user.id,user.username,user.address,user.password,article.id as aid,article.title,article.content from user,article
              where user.id=article.userid and user.id=#{id}
    </select>
使用association来定义user,并进行联合查询。
association也可以定义为:
<association property="user" javaType="User" resultMap="resultListUser"/>
以提高代码复用程度。

其中需要说明的是,如果两个表中存在重名字段,如上文所述uesr表中有id字段,article表中同样有id字段,在使用Mybatis进行联合查询的过程中,会遇到只返回一条数据的情况。然而在数据库中直接运行sql语句是正常的。
在这里需要注意的是,在出现重名字段时,配置Mybatis文件时需要把字段重新命名,否则Mybatis会混乱。

<id property="id" column="aid"/>

select user.id,user.username,user.address,user.password,article.id as aid,article.title,article.content from user,article
              where user.id=article.userid and user.id=#{id}
如代码中所示,在这里就把article表中的id字段命名为了aid,即可正常显示。
结果如下:




4、总结


1)Mybatis联合查询可以使用association标签进行配置;
2)当存在不同表有重名字段时,使用别名进行区分,否则mybatis只能返回一条数据。
未尽之处后期再补,代码下载地址: https://github.com/EdwardEricZhang/MybatisFun

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值