今天遇到遇到一个myabitis 批量Insert时报出的
Parameter ‘promotionActivityRuleList’ not found. Available parameters are [collection, list]
org.apache.ibatis.binding.BindingException: Parameter 'List' not found. Available parameters are [collection, list]
测试代码:
@Test
public void testAddRule(){
RulePlatformDto rulePlatformDto = new RulePlatformDto();
rulePlatformDto.setActivityId(2);
rulePlatformDto.setActivityType(1);
rulePlatformDto.setResidueStock(100);
rulePlatformDto.setExtendJson("{'name':'android ios java'}");
rulePlatformDto.setC_t(2323113);
rulePlatformDto.setU_t(123231);
rulePlatformDto.setCreateId(23);
rulePlatformDto.setCreateName("21");
rulePlatformDto.setUpdateId(12);
rulePlatformDto.setUpdateName("meicai");
rulePlatformDto.setC_t(231);
rulePlatformDto.setU_t(213);
rulePlatformDto.setCreateId(777);
rulePlatformDto.setIs_deleted(0);
List<ActivityRulePlatformDto> list = new ArrayList<>();
list.add(rulePlatformDto);
BaseResultPlatformDto<Boolean> booleanBaseResultPlatformDto= rulePlatformApi.addPromotionActivityRule(list);
LogUtils.e(booleanBaseResultPlatformDto.toString());
}
Dao 文件:
int batchInsert(List<PromotionRule> promotionRuleList);
Mapper文件:
<insert id="batchInsert" parameterType="java.util.List">
INSERT INTO t_promotion_rule
(id, f_activity_id, activity_type, residue_stock, extend_json, create_id, create_name, update_id, update_name, c_t, u_t, is_deleted)
VALUES
<foreach collection="promotionRuleList" item="item" index="index" separator="union all">
(#{item.id},
#{item.factivityId},
#{item.activityType},
#{item.residueStock},
#{item.extendJson},
#{item.createId},
#{item.createName},
#{item.updateId},
#{item.updateName},
UNIX_TIMESTAMP(),
UNIX_TIMESTAMP(),
0)
</foreach>
</insert>
mybatis 文档描述的是List 将会被转为Map, 这里我转入collection=”promotionRuleList” 将被转换成
Map<”list”: promotionRuleList>
http://www.mybatis.org/mybatis-3/zh/sqlmap-xml.html#Parameters
收集了两种解决方案:
方案一:
collection="promotionRuleList" 改写为 collection="list"
方案二:
在Dao层将参数封装到Map数据结构中
在mapper.xml 使collection="map"