Mybatis手冊——學習筆記數據庫
Mybatis自動生成主鍵,mapper文件配置app
1.數據庫支持自動生成主鍵字段dom
你能夠設置 useGeneratedKeys=」true」,並且設置 keyProperty 到你已經作好的目標屬性上。 例如,若是上面的 Author 表已經對 id 使用了自動生成的列類型,那麼語句能夠修改成:學習
<insert id="insertAuthor" parameterType="domain.blog.Author" useGeneratedKeys="true" keyProperty="id"> insert into Author (username,password,email,bio) values (#{username},#{password},#{email},#{bio}) </insert>
2.數據庫不支持自動生成主鍵字段code
<insert id="insertAuthor" parameterType="domain.blog.Author"> <selectKey keyProperty="id" resultType="int" order="BEFORE"> select CAST(RANDOM()*1000000 as INTEGER) a from SYSIBM.SYSDUMMY1 </selectKey> insert into Author (id, username, password, email,bio, favourite_section) values (#{id}, #{username}, #{password}, #{email}, #{bio}, #{favouriteSection,jdbcType=VARCHAR}) </insert>