1.关联关系是有方向的.
1)一对多关联:一个老师可以教多个学生,多个学生只有一个老师来教,站在老师方,就是一对多关联.
2)多对一关联:一个老师可以教多个学生,多个学生只有一个老师来教,站在学生方,就是多对一关联.
3)一对一关联:一个老师辅导一个学生,一个学生只请教一个老师.学生和老师是一对一.
4)多对多关联:园区划线的车位和园区的每一辆车.任意一个车位可以停任意一辆车.任意一车辆车可以停在任意一个车位上.
2.一对多关联关系
客户和订单就是典型的一对多关联关系.
一个客户名下可以有多个订单.
客户表是一方,订单表是多方.客户一中持有订单的集合.
使用一对多的关联关系,可以满足查询客户的同时查询该客户名下的所有订单.
客户表:
订单表:
Customer实体类:
package com.xin.entity;
import java.util.List;
public class Customer {
//customer表中的三个列
private Integer id;
private String name;
private Integer age;
//该客户名下的所有订单集合
private List<Orders> ordersList;
public Customer() {
}
public Customer(Integer id, String name, Integer age, List<Orders> ordersList) {
this.id = id;
this.name = name;
this.age = age;
this.ordersList = ordersList;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public List<Orders> getOrdersList() {
return ordersList;
}
public void setOrdersList(List<Orders> ordersList) {
this.ordersList = ordersList;
}
@Override
public String toString() {
return "Customer{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", ordersList=" + ordersList +
'}';
}
}
CustomerMapper接口:
import com.xin.entity.Customer;
public interface CustomerMapper {
//根据客户的id查询所有用户信息并同时查询该客户下的所有订单
Customer getById(Integer id);
}
CustomerMapper.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="com.xin.mapper.CustomerMapper">
<!--
//根据客户的id查询所有用户信息并同时查询该客户下的所有订单
Customer getById(Integer id);
customer表: //customer表中的三个列
private Integer id;
private String name;
private Integer age;
//该客户名下的所有订单集合
private List<Orders> ordersList;
orders表:public Integer id;
public String orderNumber;
public String orderPrice;
-->
<resultMap id="customermap" type="customer">
<!--主键绑定-->
<id property="id" column="cid"></id>
<!--非主键绑定-->
<result property="name" column="name"></result>
<result property="age" column="age"></result>
<!-- //该客户名下的所有订单集合
private List<Orders> ordersList;
orders表:public Integer id;
public String orderNumber;
public String orderPrice;
-->
<collection property="ordersList" ofType="orders">
<!--主键绑定-->
<id property="id" column="oid"></id>
<!--非主键绑定-->
<result property="orderNumber" column="orderNumber"></result>
<result property="orderPrice" column="orderPrice"></result>
</collection>
</resultMap>
<select id="getById" parameterType="int" resultMap="customermap">
select c.id cid,name,age,o.id oid,orderNumber,orderPrice,customer_id
from customer c left join orders o on c.id=o.customer_id
where c.id=#{id}
</select>
</mapper>
测试:
@Test
public void testGetById() {
Customer customer=customerMapper.getById(1);
System.out.println(customer);
}
3.多对一关联关系.
订单和客户就是多对一关联.
站在订单的方向查询订单的同时将客户信息查出.
订单是多方,会持有一方的对象.客户是一方.
Orders1实体类:
package com.xin.entity;
public class Orders1 {
public Integer id;
public String orderNumber;
public String orderPrice;
public Customer customer;
public Orders1() {
}
public Orders1(Integer id, String orderNumber, String orderPrice, Customer customer) {
this.id = id;
this.orderNumber = orderNumber;
this.orderPrice = orderPrice;
this.customer = customer;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getOrderNumber() {
return orderNumber;
}
public void setOrderNumber(String orderNumber) {
this.orderNumber = orderNumber;
}
public String getOrderPrice() {
return orderPrice;
}
public void setOrderPrice(String orderPrice) {
this.orderPrice = orderPrice;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
@Override
public String toString() {
return "Orders1{" +
"id=" + id +
", orderNumber='" + orderNumber + '\'' +
", orderPrice='" + orderPrice + '\'' +
", customer=" + customer +
'}';
}
}
Orders1Mapper接口:
package com.xin.mapper;
import com.xin.entity.Orders1;
public interface Orders1Mapper {
Orders1 getById1(Integer id);
}
Orders1Mapper.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="com.xin.mapper.Orders1Mapper">
<!--
Orders1Mapper getById(Integer id);
orders1表:public Integer id;
public String orderNumber;
public String orderPrice;
public Customer customer;
customer表:/customer表中的三个列
private Integer id;
private String name;
private Integer age;
//该客户名下的所有订单集合
private List<Orders> ordersList;//这个不用写
-->
<resultMap id="orders1map" type="orders1">
<id property="id" column="oid"></id>
<result property="orderNumber" column="orderNumber"></result>
<result property="orderPrice" column="orderPrice"></result>
<association property="customer" javaType="customer">
<id property="id" column="cid"></id>
<result property="name" column="name"></result>
<result property="age" column="age"></result>
</association>
</resultMap>
<select id="getById1" parameterType="int" resultMap="orders1map">
select o.id oid,orderNumber,orderPrice,customer_id,c.id cid,name,age
from orders o inner join customer c on o.customer_id=c.id
where o.id=#{id}
</select>
</mapper>
测试:
@Test
public void testGetById1() {
Orders1 orders1=orders1Mapper.getById1(11);
System.out.println(orders1);
}
总结:无论是什么关联关系,如果某方持有另一方的集合,则使用< collection >标签完成映射,如果某方持有另一方的对象,则使用< association >标签完成映射。
版权声明:本文为m0_56336875原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。