MyBatis自增主键返回

mysql自增主键返回

<!-- 保存用户 -->

<insert id="saveUser" parameterType="com.itheima.mybatis.pojo.User">

<!-- selectKey 标签实现主键返回 -->

<!-- keyProperty:主键对应的pojo中的哪一个属性 -->

<!-- order:设置在执行insert语句前执行查询id的sql,孩纸在执行insert语句之后执行查询id的sql -->

<!-- resultType:设置返回的id的类型 -->

<selectKey keyProperty="id" order="AFTER" resultType="Integer">

SELECT LAST_INSERT_ID()

</selectKey>

INSERT INTO USER

(username,birthday,sex,address) VALUES

(#{username},#{birthday},#{sex},#{address})

</insert>

 

LAST_INSERT_ID():是Mysql的函数,返回auto_increment自增列新记录id值。

 

 

ORACLE非自增主键返回

<insert id="insertQrtzJob">

insert into x () values ())

<selectKey resultType="Long" order="BEFORE" keyProperty="jobId"> #实体类的主键id

SELECT QRTZ_JOB_SEQ.Nextval from DUAL

</selectKey>

</insert>

以上这种试用于表中不关联触发器与序列,主键都靠插入

ORACLE自增主键返回

<insert id="insertQrtzJob">

insert into x () values ())

<selectKey resultType="Long" keyProperty="jobId" order="AFTER">

select QRTZ_JOB_SEQ.CURRVAL from dual

</selectKey>

</insert>

这种适用表关联触发器与序列

猜你喜欢

转载自blog.csdn.net/weixin_42210904/article/details/109150465