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获取数据.只是这个时候就有一个问题,这些数据 ...
随机推荐
- linux信任公钥的配置
一.每个用户都有自己的家目录 访问方式是:~/.ssh/id_rsa.pub 使用~就是表示家目录. 具体家目录在哪里,在用户密码配置文件中:/etc/passwd中.第6列的值就是. 可以使用~访问 ...
- MAC使用CocoaPods
前言,還是那句話,按照濤叔下面畫黃色的步驟順序執行就好了 使用CocoaPods兩種方式:使用之前安裝的插件&命令行. 一.利用插件 1.創建項目后添加CocoaPods 2.在文本框中輸入如 ...
- 瞬间读懂什么是互联网思维、大数据、O2O、众筹、红海
1.什么叫大数据? 某必胜客店的电话铃响了,客服人员拿起电话. 客服:必胜客.您好,请问有什么需要我为您服务? 顾客:你好,我想要一份…… 客服:先生,烦请先把您的会员卡号告诉我. 顾客:16846 ...
- $('div a') 与$('div>a'),.div+.div2与.div~.div2
$('div a'):div标签下所有层次a元素的jquery对象 $('div>a'):div标签下子元素层次a元素的jquery对象 <body> <div class=' ...
- Koala – 开源的前端预处理器语言图形编译工具
koala 是一个前端预处理器语言图形编译工具,支持 Less.Sass.Compass.CoffeeScript,帮助 Web 开发者更高效地使用它们进行开发.跨平台运行,完美兼容 Windows. ...
- 乱码之MyEclipse控制台
今天突然发现控制台出现乱码,查了资料解决方案不一. 我的解决方案如下: Run -> Debug Configuration... -> MyEclipse Servler -> M ...
- JavaScript中的普通函数与构造函数比较
问题 什么是构造函数?构造函数与普通函数区别是什么?用new关键字的时候到底做了什么?构造函数有返回值怎么办?构造函数能当普通函数调用吗? thisthis永远指向当前正在被执行的函数或方法的owne ...
- 在 SharePoint 2013 中选择正确的 API 集
决定使用哪个 API 集的因素 您可以在多个 API 集中选择一个来访问 SharePoint 2013 平台.您使用哪一个 API 集取决于以下因素: 应用程序的类型. 可能的类型包括但不限于以 ...
- Office 365 - SharePoint 2013 Online 之应用程序开发
1.给站点添加完Napa后,在网站内容里点击Napa,如下图: 2.创建一个新的app,如下图: 3.可以在Napa里添加新的项目,如下图: 4.添加新的文件,可以添加web页面.样式表.脚本,如下图 ...
- RecyclerView的基本使用
1.布局文件中使用 <android.support.v7.widget.RecyclerView android:id="@+id/recycleview" android ...