SSM学习08MyBatis关联映射一对多

<resultMap>中的<collection>子元素就是用来处理一对多映射关系的。

1.在mybatis数据库中新建表:

  

 1 create table tb_user(
 2     id int(32) primary key auto_increment,
 3     username varchar(32),
 4     address varchar(256)
 5 );
 6  
 7 insert into tb_user values('1','詹姆斯','克利夫兰');
 8 insert into tb_user values('2','科比','洛杉矶');
 9 insert into tb_user values('3','保罗','洛杉矶');
10  
11 create table tb_orders(
12     id int(32) primary key auto_increment,
13     number varchar(32) not null,
14     user_id int(32) not null,
15     foreign key(user_id) references tb_user(id)
16 );
17  
18 insert into tb_orders values('1','1000011','1');
19 insert into tb_orders values('2','1000012','2');
20 insert into tb_orders values('3','1000013','3');
21 insert into tb_orders values('4','1000014','1');

可以看出tb_orders表中引用tb_user中的主键作为外键。

Orders类:

 1 package com.zyk.po;
 2 
 3 import java.util.List;
 4 
 5 public class Orders {
 6     private int id;
 7     @Override
 8     public String toString() {
 9         return "Orders [id=" + id + ", productlist=" + productlist + ", number=" + number + "]";
10     }
11     private List<Product> productlist;
12     private String number;
13     public List<Product> getProductlist() {
14         return productlist;
15     }
16     public void setProductlist(List<Product> productlist) {
17         this.productlist = productlist;
18     }
19     
20     public int getId() {
21         return id;
22     }
23     public void setId(int id) {
24         this.id = id;
25     }
26     public String getNumber() {
27         return number;
28     }
29     public void setNumber(String number) {
30         this.number = number;
31     }
32     
33     
34 }

这里的product和他的get set方法是后面实验所要用的要先去掉!!!!!!!!!!

User类:

 1 package com.zyk.po;
 2 
 3 import java.util.List;
 4 
 5 public class User {
 6     private int id;
 7     @Override
 8     public String toString() {
 9         return "User [id=" + id + ", username=" + username + ", address=" + address + ", orderList=" + orderList + "]";
10     }
11     private String username;
12     private String address;
13     private List<Orders> orderList;
14     public int getId() {
15         return id;
16     }
17     public void setId(int id) {
18         this.id = id;
19     }
20     public String getUsername() {
21         return username;
22     }
23     public void setUsername(String username) {
24         this.username = username;
25     }
26     public String getAddress() {
27         return address;
28     }
29     public void setAddress(String address) {
30         this.address = address;
31     }
32     public List<Orders> getOrderList() {
33         return orderList;
34     }
35     public void setOrderList(List<Orders> orderList) {
36         this.orderList = orderList;
37     }
38     
39 }

从User类中可以看出,一个User可以有很多Orders,就是很典型的一对多。

UserMapper.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE mapper
 3    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 <mapper namespace="com.itheima.mapper.UserMapper">
 6    
 7     <select id="findUserWithOrders" parameterType="Integer" resultMap="UserWithOrdersResult">
 8         select u.*,o.id as orders_id,o.number
 9         from tb_user u,tb_orders o
10         where u.id=o.user_id
11         and u.id=#{id}
12     </select>
13     <resultMap type="User" id="UserWithOrdersResult">
14       <id property="id" column="id"/>
15       <result property="username" column="username"/>
16       <result property="address" column="address"/> 
17       <collection property="ordersList" ofType="Orders">
18           <id property="id" column="orders_id"/>
19           <result property="number" column="number"/>
20       </collection> 
21     </resultMap>
22 </mapper>

分析该xml:

  <select>中还是先写的sql语句,从tb_user和tb_orders查询u中的全部内容,o中的order_id和o的number且u的id和o的user_id相同。<resulyMap>重点就是映射规则了

测试类:

1 @Test
2     public void findUserTest() {
3         SqlSession sqlSession=MybatisUtils.getSession();
4         User user=sqlSession.selectOne("com.zyk.mapper.UserMapper.findUserWithOrders", 1);
5         System.out.println(user);
6         sqlSession.close();
7     }

实验结果:

  

猜你喜欢

转载自www.cnblogs.com/2312947032zyk/p/10566596.html