咱們向數據庫插入一條記錄時,使用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>