zoukankan      html  css  js  c++  java
  • Mybatis学习(4)实现关联数据的查询

    有了前面几章的基础,对一些简单的应用是可以处理的,但在实际项目中,经常是关联表的查询,比如最常见到的多对一,一对多等。这些查询是如何处理的呢,这一讲就讲这个问题。我们首先创建一个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;

    -- ----------------------------
    -- 添加几条测试数据
    -- ----------------------------
    Insert INTO `article` VALUES ('1', '1', 'test_title', 'test_content');
    Insert INTO `article` VALUES ('2', '1', 'test_title_2', 'test_content_2');
    Insert INTO `article` VALUES ('3', '1', 'test_title_3', 'test_content_3');
    Insert INTO `article` VALUES ('4', '1', 'test_title_4', 'test_content_4');

    你应该发现了,这几个文章对应的userid都是1,所以需要用户表user里面有id=1的数据。可以修改成满足自己条件的数据.按照orm的规则,表已经创建了,那么肯定需要一个对象与之对应,所以我们增加一个 Article 的class
    程序代码 程序代码

    package com.yihaomen.mybatis.model;

    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;
        }

    }


    注意一下,文章的用户是怎么定义的,是直接定义的一个User对象。而不是int类型。

    多对一的实现
    场景:在读取某个用户发表的所有文章。当然还是需要在User.xml 里面配置 select 语句, 但重点是这个 select 的resultMap 对应什么样的数据呢。这是重点,这里要引入 association 看定义如下:
    程序代码 程序代码

    < !-- User 联合文章进行查询 方法之一的配置 (多对一的方式)  -->    
        <resultMap id="resultUserArticleList" type="Article">
            <id property="id" column="aid" />
            <result property="title" column="title" />
            <result property="content" column="content" />
            
            <association property="user" javaType="User">
                <id property="id" column="id" />
                <result property="userName" column="userName" />
                <result property="userAddress" column="userAddress" />            
            </association>        
        </resultMap>

    < select id="getUserArticles" parameterType="int" resultMap="resultUserArticleList">
           select user.id,user.userName,user.userAddress,article.id aid,article.title,article.content from user,article
                  where user.id=article.userid and user.id=#{id}
        </select>

    这样配置之后,就可以了,将select 语句与resultMap 对应的映射结合起来看,就明白了。用association 来得到关联的用户,这是多对一的情况,因为所有的文章都是同一个用户的。

    还有另外一种处理方式,可以复用我们前面已经定义好的 resultMap ,前面我们定义过一个 resultListUser ,看这第二种方法如何实现:
    程序代码 程序代码

    <resultMap type="User" id="resultListUser">
            <id column="id" property="id" />
            <result column="userName" property="userName" />
            <result column="userAge" property="userAge" />
            <result column="userAddress" property="userAddress" />
        </resultMap>

        <!-- User 联合文章进行查询 方法之二的配置 (多对一的方式) -->    
        <resultMap id="resultUserArticleList-2" type="Article">
            <id property="id" column="aid" />
            <result property="title" column="title" />
            <result property="content" column="content" />        
            <association property="user" javaType="User" resultMap="resultListUser" />            
        </resultMap>
        
        <select id="getUserArticles" parameterType="int" resultMap="resultUserArticleList">
           select user.id,user.userName,user.userAddress,article.id aid,article.title,article.content from user,article
                  where user.id=article.userid and user.id=#{id}
        </select>

    将 association  中对应的映射独立抽取出来,可以达到复用的目的。

    好了,现在在Test 类中写测试代码:
    程序代码 程序代码

    public void getUserArticles(int userid){
            SqlSession session = sqlSessionFactory.openSession();
            try {
                IUserOperation userOperation=session.getMapper(IUserOperation.class);          
                List<Article> articles = userOperation.getUserArticles(userid);
                for(Article article:articles){
                    System.out.println(article.getTitle()+":"+article.getContent()+
                            ":作者是:"+article.getUser().getUserName()+":地址:"+
                             article.getUser().getUserAddress());
                }
            } finally {
                session.close();
            }
        }


    漏掉了一点,我们一定要在 IUserOperation 接口中,加入 select 对应的id 名称相同的方法:
    public List<Article> getUserArticles(int id);

    然后运行就可以测试。
  • 相关阅读:
    Atitit.Java exe bat  作为windows系统服务程序运行
    Atitit. Object-c语言 的新的特性  attilax总结
    Atitit. Object-c语言 的新的特性  attilax总结
    Atitit。Time base gc 垃圾 资源 收集的原理与设计
    Atitit。Time base gc 垃圾 资源 收集的原理与设计
    Atitit.go语言golang语言的新的特性  attilax总结
    Atitit.go语言golang语言的新的特性  attilax总结
    Atitit.pdf 预览 转换html attilax总结
    Atitit.pdf 预览 转换html attilax总结
    Atitit.office word  excel  ppt pdf 的web在线预览方案与html转换方案 attilax 总结
  • 原文地址:https://www.cnblogs.com/love-omnus/p/4196581.html
Copyright © 2011-2022 走看看