@Insert、@Update批量新增、更新数据

  • Post author:
  • Post category:其他


MyBatis用注解实现批量新增和更新

1.@Insert批量新增

@Insert({"<script>" +
            "INSERT INTO  data_info (" +
            "id,\n" +
            "operation,\n" +
            "record_time)" +
            "values  " +
            "<foreach collection=\"dataInfoList\" item=\"item\" separator=\",\">" +
            "(#{item.id}, #{item.operation}, " +
            "#{item.recordTime})" +
            "</foreach>" +
            "</script>"})
    void batchInsert(@Param("dataInfoList") List<DataInfo> dataInfoList);

2.@Update批量更新

@Update({"<script>" +
            "<foreach collection=\"dataInfoList\" item=\"item\" separator=\";\">" +
            " UPDATE" +
            " data_info" +
            " SET operation = #{item.operation, jdbcType=INTEGER}, " +
            "  record_time = #{item.recordTime, jdbcType=TIMESTAMP} " +
            "  WHERE " +
            "   id = #{item.id, jdbcType=INTEGER} " +
            "</foreach>" +
            "</script>"})
    void batchUpdate(@Param("dataInfoList") List<DataInfo> dataInfoList);



版权声明:本文为MTonj原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。