selectKey返回最近一次插入的Id,用于获取插入记录之后的自增Id
<insert id="insert" parameterType="com.qiu.user">
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
SELECT LAST_INSERT_ID()
</selectKey>
insert into user (username,password) values(#{username},#{password})
</insert>
属性详解
keyProperty : selectKey 语句结果应该被设置的目标属性
resultType : 结果的类型
order : 设置为 BEFORE 或 AFTER,如果设置为 BEFORE,那么它会先选择主键,设置 keyProperty 然后执行插入语句;如果设置为 AFTER,那么先执行插入语句,然后是 selectKey 元素
statementType : 和前面的相同,MyBatis 支持 STATEMENT ,PREPARED 和CALLABLE语句的映射类型,分别代表 PreparedStatement 和CallableStatement 类型
Id通过传入对象的 get方法 可以得到