MyBatis3-实现多表关联数据的查询
前提:
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-实现多表关联数据的查询的更多相关文章
- hibernate的基础学习--多表关联数据查询
Hibernate共提供4种多表关联数据查询方式 OID数据查询+OGN数据查询方式 HQL数据查询方式 QBC数据查询方式 本地SQL查询方式(hibernate很少用) 1.OID数据查询+OGN ...
- EF里单个实体的增查改删以及主从表关联数据的各种增删 改查
本文目录 EF对单个实体的增查改删 增加单个实体 查询单个实体 修改单个实体 删除单个实体 EF里主从表关联数据的各种增删改查 增加(增加从表数据.增加主从表数据) 查询(根据主表找从表数据.根据从表 ...
- MagicalRecord 多表关联数据操作
最近在使用MagicalRecord做数据持久层CoreData的操作库,今天做了一个多表关联数据的操作,整理了一个demo,特此记录一下. 关于如何使用Cocopads 和 MagicalRecor ...
- 将一个多表关联的条件查询中的多表通过 create select 转化成一张单表的sql、改为会话级别临时表 【我】
将一个多表关联的条件查询中的多表通过 create select 转化成一张单表的sql 将结果改为创建一个会话级别的临时表: -- 根据下面这两个sql CREATE TABLE revenu ...
- 20.Yii2.0框架多表关联一对多查询之hasMany
目录 新手模式 hasMany关联模式查询 新建mode层Article.php 新建mode层Category.php 新建控制器HomeController.php 新手模式 用上次的查询结果,作 ...
- Mysql基础语法-建库-建表(增、删、改、查、表关联及子查询)
前言:MySQL是一个数据库管理系统,也是一个关系数据库.它是由Oracle支持的开源软件,MySQL可以在各种平台上运行UNIX,Linux,Windows等.可以将其安装在服务器甚至桌面系统上. ...
- 10-MySQL-Ubuntu-数据表中数据的查询(三)
数据的查询(select) (1)查询整个表的数据: select * from 表名; (2)查询给定条件的数据: select * from 表名 where 条件; (3)查询表中某些字段: ...
- T-SQL - query01_创建数据库|创建表|添加数据|简单查询
时间:2017-09-29 整理:byzqy 本篇以"梁山好汉花名册"为例,记录MS SQLServer T-SQL语句的使用,包含命令: 创建数据库 | 删除数据库 创建表 | ...
- MySQL多表关联数据同时删除
MySQL多表关联时的多表删除: DELETE t1, t2FROM t1LEFT JOIN t2 ON t1.id = t2.idWHERE t1.id = 25
随机推荐
- nonebot 源码阅读笔记
前言 nonebot 是一个 QQ 消息机器人框架,它的一些实现机制,值得参考. nonebot NoneBot 初始化(配置加载) 阅读 nonebot 文档,第一个示例如下: import non ...
- python完成简单购物功能
# # -*- coding: utf8 -*- # # Author:wxq # # date:2017/11/13 # # python 3.6 # 创建一个商品列表: product_lis = ...
- C++STL——set
一.相关定义 set 集合,有唯一性,即每一个元素只有一个: 是一个有序的容器,里面的元素都是排序好的: 支持插入,删除,查找等操作. 注意 set中的元素可以是任意类型的,但是由于需要排序,所以元素 ...
- Mininet实验 基于Mininet测量路径的损耗率
实验原理 在SDN环境中,控制器可以通过对交换机下发流表操作来控制交换机的转发行为,此外,还可以利用控制器测量路径的损耗率.在本实验中,基于Mininet脚本,设置特定的交换机间的路径损耗速率,然后编 ...
- postman工具【接口自动化测试关于断言】
在使用postman工具进行接口自动化时我们经常需要断言来进行判断,结果到底是成功还是失败. 但在collection runner/Newman里如果不加断言,跑完后都无法知道是成功还是失败 断言是 ...
- mysqli DB封装
<?php class DB { //私有的属性 private static $dbcon = false; private $host; private $port; private $us ...
- CSS设计指南之ID属性
1.用于页内导航的ID ID也可以用在页内导航连接中.下面就是一个链接,其目标是同一页的另一个位置. <a href="#bio">Biography</a> ...
- jquery/js iframe 元素操作
1.判断id/ class 是否存在? <script> $(function(){ if(("#id_name").length()>0){ //如果id 存在 ...
- CF858F Wizard's Tour 解题报告
题目描述 给定一张 \(n\) 个点 \(m\) 条边的无向图,每条边连接两个顶点,保证无重边自环,不保证连通. 你想在这张图上进行若干次旅游,每次旅游可以任选一个点 \(x\) 作为起点,再走到一个 ...
- SCU3037 Painting the Balls
Description Petya puts the \(N\) white balls in a line and now he wants to paint some of them in bla ...