说在前面
这个问题不知道其他人会不会遇见,反正我遇见了,我不管,我就是要记录下来,谁知道我下次会不会再遇见,毕竟我健忘呀。
问题描述
昨天做项目的时候要实现一个需求的的功能,持久层里要添加查找需求单的详细信息的接口,注意哦,是同时要查找出发单人还有接单人这两个对象的姓名、头像的哦,而发单人这个对象和接单人对象的关系是外键连接。
老规矩,以源码为导向
package ssm.aidai.pojo;
Order 实体类
import java.math.BigDecimal;
import java.util.Date;
public class Order {
private Long orderId;
private BigDecimal orderReward;
private Integer orderReceiveId;
private Integer orderType;
private Integer orderSendId;
private Integer placeId;
private Date orderTime;
private String orderExchnPlace;
private Date orderExpireTime;
private Date orderSendTime;
private BigDecimal orderWyMoney;
private Integer orderStatus;
private Date orderFinishTime;
private String orderContent;
private User receiveUser; //接单人User
private User sendUser;//发单人User
private Place place;
public User getReceiveUser() {
return receiveUser;
}
public void setReceiveUser(User receiveUser) {
this.receiveUser = receiveUser;
}
public User getSendUser() {
return sendUser;
}
public void setSendUser(User sendUser) {
this.sendUser = sendUser;
}
public Place getPlace() {
return place;
}
public void setPlace(Place place) {
this.place = place;
}
public Long getOrderId() {
return orderId;
}
public void setOrderId(Long orderId) {
this.orderId = orderId;
}
public BigDecimal getOrderReward() {
return orderReward;
}
public void setOrderReward(BigDecimal orderReward) {
this.orderReward = orderReward;
}
public Integer getOrderReceiveId() {
return orderReceiveId;
}
public void setOrderReceiveId(Integer orderReceiveId) {
this.orderReceiveId = orderReceiveId;
}
public Integer getOrderType() {
return orderType;
}
public void setOrderType(Integer orderType) {
this.orderType = orderType;
}
public Integer getOrderSendId() {
return orderSendId;
}
public void setOrderSendId(Integer orderSendId) {
this.orderSendId = orderSendId;
}
public Integer getPlaceId() {
return placeId;
}
public void setPlaceId(Integer placeId) {
this.placeId = placeId;
}
public Date getOrderTime() {
return orderTime;
}
public void setOrderTime(Date orderTime) {
this.orderTime = orderTime;
}
public String getOrderExchnPlace() {
return orderExchnPlace;
}
public void setOrderExchnPlace(String orderExchnPlace) {
this.orderExchnPlace = orderExchnPlace == null ? null : orderExchnPlace
.trim();
}
public Date getOrderExpireTime() {
return orderExpireTime;
}
public void setOrderExpireTime(Date orderExpireTime) {
this.orderExpireTime = orderExpireTime;
}
public Date getOrderSendTime() {
return orderSendTime;
}
public void setOrderSendTime(Date orderSendTime) {
this.orderSendTime = orderSendTime;
}
public BigDecimal getOrderWyMoney() {
return orderWyMoney;
}
public void setOrderWyMoney(BigDecimal orderWyMoney) {
this.orderWyMoney = orderWyMoney;
}
public Integer getOrderStatus() {
return orderStatus;
}
public void setOrderStatus(Integer orderStatus) {
this.orderStatus = orderStatus;
}
public Date getOrderFinishTime() {
return orderFinishTime;
}
public void setOrderFinishTime(Date orderFinishTime) {
this.orderFinishTime = orderFinishTime;
}
public String getOrderContent() {
return orderContent;
}
public void setOrderContent(String orderContent) {
this.orderContent = orderContent == null ? null : orderContent.trim();
}
}
OrderMapper关键源码:
<resultMap id="BaseResultMap2" type="ssm.aidai.pojo.Order"
extends="BaseResultMap">
<!-- 接单人身份的receiveUser关联 -->
<association property="receiveUser" javaType="ssm.aidai.pojo.User"
column="order_receive_id" select="getReceiveUser" />
<!-- 发单人身份的sendUser关联 -->
<association property="sendUser" column="order_send_id"
select="getSendUser" />
</resultMap>
<!-- 连接接单人对象的函数 -->
<select id="getReceiveUser" resultMap="UserResultMap"
parameterType="java.lang.Integer">
select
user_id,user_name,user_icon
from aidai_user where
user_id =
#{orderReceiveId,jdbcType=INTEGER}
</select>
<!-- 连接发单人对象的函数 -->
<select id="getSendUser" resultMap="UserResultMap"
parameterType="java.lang.Integer">
select
user_id,user_name,user_icon
from aidai_user where
user_id =
#{orderSendId,jdbcType=INTEGER}
</select>
<!-- 查找订单信息 -->
<select id="selectByPrimaryKey" resultMap="BaseResultMap2"
parameterType="java.lang.Long">
select
<include refid="Base_Column_List" />
,
<include refid="Additional_Column_Place" />
from aidai_order o,
aidai_place p
where order_id =
#{orderId,jdbcType=INTEGER} and p.place_id =
o.place_id
</select>
源码分析:
在Order实体类中嵌套这两个对象,分别是:receiveUser和sendUser,而我们要做的就是将在查询Order这个对象的时候通过orderReceiveId和orderSendId这两个外键查询receiveUser和sendUser这两个对象,那么我是怎么做的呢?
在resultMap中:
<resultMap id="BaseResultMap2" type="ssm.aidai.pojo.Order" extends="BaseResultMap">
<!-- 接单人身份的receiveUser关联 -->
<association property="receiveUser" javaType="ssm.aidai.pojo.User" column="order_receive_id" select="getReceiveUser" />
<!-- 发单人身份的sendUser关联 -->
<association property="sendUser" column="order_send_id" javaType="ssm.aidai.pojo.User" select="getSendUser" />
</resultMap>
根据应用场景进行元素分析:
- association: 复杂对象的映射
- property: 映射到Order实体类中的receiveUser
- column : 这个其实就是你要传的参数名
- javaType: 一个 Java 类的完全限定名
整个查询操作流程是这样的:
1、数据库依据下面的代码查找Order对象,然后将对象映射到BaseResultMap2结果集中;
<select id="selectByPrimaryKey" resultMap="BaseResultMap2"
parameterType="java.lang.Long">
select
<include refid="Base_Column_List" />
,
<include refid="Additional_Column_Place" />
from aidai_order o,
aidai_place p
where order_id =
#{orderId,jdbcType=INTEGER} and p.place_id =
o.place_id
</select>
2、而在BaseResultMap2中有以下association复杂对象的映射,即
<association property="receiveUser" javaType="ssm.aidai.pojo.User" column="order_receive_id" select="getReceiveUser" />
所以系统会通过select的属性值即getReceiveUser跳转到id="getReceiveUser"的xml中,并将column中的order_receive_id的映射字段中的值传递过去即
<select id="getReceiveUser" resultMap="UserResultMap"
parameterType="java.lang.Integer">
select
user_id,user_name,user_icon
from aidai_user where
user_id =
#{orderReceiveId,jdbcType=INTEGER}
</select>
那么为什么是#{orderReceiveId,jdbcType=INTEGER}呢,因为orderReceiveId是order_receive_id在实体类Order中的映射字段,可以装载值。
在查询到对象之后会将对象放入receiveUser。
sendUser的操作是一样的,就不解释了!!!
Note:发布的这些文章全都是自己边学边总结的,难免有纰漏,如果发现有不足的地方,希望可以指出来,一起学习咯,么么哒。
开源爱好者,相信开源的力量必将改变世界:
** osc :** https://git.oschina.net/xi_fan
github: https://github.com/wiatingpub