一个用户对多个订单

在用户中添加属性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——多对一的更多相关文章

  1. 【MyBatis学习10】高级映射之多对多查询

    本文来总结一下mybatis中的多对多映射,从第8节的文章中可以看出,用户表和商品表示多对多关系,它们两的多对多是通过订单项和订单明细这两张表所关联起来的,那么这一节主要来总结一下用户表和商品表之间的 ...

  2. 【Todo】Mybatis学习-偏理论

    之前写过好几篇Mybatis相关的文章: http://www.cnblogs.com/charlesblc/p/5906431.html  <SSM(SpringMVC+Spring+Myba ...

  3. 【MyBatis学习笔记】

    [MyBatis学习笔记]系列之预备篇一:ant的下载与安装 [MyBatis学习笔记]系列之预备篇二:ant入门示例 [MyBatis学习笔记]系列之一:MyBatis入门示例 [MyBatis学习 ...

  4. MyBatis学习总结(二)——MyBatis核心配置文件与输入输出映射

    在上一章中我们学习了<MyBatis学习总结(一)——ORM概要与MyBatis快速起步>,这一章主要是介绍MyBatis核心配置文件.使用接口+XML实现完整数据访问.输入参数映射与输出 ...

  5. MyBatis学习总结(三)——多表关联查询与动态SQL

    在上一章中我们学习了<MyBatis学习总结(二)——MyBatis核心配置文件与输入输出映射>,这一章主要是介绍一对一关联查询.一对多关联查询与动态SQL等内容. 一.多表关联查询 表与 ...

  6. Mybatis 学习总结

    1 Mybatis入门 1.1 单独使用jdbc编程问题总结 1.1.1 jdbc程序 public static void main(String[] args) { Connection conn ...

  7. MyBatis学习(二)---数据表之间关联

    想要了解MyBatis基础的朋友可以通过传送门: MyBatis学习(一)---配置文件,Mapper接口和动态SQL http://www.cnblogs.com/ghq120/p/8322302. ...

  8. Mybatis学习---基础知识考核

    MyBatis 2.什么是MyBatis的接口绑定,有什么好处 接口映射就是在IBatis中任意定义接口,然后把接口里面的方法和SQL语句绑定,    我们直接调用接口方法就可以,这样比起原来了Sql ...

  9. mybatis学习笔记之学习目录(1)

    mybatis学习笔记之学习结构(1) 学习结构: 1.mybatis开发方法 原始dao开发方法(程序需要编写dao接口和dao实现类) mybatis的mapper接口(相当于dao接口)代理开发 ...

随机推荐

  1. Speeding Up The Traveling Salesman Using Dynamic Programming

    Copied From:https://medium.com/basecs/speeding-up-the-traveling-salesman-using-dynamic-programming-b ...

  2. position和float小结

    position属性值 Position的属性值共有四个static.relative.absolute.fixed. static 所有元素在默认的情况下position属性均为static,而我们 ...

  3. 【git】之常用命令

    再使用git过程常用的命令在6-10左右,但是如果你想精通git那么需要记住的命令在80左右, 下面看这张图,我们了解一下git的概念的常用操作! Workspace:工作区(例如eclipse的工作 ...

  4. java浅析final关键字

    谈到final关键字,想必很多人都不陌生,在使用匿名内部类的时候可能会经常用到final关键字.另外,Java中的String类就是一个final类,那么今天我们就来了解final这个关键字的用法. ...

  5. 堆叠箱子(基础dp)

    P1086 时间限制: 1 Sec  内存限制: 128 MB提交: 38  解决: 27[提交][状态][讨论版][命题人:外部导入] 题目描述 现有N种箱子,每种箱子高度H_i,数量C_i.现选取 ...

  6. Hbase物理模型设计

    Hbase的存储结构 1.Hbase宏观架构 从上图可以看hbase集群由一个master和多个RegionServer组成,右下角是一个RegionServer的内部图. Hbase的服务器角色构成 ...

  7. github_源码

      固定头部: hongyangAndroid/Android-StickyNavLayout:ListView 与ViewPager 滑动冲突处理,滑动到顶部固定位置停顿; ufo22940268/ ...

  8. [UE4]Cast to OverlaySlot、Set Vertical Alignment、Get Slot,解决Child Widget垂直居中对齐问题

  9. 在Docker中安装配置Oracle12c并实现数据持久化

    在Docker中安装配置Oracle12c并实现数据持久化 选定镜像,并pull到系统中,一定要先配置加速,不然超级慢 eric@userver:~$ docker pull sath89/oracl ...

  10. C# DataGridView导出Excel

    using Microsoft.Office.Interop.Excel;                using Excel=Microsoft.Office.Interop.Excel; //这 ...