前提:

1、新建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 ('', '', 'test_title', 'test_content');
Insert INTO `article` VALUES ('', '', 'test_title_2', 'test_content_2');
Insert INTO `article` VALUES ('', '', 'test_title_3', 'test_content_3');
Insert INTO `article` VALUES ('', '', 'test_title_4', 'test_content_4');

实现步骤,也是多对一的实现:

1、新建Article的类,也就是POJOs,与上面新建的article表一一对应,代码如下:

package com.jsoft.testmybatis.models;

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类型。

2、配置User.xml,这里要引入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 as aid,article.title,article.content from user,article where user.id=article.userid and user.id=#{id}
</select>

用association来得到关联的用户,这是多对一的情况,因为所有的文章都是同一个用户的。

还有另外一种处理方式,可以复用前面已经定义好的resultMap,前面定义过一个resultListUser,定义如下:

    <!-- 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>

3、在IUserOperation接口中加入select对应的id名称相同的方法:

public List<Article> getUserArticles(int id);

4、在Configuration.xml中配置typeAliases:

    <typeAliases>
<typeAlias alias="Article" type="com.jsoft.testmybatis.models.Article" />
</typeAliases>

5、测试核心代码:

                List<Article> articles = userOperation.getUserArticles(1);
for (Article article : articles) {
System.out.println( article.getTitle() + ":" + article.getContent() +
":作者是:" + article.getUser().getUserName() +
":地址:" + article.getUser().getUserAddress());
}

6、测试结果:

测试工程:https://github.com/easonjim/5_java_example/tree/master/mybatis/test4

参考:

http://www.yihaomen.com/article/java/306.htm

MyBatis3-实现多表关联数据的查询的更多相关文章

  1. hibernate的基础学习--多表关联数据查询

    Hibernate共提供4种多表关联数据查询方式 OID数据查询+OGN数据查询方式 HQL数据查询方式 QBC数据查询方式 本地SQL查询方式(hibernate很少用) 1.OID数据查询+OGN ...

  2. EF里单个实体的增查改删以及主从表关联数据的各种增删 改查

    本文目录 EF对单个实体的增查改删 增加单个实体 查询单个实体 修改单个实体 删除单个实体 EF里主从表关联数据的各种增删改查 增加(增加从表数据.增加主从表数据) 查询(根据主表找从表数据.根据从表 ...

  3. MagicalRecord 多表关联数据操作

    最近在使用MagicalRecord做数据持久层CoreData的操作库,今天做了一个多表关联数据的操作,整理了一个demo,特此记录一下. 关于如何使用Cocopads 和 MagicalRecor ...

  4. 将一个多表关联的条件查询中的多表通过 create select 转化成一张单表的sql、改为会话级别临时表 【我】

    将一个多表关联的条件查询中的多表通过 create   select  转化成一张单表的sql 将结果改为创建一个会话级别的临时表: -- 根据下面这两个sql CREATE TABLE revenu ...

  5. 20.Yii2.0框架多表关联一对多查询之hasMany

    目录 新手模式 hasMany关联模式查询 新建mode层Article.php 新建mode层Category.php 新建控制器HomeController.php 新手模式 用上次的查询结果,作 ...

  6. Mysql基础语法-建库-建表(增、删、改、查、表关联及子查询)

    前言:MySQL是一个数据库管理系统,也是一个关系数据库.它是由Oracle支持的开源软件,MySQL可以在各种平台上运行UNIX,Linux,Windows等.可以将其安装在服务器甚至桌面系统上. ...

  7. 10-MySQL-Ubuntu-数据表中数据的查询(三)

    数据的查询(select) (1)查询整个表的数据: select  * from 表名; (2)查询给定条件的数据: select  * from 表名 where 条件; (3)查询表中某些字段: ...

  8. T-SQL - query01_创建数据库|创建表|添加数据|简单查询

    时间:2017-09-29  整理:byzqy 本篇以"梁山好汉花名册"为例,记录MS SQLServer T-SQL语句的使用,包含命令: 创建数据库 | 删除数据库 创建表 | ...

  9. MySQL多表关联数据同时删除

    MySQL多表关联时的多表删除: DELETE t1, t2FROM    t1LEFT JOIN t2 ON t1.id = t2.idWHERE    t1.id = 25

随机推荐

  1. SpringMVC 上传图片保存到服务器 同时更改图片名称保存至数据库

    @RequestMapping(value = "/save.do", method = RequestMethod.POST)    public String saveDriv ...

  2. PHP+IIS上传大文件

    最近刚接触IIS服务器,在使用php上传大文件的时候,遇到了一些问题.通过查阅网上资料进行了总结,希望对各位有帮助. 第一步,检查PHP的配置. 打开php.ini配置文件 1.file_upload ...

  3. lintcode-45-最大子数组差

    45-最大子数组差 给定一个整数数组,找出两个不重叠的子数组A和B,使两个子数组和的差的绝对值|SUM(A) - SUM(B)|最大. 返回这个最大的差值. 注意事项 子数组最少包含一个数 样例 给出 ...

  4. 关于debian配置的问题汇总

    debian的apache多域名配置: https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-ho ...

  5. [转] const int *a与int *const a,const int *const a的区别

    http://blog.csdn.net/zhangheng837964767/article/details/33783511 关键问题点:const 属于修饰符 ,关键是看const 修饰的位置在 ...

  6. 201621123033 《Java程序设计》第14周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结与数据库相关内容. 2. 使用数据库技术改造你的系统 2.1 简述如何使用数据库技术改造你的系统.要建立什么表?截图你的表设计. 2 ...

  7. javascript中判断变量是否存在的正确方式

    在Javascript中,我们通常判断一个变量是否存在(即不为null或者undefined),往往是这样判断的 if(tomy){ console.log(obj.name); } 这种写法在大部分 ...

  8. [剑指Offer] 19.顺时针打印矩阵

    [思路]本题关键在于 右->左 和 下->上 两个循环体中的判断条件,即判断是否重复打印. class Solution { public: vector<int> print ...

  9. 【Python】Python—判断变量的基本类型

    type() >>> type(123)==type(456) True >>> type(123)==int True >>> type('ab ...

  10. BZOJ3295: [Cqoi2011]动态逆序对 莫队

    这个用莫队做会被卡,但是我还是...... 收获在于树状数组维护后缀和以及双维排序...... 莫队的时间复杂度比想象中的要好一些.... 然而我还是被卡了...... #include<ios ...