使用 MyBatis 获取地图<Long, List<Object>>
Getting Map<Long, List<Object>> using MyBatis
这是我的方法,我需要得到一个Map<Long, List<CMBill>>
:
@Mapper
public interface CMBillMapper {
@MapKey("groupId")
Map<Long, List<CMBill>> getCMInvoicesByIdListSQL(@Param("invoiceIds") Set<Long> invoiceIds);
}
这里是MyBatis xml config,配置了这张图
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="ru.example.CMBillMapper">
<resultMap id="CMBillResult" type="CMBill">
<result property="id" column="id"/>
<result property="groupId" column="group_id"/>
<result property="clientId" column="client_id"/>
<result property="paidAmount" column="paid_amount"/>
<result property="amount" column="amount"/>
<result property="currency" column="currency"/>
<result property="status" column="status"/>
</resultMap>
<select id="getCMInvoicesByIdListSQL" resultMap="CMBillResult">
select it.group_id as group_id,
it.id as id,
it.client_id as receiver_id,
it.status as invoice_status,
it.amount as amount,
it.currency as currency,
it.paid_amount as paid_amount,
if.type as if_type,
if.code as if_code,
if.val as if_val
from cm_invoice_tab it
left join cm_inv_group_field_tab if
on if.group_id = it.group_id
where it.group_id in (#{invoiceIds})
</select>
</mapper>
但是mapper的结果是Map<Long, CMBill>
,我需要另一个。如何重写我的映射器以获得我需要的结果?
这种情况建议直接查询获取List,获取结果集后再对group_id进行分组
这是我的方法,我需要得到一个Map<Long, List<CMBill>>
:
@Mapper
public interface CMBillMapper {
@MapKey("groupId")
Map<Long, List<CMBill>> getCMInvoicesByIdListSQL(@Param("invoiceIds") Set<Long> invoiceIds);
}
这里是MyBatis xml config,配置了这张图
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="ru.example.CMBillMapper">
<resultMap id="CMBillResult" type="CMBill">
<result property="id" column="id"/>
<result property="groupId" column="group_id"/>
<result property="clientId" column="client_id"/>
<result property="paidAmount" column="paid_amount"/>
<result property="amount" column="amount"/>
<result property="currency" column="currency"/>
<result property="status" column="status"/>
</resultMap>
<select id="getCMInvoicesByIdListSQL" resultMap="CMBillResult">
select it.group_id as group_id,
it.id as id,
it.client_id as receiver_id,
it.status as invoice_status,
it.amount as amount,
it.currency as currency,
it.paid_amount as paid_amount,
if.type as if_type,
if.code as if_code,
if.val as if_val
from cm_invoice_tab it
left join cm_inv_group_field_tab if
on if.group_id = it.group_id
where it.group_id in (#{invoiceIds})
</select>
</mapper>
但是mapper的结果是Map<Long, CMBill>
,我需要另一个。如何重写我的映射器以获得我需要的结果?
这种情况建议直接查询获取List,获取结果集后再对group_id进行分组