Detail MyBatis batch insert data Mapper configuration file writing

  • 2020-06-23 00:27:13
  • OfStack

The use of the MyBatis configuration file 1 is not familiar, as it was previously developed using annotations, but the downside of annotations is that they can be cumbersome to modify in code if the database table structure changes.

In fact, batch insert is very simple, here are some brief explanation. Please see how to write the configuration file:


<insert id="insertAll" parameterType="java.util.List" useGeneratedKeys="true">
    <selectKey resultType="long" keyProperty="id" order="AFTER">
      SELECT
      LAST_INSERT_ID()
    </selectKey>
    insert into
    workflow_info(belong_program,workflow_comment,schedule_id,job_id,parent_job_id,job_level,exec_time,created_user)
    values
    <foreach collection="list" item="item" index="index" separator=",">

      (#{item.belongProgram},#{item.workFlowComment},#{item.scheduleId},#{item.jobId},#{item.parentJobId},#{item.jobLevel},#{item.execTime},#{item.createdUser})

    </foreach>

  </insert>

< selectKey > The role of the tag is set to automatically generate the primary key of the database entities to our class, including order = "AFTER" is set for the operating table name is set up after the insert execution, because some database does not support the function of the primary key from the growth, there will be "order =" BEFORE all "" usage, before the insert for you generated by MyBatis 1 only 1 ID, specific usage can refer to this article: http: / / www. mybatis. org/mybatis - 3 / zh/sqlmap - xml. html


Related articles: