myBatis系列之四:关联数据的查询
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" />
- <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>
myBatis系列之四:关联数据的查询的更多相关文章
- MyBatis系列四 之 智能标签进行查询语句的拼接
MyBatis系列四 之 智能标签进行查询语句的拼接 使用Foreach进行多条件查询 1.1 foreach使用数组进行多条件查询 在MyBatis的映射文件中进行如下配置 <!--根据数组进 ...
- mybatis实战教程(mybatis in action)之四:实现关联数据的查询
有了前面几章的基础,对一些简单的应用是可以处理的,但在实际项目中,经常是关联表的查询,比如最常见到的多对一,一对多等.这些查询是如何处理的呢,这一讲就讲这个问题.我们首先创建一个Article 这个表 ...
- 4. mybatis实战教程(mybatis in action)之四:实现关联数据的查询
转自:https://www.cnblogs.com/shanheyongmu/p/5653599.html 有了前面几章的基础,对一些简单的应用是可以处理的,但在实际项目中,经常是关联表的查询,比如 ...
- mybatis实战教程(mybatis in action)之四:实现关联数据的查询
有了前面几章的基础,对一些简单的应用是可以处理的,但在实际项目中,经常是关联表的查询,比如最常见到的多对一,一对多等.这些查询是如何处理的呢,这一讲就讲这个问题.我们首先创建一个Article 这个表 ...
- Mybatis学习(4)实现关联数据的查询
有了前面几章的基础,对一些简单的应用是可以处理的,但在实际项目中,经常是关联表的查询,比如最常见到的多对一,一对多等.这些查询是如何处理的呢,这一讲就讲这个问题.我们首先创建一个Article 这个表 ...
- mybatis 使用resultMap实现关联数据的查询(association 和collection )
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "- ...
- MyBatis3-实现多表关联数据的查询
前提: 1.新建Article表和增加模拟数据,脚本如下: Drop TABLE IF EXISTS `article`; Create TABLE `article` ( `id` ) NOT NU ...
- Mybatis中的关联映射和查询
一.商品订单数据模型 1.数据表 这里定义了四个表,分别表示用户,商品,订单,和订单详情. 用户表user CREATE TABLE `user` ( `id` int(11) NOT NULL AU ...
- 网络相关系列之四:数据解析之SAX方式解析XML数据
一.XML和Json数据的引入: 通常情况下.每一个须要訪问网络的应用程序都会有一个自己的server.我们能够向server提交数据,也能够从server获取数据.只是这个时候就有一个问题,这些数据 ...
随机推荐
- Scalaz(3)- 基础篇:函数概括化-Generalizing Functions
Scalaz是个通用的函数式编程组件库.它提供的类型.函数组件都必须具有高度的概括性才能同时支持不同数据类型的操作.可以说,scalaz提供了一整套所有编程人员都需要的具有高度概括性的通用函数,它是通 ...
- 泛函编程(33)-泛函IO:Free Functor - Coyoneda
在前几期讨论中我们终于推导出了Free Monad.这是一个Monad工厂,它可以把任何F[A]变成Monad.可惜的是它对F[A]是有所要求的:F必须是个Functor.Free Monad由此被称 ...
- linux网络编程 no route to host 解决方案
linux网络编程 no route to host 解决方案 [整合资料] (2013-05-13 21:38:12) 转载▼ 标签: net iptables it 分类: Linux 参考资料h ...
- jQuery cbpContentSlider 滑动切换
cbpContentSlider是一款选项卡插件,只要按照以下html结构就可以自动生成菜单切换内容特效. 在线实例 实例演示 使用方法 <div id="cbp-contentsli ...
- 基于jPlayer的三分屏制作
三分屏,这里的三分屏只是在一个播放器里同时播放三个视频,但是要求只有一个控制面板同时控制它们,要求它们共享一个时间轨道.这次只是简单的模拟了一下功能,并没有深入的研究. 首先,需要下载jPlayer, ...
- [js开源组件开发]localStorage-cache本地存储的缓存管理
localStorage-cache本地存储的缓存管理 距离上次的组件开发有近三个月的时间了,最近一直在做一些杂事,无法静下心来写写代码,也是在学习emberjs,在emberjs中有一个很重要的东西 ...
- Windows 安装 GTK+ 图文说明
首先去官方下载: //官方网站 http://www.gtk.org/download/index.php //下载链接 http://win32builder.gnome.org/gtk+-bund ...
- Sass的使用和基础语法
sass安装 官网下载ruby的windows安装包,安装时勾选上添加到环境变量add ruby executables to your path.安装完成后打开命令行,ruby -v输出内容则安装完 ...
- Sql Server Always On主库与附库遇到的问题
使用Always On的时候永远要记住只有一个主数据库可写,如果写的话要不在监听节点上做写的操作,要不只在主数据库上写的操作不然只读库无法读写
- Sharepoint学习笔记—习题系列--70-576习题解析 -(Q81-Q83)
Question 81You are designing a custom administrative timer job for a SharePoint 2010 farm. You need ...