会员
周边
众包
新闻
博问
闪存
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
huaobin
博客园
首页
新随笔
联系
订阅
管理
嵌套映射
当关联查询非常复杂时,可以用嵌套的select,其原理是在映射复杂数据时执行另一个select来完成
<
resultMap
id=
"order_resultMap2"
type=
"Order"
autoMapping=
"true">
<
id
column=
"id"
property=
"id"/>
<!-- 指定嵌套查询 column是传给内层查询的参数 -->
<
association
property=
"user"
column=
"user_id"
select=
"selectUserByUid"
javaType=
"user"/>
</
resultMap>
<!-- 外层查询-->
<
select
id=
"selectOrderByID2"
parameterType=
"int"
resultMap=
"order_resultMap2"> select * from orders where id = #{id}
</
select>
<!-- 嵌套查询-->
<
select
id=
"selectUserByUid"
parameterType=
"int"
resultType=
"user"> select *from kuser where id = #{id}
</
select>
这种方式同样适用于一对多的关联关系
<!--自定义映射-->
<
resultMap
id=
"user_resultMap2"
type=
"user"
autoMapping=
"true">
<
result
column=
"username"
property=
"name"/>
<
collection
property=
"orders"
ofType=
"order"
select=
"selectOrderByUserID"
column=
"id">
<
id
column=
"id"
property=
"id"/>
</
collection>
</
resultMap>
<
select
id=
"selectUserByID2"
parameterType=
"int"
resultMap=
"user_resultMap2"> select * from kuser where id = #{uid}
</
select>
<
select
id=
"selectOrderByUserID"
resultType=
"order"
parameterType=
"int"> select *from orders where user_id = #{uid}
</
select>
1
|
0
posted @
2021-11-17 10:15
青竹之下
阅读(
110
) 评论(
0
)
编辑
收藏
举报
会员力量,点亮园子希望
刷新页面
返回顶部
公告