Mybatis学习4——多对一
一个用户对多个订单
在用户中添加属性List<user>
User.java
package pojo; import java.util.Date;
import java.util.List; public class User { private Integer id;
private String username;// 鐢ㄦ埛濮撳悕
private String sex;// 鎬у埆
private Date birthday;// 鐢熸棩
private String address;// 鍦板潃
private String uuid2;
private List<Order> orderList; public List<Order> getOrderList() {
return orderList;
} public void setOrderList(List<Order> orderList) {
this.orderList = orderList;
} public String getUuid2() {
return uuid2;
} public void setUuid2(String uuid2) {
this.uuid2 = uuid2;
} public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
} public Date getBirthday() {
return birthday;
} public void setBirthday(Date birthday) {
this.birthday = birthday;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} @Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", sex=" + sex + ", birthday=" + birthday + ", address="
+ address + ", uuid2=" + uuid2 + ", orderList=" + orderList + "]";
} }
mapper文件UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="mapper.UserMapper">
<resultMap type="user" id="user_order_map">
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="address" column="address"/>
<result property="birthday" column="birthday"/>
<result property="sex" column="sex"/>
<!-- collection用于配置一对多关联 -->
<collection property="orderList" ofType="order">
<id property="id" column="oid"/>
<result property="userId" column="id"/>
<result property="number" column="number"/>
<result property="createtime" column="createtime"/>
<result property="note" column="note"/>
</collection>
</resultMap>
<select id="getUserOrderMap" resultMap="user_order_map">
select
u.id,
u.username,
u.birthday,
u.sex,
u.address,
u.uuid2,
o.id oid,
o.number,
o.createtime,
o.note
from
user u
left join
order1 o
on o.user_id = u.id </select>
</mapper>
测试
@Test
public void getUserOrderMap() {
SqlSession openSession = SqlSessionFactoryUtil.getSqlSessionFactory().openSession();
//获得接口实现类
UserMapper mapper = openSession.getMapper(UserMapper.class);
List<User> orderUserMap = mapper.getUserOrderMap();
for (User user : orderUserMap) {
List<Order> orderList = user.getOrderList();
for (Order order : orderList) {
if(order.getId()!=null)
System.out.println("========"+order);
}
}
openSession.close();
}
Mybatis学习4——多对一的更多相关文章
- 【MyBatis学习10】高级映射之多对多查询
本文来总结一下mybatis中的多对多映射,从第8节的文章中可以看出,用户表和商品表示多对多关系,它们两的多对多是通过订单项和订单明细这两张表所关联起来的,那么这一节主要来总结一下用户表和商品表之间的 ...
- 【Todo】Mybatis学习-偏理论
之前写过好几篇Mybatis相关的文章: http://www.cnblogs.com/charlesblc/p/5906431.html <SSM(SpringMVC+Spring+Myba ...
- 【MyBatis学习笔记】
[MyBatis学习笔记]系列之预备篇一:ant的下载与安装 [MyBatis学习笔记]系列之预备篇二:ant入门示例 [MyBatis学习笔记]系列之一:MyBatis入门示例 [MyBatis学习 ...
- MyBatis学习总结(二)——MyBatis核心配置文件与输入输出映射
在上一章中我们学习了<MyBatis学习总结(一)——ORM概要与MyBatis快速起步>,这一章主要是介绍MyBatis核心配置文件.使用接口+XML实现完整数据访问.输入参数映射与输出 ...
- MyBatis学习总结(三)——多表关联查询与动态SQL
在上一章中我们学习了<MyBatis学习总结(二)——MyBatis核心配置文件与输入输出映射>,这一章主要是介绍一对一关联查询.一对多关联查询与动态SQL等内容. 一.多表关联查询 表与 ...
- Mybatis 学习总结
1 Mybatis入门 1.1 单独使用jdbc编程问题总结 1.1.1 jdbc程序 public static void main(String[] args) { Connection conn ...
- MyBatis学习(二)---数据表之间关联
想要了解MyBatis基础的朋友可以通过传送门: MyBatis学习(一)---配置文件,Mapper接口和动态SQL http://www.cnblogs.com/ghq120/p/8322302. ...
- Mybatis学习---基础知识考核
MyBatis 2.什么是MyBatis的接口绑定,有什么好处 接口映射就是在IBatis中任意定义接口,然后把接口里面的方法和SQL语句绑定, 我们直接调用接口方法就可以,这样比起原来了Sql ...
- mybatis学习笔记之学习目录(1)
mybatis学习笔记之学习结构(1) 学习结构: 1.mybatis开发方法 原始dao开发方法(程序需要编写dao接口和dao实现类) mybatis的mapper接口(相当于dao接口)代理开发 ...
随机推荐
- chrome flash
chrome://settings/content/flash 在Chrome地址栏中输入:chrome://settings/content/flash,进入Flash设置,勾选允许网站运行flas ...
- python基本知识点
1.基本数据类型 1.1int 字符串转换为数字,比如 a = “123” print(type(a) , a) b = int(a) print(type(b),b) num = “b” v = i ...
- 【亲测可用,亦可配置同一平台的不同账号,例如阿里云的两个不同账号】Windows下Git多账号配置,同一电脑多个ssh-key的管理
Windows下Git多账号配置,同一电脑多个ssh-key的管理 这一篇文章是对上一篇文章<Git-TortoiseGit完整配置流程>的拓展,所以需要对上一篇文章有所了解,当然直接 ...
- 关于Javascript闭包(Closure)
闭包(closure)是Javascript语言的一个难点,也是它的特色,很多高级应用都要依靠闭包实现. 一.变量的作用域 要理解闭包,首先必须理解Javascript特殊的变量作用域. 变量的作用域 ...
- RTMP与HLS压力测试工具安装与配置
在CentOS 6.5环境中安装依赖软件包,使用git下载最新版本st-load源码包 [root@localhost ~]# yum install git unzip patch gcc gcc- ...
- bzoj3815: 卡常数
随机数据,带修改,求到空间中到给定点距离为给定值的点的编号,唯一解. 建三维kdtree,对查询用可行性剪枝在树上找,由于数据随机,插入删除时不需要维护平衡. #include<bits/std ...
- 固态硬盘使用简要手册——windows平台
第一步,请安装它. 第二步,请关闭磁盘整理功能,如图 详细:控制面板--管理工具--任务计划程序 打开界面,设置如下 图1 第三步:预读(Superfetch)和快速搜索(Windows Search ...
- [UE4]显示队友
- [UE4]Make Array创建数组,而不是定义数组
当不想新建一个数组对象的时候,就可以使用“Make Array”创建一个数组
- Navicat 12 破解方法
1.按步骤安装Navicat Premium,如果没有可以去官网下载:http://www.navicat.com.cn/download/navicat-premium 2.安装好后下载激活文件:h ...