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. Scalaz(9)- typeclass:checking instance abiding the laws

    在前几篇关于Functor和Applilcative typeclass的讨论中我们自定义了一个类型Configure,Configure类型的定义是这样的: case class Configure ...

  2. [moka同学笔记]Yii2 自定义class、自定义全局函数(摘录)

    1.在app\components下新建MyComponent.PHP namespace app\components; use Yii; use yii\base\Component; use y ...

  3. phpcms 二次开发数据过滤的技巧

    参数过滤 1,针对不能直接使用pdo进行参数绑定,可以使用sprintf模拟,并使用new_addslashes来过滤,然后使用query执行拼接的sql %% - 返回百分比符号 %b - 二进制数 ...

  4. Java中的GOF23(23中设计模式)--------- 单例模式(Singleton)

    Java中的GOF23(23中设计模式)--------- 单例模式(Singleton) 在Java这这门语言里面,它的优点在于它本身的可移植性上面,而要做到可移植的话,本身就需要一个中介作为翻译工 ...

  5. C++之虚函数的作用和使用方法

    在同一类中是不能定义两个名字相同.参数个数和类型都相同的函数的,否则就是“重复定义”.但是在类的继承层次结构中,在不同的层次中可以出现名字相同.参数个数和类型都相同而功能不同的函数.例如在例12.1( ...

  6. JQuery读取XML文件

    <?xml version="1.0" encoding="utf-8" ?> <taxrates> <taxrate id=&q ...

  7. [转]精通JS正则表达式

    原文路径:http://www.jb51.net/article/25313.htm 正则表达式可以: •测试字符串的某个模式.例如,可以对一个输入字符串进行测试,看在该字符串是否存在一个电话号码模式 ...

  8. JS高程2.在HTML中使用Javascript(2)

    1.延迟脚本defer 在<script>元素中设置defer属性,相当于告诉浏览器立即下载,但是延迟执行.<script>中的脚本会延迟到浏览器遇到</html> ...

  9. ubuntu 搭建开发环境

    一. 安装C/C++程序的开发环境 1. sudo apt-get install build-essential //安装主要编译工具 gcc, g++, make 2. sudo apt-get ...

  10. 几个有用的JSON工具

    好久没写博客了,这里都要长草了:) 这几天研究PLM360 REST API和Infraworks REST API,一天到晚和JSON打交道,发现这几个小工具非常好用,推荐给大家. 第0个,大名鼎鼎 ...