zoukankan      html  css  js  c++  java
  • mybatis中Insert后主键返回

    转载:https://www.cnblogs.com/Lyn4ever/p/11390497.html

    1.Mapper的写法,返回的这个int是受影响的行号

    int insertNewUser(User newUser);
    

    2.xml的写法

    	<!--返回主键 形式1 -->
        <insert id="saveReturnPK1" parameterType="cn.lyn4ever.bean.User" useGeneratedKeys="true" keyProperty="id">
            INSERT INTO `test`.`tb_user`(`username`, age) VALUES(#{username}, #{age})
        </insert>
    
        	<!-- 返回主键 形式2 -->
        <insert id="saveReturnPK2" parameterType="cn.lyn4ever.bean.User">
            <selectKey keyProperty="id" resultType="int" order="AFTER">
                SELECT LAST_INSERT_ID()
            </selectKey>
            INSERT INTO `test`.`tb_user`(`username`, age) VALUES(#{username}, #{age})
        </insert>
    

    3.如何拿到我们刚插入的这个类呢?还是用我们之前插入时的那个newUser,mybatis会给它加上返回的主键的,Mapper方法中返回的那个int只是受影响的行号而已,此时,只会返回0或1

    newUser.getId();  这个不再是空的了
  • 相关阅读:
    python之简单爬虫
    python之正则表达式
    python之面向对象
    python之模块与包
    python之循环嵌套与算法
    linux shell实现从函数返回数组
    linux脚本实现数组值相加
    linux中使用函数输出
    linux shelll中显示的意义
    lsof命令
  • 原文地址:https://www.cnblogs.com/brithToSpring/p/14836229.html
Copyright © 2011-2022 走看看