在mybatis配置文件中的insert语句如下时
<insert id="insert" parameterType="TBookmark" >
insert into t_bookmark (BOOKMARK_ID, BOOKMARK_NAME, USER_ID,
CONTENT_ID, CHAPTER_ID, CHAPTER_NAME,
SECTION, PAGE, POSITION,
BOOKMARK_TYPE, BOOKMARK_CREATE_TIME, CONTENT_NAME,
SERIAL_NUM, BUSINESS_SORT_ID, BSSORT_NAME,BOOKMARK_SYSTEM_TIME
)
values (#{bookmarkId,jdbcType=DECIMAL}, #{bookmarkName,jdbcType=VARCHAR}, #{userId,jdbcType=DECIMAL},
#{contentId,jdbcType=DECIMAL}, #{chapterId,jdbcType=DECIMAL}, #{chapterName,jdbcType=VARCHAR},
#{section,jdbcType=DECIMAL}, #{page,jdbcType=DECIMAL}, #{position,jdbcType=DECIMAL},
#{bookmarkType,jdbcType=VARCHAR}, #{bookmarkCreateTime,jdbcType=TIMESTAMP}, #{contentName,jdbcType=VARCHAR},
#{serialNum,jdbcType=DECIMAL}, #{businessSortId,jdbcType=DECIMAL}, #{bssortName,jdbcType=VARCHAR},#{bookmarkSystemTime,jdbcType=TIMESTAMP}
)
</insert>
useGeneratedKeys的属性默认为true,也就是说主键用的是自动生成,这是当传进来的实体中TBookmark已设置了主键时,程序会报java.lang.ArrayIndexOutOfBoundsException,因为实体中有了主键,mybatis又自动帮我们生成了一个主键,属性值多出一个了,只要设置useGeneratedKeys属性为false,就用到了实体传进来的主键值了,如下
<insert id="insert" parameterType="TBookmark" useGeneratedKeys="false">
insert into t_bookmark (BOOKMARK_ID, BOOKMARK_NAME, USER_ID,
CONTENT_ID, CHAPTER_ID, CHAPTER_NAME,
SECTION, PAGE, POSITION,
BOOKMARK_TYPE, BOOKMARK_CREATE_TIME, CONTENT_NAME,
SERIAL_NUM, BUSINESS_SORT_ID, BSSORT_NAME,BOOKMARK_SYSTEM_TIME
)
values (#{bookmarkId,jdbcType=DECIMAL}, #{bookmarkName,jdbcType=VARCHAR}, #{userId,jdbcType=DECIMAL},
#{contentId,jdbcType=DECIMAL}, #{chapterId,jdbcType=DECIMAL}, #{chapterName,jdbcType=VARCHAR},
#{section,jdbcType=DECIMAL}, #{page,jdbcType=DECIMAL}, #{position,jdbcType=DECIMAL},
#{bookmarkType,jdbcType=VARCHAR}, #{bookmarkCreateTime,jdbcType=TIMESTAMP}, #{contentName,jdbcType=VARCHAR},
#{serialNum,jdbcType=DECIMAL}, #{businessSortId,jdbcType=DECIMAL}, #{bssortName,jdbcType=VARCHAR},#{bookmarkSystemTime,jdbcType=TIMESTAMP}
)
</insert>