天天看點

mybatis 插入時傳回主鍵

<mapper namespace="com.example.wjtweb.mapper.SelectKeyMapper">
   
    <insert id="insert" parameterType="com.example.wjtweb.pojo.Goods">
        // keyProperty:實體類中的屬性名
        // order = "AFTER" 表示 SELECT LASTINSERTID() 在insert執行之後執行,多用與自增主鍵,
        // resultType 主鍵的資料類型
        // order = "BEFORE"表示SELECT LASTINSERTID() 在insert執行之前執行,這樣的話就拿不到主鍵了,這種适合那種主鍵不是自增的
        <selectKey keyProperty="id" order="AFTER" resultType="Integer">
            SELECT LAST_INSERT_ID()
        </selectKey>
        
        INSERT INTO Goods (MID,GNAME,PRICE,AMOUNT,imageName)
        VALUES (#{mid},#{gname},#{price},#{amount},#{imageName});
    </insert>
 
</mapper>