pageHelper 插件一对多查询分页问题

1.首先先确定我们需要返回的数据数据结构,这里我的贴出实体类(set/get方法自己生成)

public class BillInfoAndStudentInfoBean {
private String id;
private String billId;
private BigDecimal moneyTotal;
private List<ItemsBean> items;
}

2.然后我们在mapper.xml建立对应的关系,要实现分页正确我们需要建立两个 resultMap,一个用于子查询用
注意,在主查询中的 collection里面要配置子查询sql查询id的方法名要对应,里面的 column 就是子查询你需要的查询条件如果子查询需要多个条件column就这样写 column={id = billId,money = moneyTotal}
然后去子查询里面取值就好

主查询 resultMap
<resultMap id="BillInfoAndItemsInfo" type="com.lxd.domain.param.BillInfoAndStudentInfoBean">
<result column="id" property="id"></result>
<result column="billId" property="billId"></result>
<result column="moneyTotal" property="moneyTotal"></result>
<collection property="items" javaType="java.util.List" ofType="com.lxd.domain.param.ItemsBean" select="queryItemInfoById" column="billId">
</collection>
</resultMap>


子查询 resultMap

<resultMap id="ItemBeans" type="com.lxd.domain.param.ItemsBean">
<result column="item_name" property="item_name"></result>
<result column="item_price" property="item_price"></result>
<result column="is_sure" property="item_mandatory"></result>
</resultMap>

3.接下来可以开始写sql了
<!-- 子查询sql -->,注意这里的 方法名要和主查询 resultMap collection 里面 select 的方法名对应起来,这里传入的参数就是, collection 里面的 column
<单个参数>
<select id="queryItemInfoById" resultMap="ItemBeans">
SELECT ati.item_name,ati.item_price,ati.is_sure, atd.id FROM app_tuition_data atd
left JOIN app_tuition_item ati on atd.id=ati.tuition_id WHERE atd.id = #{billId}
</select>
 
 
<多个参数>
<select id="queryItemInfoById" resultMap="ItemBeans">
SELECT ati.item_name,ati.item_price,ati.is_sure, atd.id FROM app_tuition_data atd
left JOIN app_tuition_item ati on atd.id=ati.tuition_id WHERE atd.id = #{id} and #{money}
</select>


4.这样就好了,主查询sql 和原来一样就好,把之前子查询所关联的删除就好



转载于:https://www.cnblogs.com/bt2882/p/11088738.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值