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>