mybatis组合实体查询

本文详细介绍了MyBatis中使用resultMap进行复杂实体映射的方法,特别是如何通过association标签实现二次查询,以增强数据的关联性和丰富性。同时,记录了在大量使用collection标签时可能遇到的JDK限制问题。

mybatis组合实体查询

mybatis组合实体查询

<resultMap id="wxIndexMap" type="com.o2o.common.model.wxuntity.WxIndexNewMatter">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="type" jdbcType="INTEGER" property="type" />
<association property="detail_img" column="{orid=id,modulartype=type}" select="selectDetail_img"></association>
<association property="likenumber" column="{orid=id,modulartype=type}" select="com.o2o.common.dao.wx.CM_Com_LikeMapper.selectLikenumber"></association>
</resultMap>

通过这个select可以进行二次查询

问题记录:

如果采用的是collection标签,一次使用太多,jdk不会让进行运行的,

posted @ 2019-02-03 11:13 动手的程序员 阅读( ...) 评论( ...) 编辑 收藏
MyBatis组合查询是指在一个SQL语句中同时查询多个表的数据,可以使用嵌套查询实现。具体步骤如下: 1. 配置多个表的映射文件,分别对应不同的实体类。 2. 在主映射文件中使用`<select>`标签定义SQL语句,使用`<resultMap>`标签定义结果集映射关系。 3. 在`<select>`标签中使用`<include>`标签引入其他映射文件中的SQL语句。 4. 在`<resultMap>`标签中使用`<association>`标签或`<collection>`标签定义关联关系。 下面是一个示例: 引用:假设有两个表,一个是用户表,一个是订单表,一个用户可以有多个订单,订单表中有一个`user_id`字段与用户表中的`id`字段关联。我们可以使用如下的SQL语句实现组合查询: ```xml <select id="getUserWithOrders" resultMap="userWithOrders"> SELECT * FROM user WHERE id = #{id} </select> <resultMap id="userWithOrders" type="User"> <id property="id" column="id"/> <result property="username" column="username"/> <result property="password" column="password"/> <collection property="orders" ofType="Order"> <id property="id" column="id"/> <result property="orderNo" column="order_no"/> <result property="createTime" column="create_time"/> <result property="userId" column="user_id"/> </collection> </resultMap> ``` 在上面的示例中,我们定义了一个`getUserWithOrders`的SQL语句,使用`<include>`标签引入了订单表的查询语句。在`<resultMap>`标签中,我们使用了`<collection>`标签定义了用户与订单之间的一对多关系。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值