myBatis系列之三:增删改查是基于单表的查询,如果联表查询,返回的是复合对象,需要用association关键字来处理。 
如User发表Article,每个用户可以发表多个Article,他们之间是一对多的关系。

1. 创建Article表,并插入测试数据:

-- Drop the table if exists
DROP TABLE IF EXISTS `Article`; -- Create a table named 'Article'
CREATE TABLE `Article` (
`id` int NOT NULL AUTO_INCREMENT,
`user_id` int NOT NULL,
`title` varchar(100) NOT NULL,
`content` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; -- Add several test records
INSERT INTO `article`
VALUES
('', '', 'title1', 'content1'),
('', '', 'title2', 'content2'),
('', '', 'title3', 'content3'),
('', '', 'title4', 'content4');

2. com.john.hbatis.model.Article类:

public class Article {
private int id;
private User user;
private String title;
private String content;
// Getters and setters are omitted
}

3. 在IUserMapper中添加:

List<Article> getArticlesByUserId(int id); 

4. 在User.xml中添加:

<resultMap type="com.john.hbatis.model.Article" id="articleList">
<id column="a_id" property="id" />
<result column="title" property="title" />
<result column="content" property="content" /> <association property="user" javaType="User"><!-- user属性映射到User类 -->
<id column="id" property="id" />
<result column="name" property="name" />
<result column="address" property="address" />
</association>
</resultMap> <select id="getArticlesByUserId" parameterType="int" resultMap="articleList">
select u.id, u.name, u.age, u.address, a.id a_id, a.title, a.content
from article a
inner join user u
on a.user_id=u.id and u.id=#{id}
</select>

5. 测试方法:

@Test
public void getArticlesByUserIdTest() {
SqlSession session = sqlSessionFactory.openSession();
try {
IUserMapper mapper = session.getMapper(IUserMapper.class);
List<Article> articles = mapper.getArticlesByUserId(1);
for (Article article : articles) {
log.info("{} - {}, author: {}", article.getTitle(), article.getContent(), article.getUser().getName());
}
} finally {
session.close();
}
}

附:除了在association标签内定义字段和属性的映射外,还可以重用User的resultMap:

<association property="user" javaType="User" resultMap="userList" />
  1. <resultMap type="com.john.hbatis.model.Article" id="articleList">
  2. <id column="a_id" property="id" />
  3. <result column="title" property="title" />
  4. <result column="content" property="content" />
  5. <association property="user" javaType="User"><!-- user属性映射到User类 -->
  6. <id column="id" property="id" />
  7. <result column="name" property="name" />
  8. <result column="address" property="address" />
  9. </association>
  10. </resultMap>
  11. <select id="getArticlesByUserId" parameterType="int" resultMap="articleList">
  12. select u.id, u.name, u.age, u.address, a.id a_id, a.title, a.content
  13. from article a
  14. inner join user u
  15. on a.user_id=u.id and u.id=#{id}
  16. </select>

myBatis系列之四:关联数据的查询的更多相关文章

  1. MyBatis系列四 之 智能标签进行查询语句的拼接

    MyBatis系列四 之 智能标签进行查询语句的拼接 使用Foreach进行多条件查询 1.1 foreach使用数组进行多条件查询 在MyBatis的映射文件中进行如下配置 <!--根据数组进 ...

  2. mybatis实战教程(mybatis in action)之四:实现关联数据的查询

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

  3. 4. mybatis实战教程(mybatis in action)之四:实现关联数据的查询

    转自:https://www.cnblogs.com/shanheyongmu/p/5653599.html 有了前面几章的基础,对一些简单的应用是可以处理的,但在实际项目中,经常是关联表的查询,比如 ...

  4. mybatis实战教程(mybatis in action)之四:实现关联数据的查询

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

  5. Mybatis学习(4)实现关联数据的查询

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

  6. mybatis 使用resultMap实现关联数据的查询(association 和collection )

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "- ...

  7. MyBatis3-实现多表关联数据的查询

    前提: 1.新建Article表和增加模拟数据,脚本如下: Drop TABLE IF EXISTS `article`; Create TABLE `article` ( `id` ) NOT NU ...

  8. Mybatis中的关联映射和查询

    一.商品订单数据模型 1.数据表 这里定义了四个表,分别表示用户,商品,订单,和订单详情. 用户表user CREATE TABLE `user` ( `id` int(11) NOT NULL AU ...

  9. 网络相关系列之四:数据解析之SAX方式解析XML数据

    一.XML和Json数据的引入: 通常情况下.每一个须要訪问网络的应用程序都会有一个自己的server.我们能够向server提交数据,也能够从server获取数据.只是这个时候就有一个问题,这些数据 ...

随机推荐

  1. psql 命令行使用

    如果觉得直接打开数据库修改繁琐,那么使用终端命令行是方便而又高大上的.下面来看看有哪些命令行: 说明:如果是正式的服务器则需要进行一个操作在执行下面的命令 ssh name @主机地址 -- name ...

  2. cnodejs社区论坛1--登陆

  3. 《Continuous Delivery》 Notes 1: The problem of delivering software

    What is "Deployment pipeline"? A deployment pipeline is an automated implementation of you ...

  4. HTML5锚点请用id代替name

    HTML5已经去掉name属性,实现锚点时请使用id,实例效果:http://keleyi.com/keleyi/phtml/html5/9.htm 支持Chrome,火狐,IE8以上等浏览器. 以下 ...

  5. [js开源组件开发]html5标签audio的样式更改

    html5标签audio的样式更改 由于html5的流行,现在移动端大多数的需求都可以使用audio来播放音频,但您可能只是需要很简单的播放/停止效果,但不同的浏览器上的audio样式却不尽人意,所以 ...

  6. 闭包和this

    一.闭包 最开始理解闭包是在一个函数内部定义一个函数,可以在外面的环境里进行调用.现在对于闭包的理解是利用函数来保存作用域内的对象. 理解闭包首先要理解执行上下文,变量对象,活动对象,作用域链.因为执 ...

  7. apache EnableMMAP指令

    官方说明地址:http://httpd.apache.org/docs/2.4/mod/core.html#enablemmap Use memory-mapping to read files du ...

  8. Session Storage、Cache Storage

    Session Storage sessionStorage用于本地存储一个会话(session)的数据,这些数据只有在同一个会话中的页面才能访问并且当会话结束后数据也随之销毁(浏览器关闭).因此se ...

  9. spring(4)——自动装配

    set注入和构造注入有时在做配置时比较麻烦.所以框架为了提高开发效率,提供自动装配功能,简化配置.spring框架式默认不支持自动装配的,要想使用自动装配需要修改spring配置文件中<bean ...

  10. jQuery原型属性和方法总结

    从大四下学期开始了解jquery源码相关的东西,在回校参加毕业典礼(准确的说是参加补考挂科太多)期间便开始借着<jQuery>内幕学习jquery源码,然后在博客园写笔记也已经两个月了,也 ...