MyBatis中<resultMap>的<association>和<collection>标签的使用

 MyBatis中<resultMap>的<association>和<collection>标签的使用

之前就用到了很多次,因为公司里架构设计表结构设计得很精细,一张表的映射注入往往不够,因此使用该标签来进行其他表字段的查询。下面是我的实体类,以及mapper.xml文件。

    1.实体类

package com.apin.modules.order.bean;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import org.json.JSONObject;
/**
 * @author wch
 * @date 2017年4月17日 
 */
public class ApinRoute implements Serializable{

private static final long serialVersionUID = 8605840659214346403L;
private  String id;//id
private  int adult_number;//成人人数
private  int child_number;//儿童人数
private  Date expire_time;//拼团关闭日期
private  String remark;//备注  
private  Byte route_type;//行程类型 1:单程2:往返3:多程  
private  Byte route_days;//往返天数  
private  Date create_time;//创建时间  
private  Byte route_status;//行程状态1:待出价2:已出价3:待付定金4:拼团待确认5:待付尾款6:待出票7:已出票8:已失效  
private  String application_id;//应用id 
private  String channel_id;//渠道id 
private  Byte is_by_hand;//是否是手工行程   0:不是1:是  
private  String user_id;//账号id
private  String phone;  
private  List<ApinRouteDetail> routeDetailList;  
}

比如说类中的phone,这个手机号是账号表中的一个字段,我现在写在这里;routeDetailList是我新建的一个list,泛型是另一个实体类,查询出来后可以直接存在这个list中;

2.mappe.XMl文件相关的代码段:

<resultMap id="result" type="com.apin.modules.order.bean.ApinRoute">
<id column="id" property="id" jdbcType="VARCHAR" />
<result column="adult_number" property="adult_number" jdbcType="INTEGER" />
<result column="child_number" property="child_number" jdbcType="INTEGER" />
<result column="remark" property="remark" jdbcType="VARCHAR" />
<result column="route_type" property="route_type" jdbcType="TINYINT" />
<result column="route_days" property="route_days" jdbcType="TINYINT" />
<result column="expire_time" property="expire_time" jdbcType="TIMESTAMP" />
<result column="create_time" property="create_time" jdbcType="TIMESTAMP" />
<result column="route_status" property="route_status" jdbcType="TINYINT" />
<result column="is_by_hand" property="is_by_hand" jdbcType="TINYINT" />
<result column="user_id" property="user_id" jdbcType="VARCHAR" />
<association column="user_id" property="phone" select="getphone" fetchType="eager"/>
<collection column="id" property="routeDetailList" javaType="ArrayList"

ofType="com.apin.modules.order.bean.ApinRouteDetail" fetchType="eager" select="selectRouteDetail"/>
</resultMap>

其中红色标签就是对此的使用,下面是使用到的查询详细代码段:

<select id="getphone" resultType="String">(resultType就是返回实体类中的类型)
select phone from apin_account  where id=#{user_id}
</select>
<select id="selectRouteDetail" resultType="com.apin.modules.order.bean.ApinRouteDetail" parameterType="String">

-----------------------------------------------------------------------------------parameterType是传过来的参数类型
select * from  apin_route_detail_info   where route_no=#{id}
</select>
<select id="searchOrder" resultMap="result">
select
apin_route.* 
from
apin_route,apin_plan_info
where apin_plan_info.plan_id=#{plan_id} and apin_plan_info.route_no=apin_route.id
<if test="map != null and map.size != 0">
order by
<foreach collection="map.keys" item="key" separator=",">
${key} ${map[key]}
</foreach>
</if>
</select>

到此为止,详细使用就写完了。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值