mybatis mysql自增主键返回

来源:互联网 发布:最优化算法袁亚湘pdf 编辑:程序博客网 时间:2024/06/09 07:44

对于自增主键的返回

<insert id="insertUser" parameterType="cn.itcast.mybatis.po.User">        <!-- selectKey实现将主键返回,将主键返回到user对象中             keyProperty:返回的主键存储在pojo中的哪个属性            order:selectKey的执行顺序,是相对与insert语句来说,由于mysql的自增原理执行完insert语句之后才将主键生成,所以这里selectKey的执行顺序为after            resultType:返回的主键是什么类型            LAST_INSERT_ID():是mysql的函数,返回auto_increment自增列新记录id值。        -->        <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">            select LAST_INSERT_ID()        </selectKey>       insert into user(username,birthday,sex,address)        values(#{username},#{birthday},#{sex},#{address});</insert>