mybatis namespace与association心得笔记

工作中有个sku模块与其他9个业务模块有关联,需要查询时同时查出其余模块的信息。


按照以前的笨方法,肯定是把其他9个模块的resultMap全部copy到sku模块里来,再copy所有的查询语句。拼成一条sql进行查询。

或者在service层注入其余9个模块的dao,在查出sku信息后,再一条一条进行查询。

这两个方法都很笨很繁琐。


然后查了下mybatis的文档,resultMap中有个association可以处理关联关系。即我们可以通过resultMap配置其余9个模块的映射,省去查询SQL的处理。

但是,还是要copy其余模块的resultMap跟查询SQL,还是觉得很麻烦!


有没有更简单的方法呢?


有的!


我们可以直接在association的select属性中添加其他模块的sql语句。只需要在sql语句前加上其namespace,即可直接调用。还copy个屁代码啊,太方便了!


以下是例子,注意看sku.xml中resultMap直接调用baseControl.xml中的查询语句:

sku.xml

<mapper namespace="com.geekplus.beetle.dao.base.SkuManageDao">
	<resultMap id="skuMap" type="BaseSku">
		<result column="pk_base_sku_id" property="skuId" jdbcType="VARCHAR" />
		<result column="sku_code" property="skuCode" jdbcType="VARCHAR" />
		<result column="customer_code" property="customerCode" jdbcType="VARCHAR" />
		<result column="batch_property_code" property="batchPropertyCode" jdbcType="VARCHAR" />
		<result column="wares_type_id_1" property="waresTypeId1" jdbcType="VARCHAR" />
		<result column="wares_type_id_2" property="waresTypeId2" jdbcType="VARCHAR" />
		<result column="wares_type_id_3" property="waresTypeId3" jdbcType="VARCHAR" />
		
		<result column="spec_class" property="specClass" jdbcType="VARCHAR" />
		<result column="sku_name" property="skuName" jdbcType="VARCHAR" />
		<result column="sku_brand" property="skuBrand" jdbcType="VARCHAR" />
		<result column="sku_price" property="skuPrice" jdbcType="DECIMAL" />
		<result column="fresh_day" property="freshDay" jdbcType="INTEGER" />
		<result column="length" property="length" jdbcType="INTEGER" />
		<result column="width" property="width" jdbcType="INTEGER" />
		<result column="height" property="height" jdbcType="INTEGER" />
		<result column="volume" property="volume" jdbcType="BIGINT" />
		<result column="gross_weight" property="grossWeight" jdbcType="INTEGER" />
		<result column="net_weight" property="netWeight" jdbcType="INTEGER" />
		<result column="sku_remarks" property="skuRemarks" jdbcType="VARCHAR" />

		<result column="add_person" property="createBy" jdbcType="VARCHAR" />
		<result column="add_time" property="createDate" jdbcType="TIMESTAMP" />
		<result column="edit_person" property="lastUpdateBy" jdbcType="VARCHAR" />
		<result column="edit_time" property="lastUpdateDate" jdbcType="TIMESTAMP" />
		<result column="status" property="status" jdbcType="INTEGER" />
		
		<result column="skuControl.baseTableType" property="skuControl.baseTableType" jdbcType="INTEGER"/>
		<association property="skuControl" column="{vo.baseTablePk=pk_base_sku_id,vo.baseTableType=skuControl.baseTableType}"
			javaType="com.geekplus.beetle.base.entity.BaseControlEntity" select="com.geekplus.beetle.dao.base.BaseControlDao.query"/>
	</resultMap>


baseControl.xml

<mapper namespace="com.geekplus.beetle.dao.base.BaseControlDao" >
	<resultMap id="BaseResultMap" type="com.geekplus.beetle.base.entity.BaseControlEntity" >
	    <id column="pk_base_rule_relationship_id" property="relationshipId" jdbcType="BIGINT" />
	    <result column="base_table_pk" property="baseTablePk" jdbcType="VARCHAR" />
	    <result column="base_table_type" property="baseTableType" jdbcType="INTEGER" />
	    
	    <result column="pack_code" property="packCode" jdbcType="VARCHAR" />
	    <result column="inventory_turn_code" property="inventoryTurnCode" jdbcType="VARCHAR" />
	    <result column="onshelf_code" property="onshelfCode" jdbcType="VARCHAR" />
	    <result column="allocation_code" property="allocationCode" jdbcType="VARCHAR" />
	    <result column="replenishment_code" property="replenishmentCode" jdbcType="VARCHAR" />
	    <result column="quality_control_code" property="qualityControlCode" jdbcType="VARCHAR" />
	    <result column="bin_appoint_type" property="binAppointType" jdbcType="INTEGER" />
	    <result column="batch_property_code" property="batchPropertyCode" jdbcType="VARCHAR" />
	    <result column="over_receive" property="overReceive" jdbcType="TINYINT" />
	    <result column="percent" property="percent" jdbcType="DECIMAL" />
	    
	    <result column="add_person" property="createBy" jdbcType="VARCHAR" />
		<result column="add_time" property="createDate" jdbcType="TIMESTAMP" />
		<result column="edit_person" property="lastUpdateBy" jdbcType="VARCHAR" />
		<result column="edit_time" property="lastUpdateDate" jdbcType="TIMESTAMP" />
		<result column="status" property="status" jdbcType="INTEGER" />
    </resultMap>
<select id="query" resultMap="BaseResultMap"  >
    <include refid="sql_base_rule_relationship_query" />
    <include refid="sql_base_rule_relationship_query_condition" />
  </select>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值