SSM-7-关联关系
- 一对一
- 一对多
- 多对多
一对一
嵌套查询跟嵌套结果
从配置文件可以看出嵌套查询是嵌入子查询,在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"> <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 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 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"> <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 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;
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"> <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/
版权声明:转载请注明出处!