一对多关联映射
买家和订单的关系就是一对多的关系
我们首先来创建person类和order类
public class Person {
private String id;
private String name;
private String address;
private String tel;
List<Order> orders;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public List<Order> getOrders() {
return orders;
}
public void setOrders(List<Order> orders) {
this.orders = orders;
}
}
public class Order {
private String id;
private String number;
private int price;
private Person p;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public Person getP() {
return p;
}
public void setP(Person p) {
this.p = p;
}
}
并且在数据库中创建对应的实体类表,如下:
并添加数据如下:
接下来我们来配置映射文件。
Person.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.cc.mybatis.domain.Person">
<resultMap type="Person" id="personMap">
<id column="personId" property="id"/>
<result column="personName" property="name"/>
<result column="personAddress" property="address"/>
<result column="personTel" property="tel"/>
<!-- 一对多 -->
<!-- property:指的是集合属性的值 ofType:集合中元素的类型-->
<collection property="orders" ofType="Order">
<id column="orderId" property="id"/>
<result column="orderNumber" property="number"/>
<result column="orderPrice" property="price"/>
</collection>
</resultMap>
<select id="selectPersonById" parameterType="string" resultMap="personMap">
select p.*,o.* from person p,orders o where p.personId = o.pid and p.personId = #{id}
</select>
</mapper>
添加映射文件到mybatis的配置中:
测试方法和测试结果:
多对一的映射
配置Order.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.cc.mybatis.domain.Order">
<resultMap type="Order" id="orderMap">
<id column="orderId" property="id"/>
<result column="orderNumber" property="number"/>
<result column="orderPrice" property="price"/>
<!-- 多对一的关系 -->
<association property="p" javaType="Person">
<id column="personId" property="id"/>
<result column="personName" property="name"/>
<result column="personAddress" property="address"/>
<result column="personTel" property="tel"/>
</association>
</resultMap>
<!-- 根据id查询Order,关联将Person查询出来 -->
<select id="selectOrderById" parameterType="string" resultMap="orderMap">
select p.*,o.* from person p,orders o where p.personId = o.pid and o.orderId = #{id}
</select>
</mapper>
再配置中加入映射文件:
测试方法和执行结果: