MyBatis 3 Annotation Example with @Select, @Insert, @Update and @Delete
Script writing
<insert id="insertBatch" parameterType="java.util.List">
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
SELECT LAST_INSERT_ID()
</selectKey>
insert into partner_channel_record ( partner_id,
uvui, gmt_timestamp, os
)
values
<foreach collection="list" item="item" index="index" open="" separator="," close="">
( #{item.partnerId,jdbcType=INTEGER},
#{item.uvui,jdbcType=VARCHAR}, #{item.gmtTimestamp,jdbcType=BIGINT}, #{item.os,jdbcType=INTEGER}
)
</foreach>
on duplicate key update gmt_timestamp=values(gmt_timestamp)
</insert>
脚本太复杂了,不如用xml文件写
public interface TestMapper {
...
@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
@Insert({
"<script>",
"INSERT INTO test_table",
"(column_one, column_two)",
"VALUES" +
"<foreach item='item' collection='list' open='' separator=',' close=''>" +
"(" +
"#{item.columnOne,jdbcType=VARCHAR},",
"#{item.columnTwo,jdbcType=VARCHAR}" +
")" +
"</foreach>",
"</script>"})
void insertBatchTestTable(@Param("list") List<TestObject> testObjs);
}