兩種方法獲取MyBatis剛剛插入的id


主要就是在xml文件中的寫法,其他省略

方法一:

<insert id="insert" parameterType="com.xxx.xxxx.pojo.User">
        insert into t_user (name) 
        values (#{user.name})
        <selectKey resultType="Integer" order="AFTER" keyProperty="user.userId">
            SELECT LAST_INSERT_ID() AS userId
        </selectKey>
</insert>

方法二:

<insert id="insert" parameterType="Spares"     
        useGeneratedKeys="true" keyProperty="id">    
        insert into system(name) values(#{name})    
</insert>

然后在調用的時候就可以用對象的get屬性方法來獲取了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM