Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I want to insert batch db records into mysql, so I write mybatis mapping xml using foreach and choose items.

However, it gives me NoSuchPropertyException when I run the test function. The mapping xml code is like this:

<insert id="insertIntervalConfig" parameterType="java.util.List" useGeneratedKeys="true"  keyProperty="id">
    INSERT INTO
        <include refid="tableName"></include>
        <include refid="insertFields"></include>
    VALUES
        <foreach collection="list" item="item" index="index" separator=",">
                <choose><when test="item.appId == null">0</when><otherwise>#{item.appId, jdbcType=INTEGER}</otherwise></choose>,
                <choose><when test="item.stageId == null">0</when><otherwise>#{item.stageId, jdbcType=INTEGER}</otherwise></choose>,
                <choose><when test="item.intervalType == null">0</when><otherwise>#{item.intervalType, jdbcType=INTEGER}</otherwise></choose>,
                <choose><when test="item.accountTypeName == null">''</when><otherwise>#{item.accountTypeName, jdbcType=VARCHAR}</otherwise></choose>,
                <choose><when test="item.accountTypeCode == null">''</when><otherwise>#{item.accountTypeCode, jdbcType=VARCHAR}</otherwise></choose>,
                #{item.accountMin, jdbcType=INTEGER}, #{item.accountMax, jdbcType=INTEGER},
                <choose><when test="item.displayName== null">''</when><otherwise>#{item.displayName, jdbcType=VARCHAR}</otherwise></choose>,
                <choose><when test="item.sequenceId == null">0</when><otherwise>#{item.sequenceId, jdbcType=INTEGER}</otherwise></choose>
        </foreach>
</insert>

and my test function is like this

List<IntervalConfigDO> configList = new ArrayList<IntervalConfigDO>();
configList.add(config1);
configList.add(config2);
intervalConfigDao.insertIntervalConfig(configList);

the detail exception is:

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.builder.BuilderException: Error evaluating expression 'item.appId == null'. Cause: org.apache.ibatis.ognl.NoSuchPropertyException

I find that it works when i remove the choose item, but I don't know why, could someone give some tips?

this problem finally be dealt with adding @Data annotation to the object class (IntervalConfigDO) – ting luo Feb 2, 2021 at 11:36 Consider answering your own question, it is not only allowed but encouraged because it will help others – Cleptus Feb 2, 2021 at 13:47

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.