0%

SSM-7-关联关系

SSM-7-关联关系

  1. 一对一
  2. 一对多
  3. 多对多

一对一

嵌套查询跟嵌套结果

从配置文件可以看出嵌套查询是嵌入子查询,在sql语句里加入sql语句,而嵌套结果是用多表查询,一次性查询,故效率上嵌套结果比嵌套查询要好

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<mapper namespace="com.itheima.mapper.PersonCardMapper">
<!-- 嵌套查询: 通过执行另外一条sql映射语句来返回预期的特殊类型 -->
<select id="findPersonById" parameterType="Integer"
resultMap="IdCardWithPersonResult">
select * from tb_person where id = #{id}
</select>
<resultMap type="Person" id="IdCardWithPersonResult">
<id property="id" column="id" />
<result property="name" column="name" />
<result property="age" column="age" />
<result property="sex" column="sex" />
<!-- 一对一: association使用select属性引入另外一条sql语句 -->
<association property="card" column="card_id"
javaType="IdCard"
select="com.itheima.mapper.IdCardMapper.findCodeById">
</association>
</resultMap>
<!-- 嵌套结果: 使用嵌套结果映射来映射重复的联合结果的子集 -->
<select id="findPersonById2" parameterType="Integer"
resultMap="IdCardWithPersonResult2">
select p.*, idcard.code from tb_person p, tb_idcard idcard
where p.card_id=idcard.id and p.id=#{id}
</select>
<resultMap type="Person" id="IdCardWithPersonResult2">
<id property="id" column="id" />
<result property="name" column="name" />
<result property="age" column="age" />
<result property="sex" column="sex" />
<!-- 一对一: association使用select属性引入另外一条sql语句 -->
<association property="card" javaType="IdCard">
<id property="id" column="card_id" />
<result property="code" column="code" />
</association>
</resultMap>
</mapper>

延迟加载

在mybatis-config里设置

1
2
3
4
5
6
<settings>
<!-- 打开延迟加载 -->
<setting name="lazyLoadingEnabled" value="true" />
<!-- 积极改为消极 -->
<setting name="aggressiveLazyLoading" value="false"/>
</settings>

这里可以打开延迟加载,延迟加载是指只需要一方数据的时候,只查询一方,当需要多方数据时候他才再次发出sql语句进行查询

一对多

配置文件如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?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="com.itheima.mapper.UserMapper">
<!-- 1对多:查看某一用户及其关联的订单信息 注意: 当关联查询出的列名相同时,需要使用别名区分 -->
<select id="findUserWithOrders" parameterType="Integer"
resultMap="UserWithOrderResult">
select u.*,o.id as orders_id,o.number from tb_user
u,tb_orders o where u.id=o.user_id and u.id=#{id}
</select>
<resultMap type="User" id="UserWithOrderResult">
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="address" column="address"/>
<!-- 一对多关联映射: collection
ofType: 表示属性集合中元素的类型,List<Orders>即Orders类 -->
<collection property="ordersList" ofType="Orders">
<id property="id" column="orders_id"/>
<result property="number" column="number"/>
</collection>
</resultMap>
</mapper>

最主要是配置sql语句吧,1对多的时候防止主键名称相同冲突,这里需要注意下,命名别名

User

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.itheima.po;

/**
* @author NoOne dotaerday@gmail.com:
* @version 创建时间:2020年3月8日 上午11:27:37
* 类说明
*/

import java.util.List;

/**
* 用户持久化类
*/
public class User {
private Integer id; // 用户编号
private String username; // 用户姓名
private String address; // 用户地址
private List<Orders> ordersList; // 用户关联的订单

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 getAddress() {
return address;
}

public List<Orders> getOrdersList() {
return ordersList;
}

public void setAddress(String address) {
this.address = address;
}

public void setOrdersList(List<Orders> ordersList) {
this.ordersList = ordersList;
}

@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", address=" + address + ", ordersList=" + ordersList
+ "]";
}
}

这里就是User,在写resultMap的时候对应上

多对多

配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?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="com.itheima.mapper.OrdersMapper">
<!-- 多对多嵌套查询: 通过执行另外一条sql映射语句来返回预期的特殊类型 -->
<select id="findOrdersWithProduct" parameterType="Integer"
resultMap="OrdersWithProductResult">
select * from tb_orders where id=#{id}
</select>
<resultMap type="Orders" id="OrdersWithProductResult">
<id property="id" column="id" />
<result property="number" column="number" />
<collection property="productList" column="id"
ofType="Product"
select="com.itheima.mapper.ProductMapper.findProductById">
</collection>
</resultMap>
<!-- 多对多嵌套结果: 通过查询某订单及其关联的商品详情 -->
<select id="findOrdersWithProduct2" parameterType="Integer"
resultMap="OrdersWithProductResult2">
select o.*,p.id as pid,p.name,p.price from tb_orders
o,tb_product p,tb_ordersitem oi
where oi.orders_id=o.id
and
oi.product_id=p.id
and o.id=#{id}
</select>
<!-- 定义手动映射类型 -->
<resultMap type="Orders" id="OrdersWithProductResult2">
<id property="id" column="id" />
<result property="number" column="number" />
<!-- 多对多关联映射 -->
<collection property="productList" ofType="Product">
<id property="id" column="pid"/>
<result property="name" column="name"/>
<result property="price" column="price"/>
</collection>
</resultMap>
</mapper>

这里主要考察sql语句的熟练程度,跟一对多类似,只是sql语句比较长了点

本文作者:NoOne
本文地址https://noonegroup.xyz/posts/2959f7ce/
版权声明:转载请注明出处!