1. 程式人生 > >mybatis. Parameter 'xxxList' not found. Available parameters are [c

mybatis. Parameter 'xxxList' not found. Available parameters are [c

今天遇到遇到一個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"