mybatis的collection的用法,select标签用法,column传值

第一种用法,一个sql查询出所有需要的数据。

<resultMap type="A类路径" id="item">

<id column="id" property="id"/>

<result column="value" property="value"/>

<collection column="id" property="与A类路径字段名一致" ofType="B类路径">

<result column="id" property="b_id"/>

<result column="value" property="b_value"/>
</collection>
</resultMap>

注意:

1.如果字段名一致,需要取别名作为区分,比如上面的b_id和b_value。

2.property="与A类路径字段名一致"    ----对应的是A类路径的字段名称,为了一一对应。

3.如果关联的表很多,此种方法速度会很慢。

第二中用法,多个sql查询出需要的数据。主要用于关联多表查询,提升查询速度

<resultMap type="A类路径" id="itemNew">

<id column="id" property="a_id"/>

<result column="value" property="a_value"/>


<collection column="{aId=a_id}" property="与A类字段名一致" ofType="B类路径" select="getList"> </collection>
</resultMap>

<select id="getListNew(与dao中方法名保持一致)" parameterType="类路径" resultMap="itemNew">
select a.id a_id ,a.value a_value from table_a a 
</select>


<resultMap type="类路径" id="itemList">

<result column="id" property="b_id"/>

<result column="value" property="b_value"/>

</resultMap>

<select id="getList(与上面select标签值保持一致)" resultMap="itemList"> 
select b.id b_id , b.value b_value from table_b b where b.aid = #{aId}
</select>

说明:

1.column="{aId=a_id}"   这个字段用于传值给子collection,如果需要传多个参数,用逗号隔开,eg;column="{aId=a_id,e=e}"

2.如果有问题,请留言,看到会回复

 

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值