咱们向数据库插入一条记录时,使用Mybatis的<insert>是没法返回插入的主键的,但咱们须要这个刚插入的主键,可使用以下方式返回:java
result返回的是插入成功条数,而主键id返回到CourseInfo的id属性中mysql
<insert id="insert" parameterType="org.andy.shop.model.CourseInfo" > insert into course_info (id, cname, caddress) values (#{id,jdbcType=INTEGER}, #{cname,jdbcType=VARCHAR}, #{caddress,jdbcType=VARCHAR}) <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer"> select last_insert_id() </selectKey> </insert>
或者:sql
<insert id="insert" parameterType="org.andy.shop.model.CourseInfo" useGeneratedKeys="true" keyProperty="id"> insert into course_info (id, cname, caddress ) values (#{id,jdbcType=INTEGER}, #{cname,jdbcType=VARCHAR}, #{caddress,jdbcType=VARCHAR} ) </insert>
<insert id="insert" parameterType="org.andy.shop.model.CourseInfo" > <selectKey keyProperty="id" order="BEFORE" resultType="java.lang.String"> select uuid() </selectKey> insert into course_info (id, cname, caddress) values (#{id,jdbcType=VARCHAR}, #{cname,jdbcType=VARCHAR}, #{caddress,jdbcType=VARCHAR}) </insert>