MySQL
<insert id="insert" parameterType="com.jbit.entity.Student">
insert into student(id,name)
values (#{id,jdbcType=BIGINT}, #{name,jdbcType=VARCHAR})
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
SELECT LAST_INSERT_ID()
</selectKey>
</insert>
作用:插入一条数据,并将插入的此条数据的主键id返回到 Student 对象中。(仅适用于主键自增)
- SELECT LAST_INSERT_ID():得到刚 insert 进去记录的主键值
- keyProperty:将查询到主键值设置到 parameterType 指定的对象的那个属性
- order:SELECT LAST_INSERT_ID() 执行顺序,相对于 insert 语句来说它的执行顺序
- resultType:指定 SELECTLAST_INSERT_ID() 的结果类型
postgreSQL
<insert id="insert" parameterType="com.jbit.entity.Student">
insert into student(id,name)
values (#{id,jdbcType=BIGINT}, #{name,jdbcType=VARCHAR})
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
SELECT currval('student_id_seq')
</selectKey>
</insert>
作用:插入一条数据,并将插入的此条数据的主键id返回到 Student 对象中。(仅适用于主键自增)
- SELECT currval(‘表名_主键_seq’):得到刚 insert 进去记录的主键值
- keyProperty:将查询到主键值设置到 parameterType 指定的对象的那个属性
- order:SELECT currval(‘表名_主键_seq’) 执行顺序,相对于 insert 语句来说它的执行顺序
- resultType:指定 SELECT currval(‘表名_主键_seq’) 的结果类型
Oracle
待验证
<insert id="createUser" parameterType="pojo.User">
<!-- selectKey 的结果会放进传参 user 对象中-->
<selectKey keyProperty="id" resultType="java.lang.Long" order="BEFORE">
select SEQ_USER_ID.NEXTVAL from dual
</selectKey>
insert into "USER" (ID, USER_NAME, PASSWORD)
values (#{id,jdbcType=BIGINT}, #{userName,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}
)
</insert>