Oracle
- 实体传参
//在SQL中加入以下内容
<selectKey resultType="java.lang.Integer" order="BEFORE" keyProperty="id">
SELECT 序列名.NEXTVAL as id from DUAL
</selectKey>
//实例如:
<insert id="saveUser" parameterType="user" statementType="PREPARED">
<selectKey resultType="java.lang.Integer" order="BEFORE" keyProperty="id">
SELECT user_seq.NEXTVAL as id from DUAL
</selectKey>
INSERT INTO USER_TB (id,name,age)
VALUES(#{id},#{name},#{age})
</insert>
- map传参
//map传参的话,原参数map需要有key为id的键值对,值随意,在SQL中被序列实际值覆盖。
//此为单独一个map参数,如果使用@Param主键设置名称,keyProperty的值为map名称.key。例keyProperty="argMap.id"
//实例如:
<insert id="saveUser" parameterType="hashmap" statementType="PREPARED">
<selectKey resultType="java.lang.Integer" order="BEFORE" keyProperty="id">
SELECT user_seq.NEXTVAL as id from DUAL
</selectKey>
INSERT INTO USER_TB (id,name,age)
VALUES(#{id},#{name},#{age})
</insert>
MySQL
1.实体参数(实体主键属性名为id)
//实例如:
<insert id="saveUser" parameterType="user" statementType="PREPARED">
<selectKey resultType="java.lang.Integer" order="AFTER" keyProperty="id">
SELECT LAST_INSERT_ID()
</selectKey>
INSERT INTO USER_TB (name,age)
VALUES(#{name},#{age})
</insert>
- map参数
//map传参的话,原参数map需要有key为id的键值对,值随意,在SQL中被序列实际值覆盖。
//此为单独一个map参数,如果使用@Param主键设置名称,keyProperty的值为map名称.key。例keyProperty="argMap.id"
//实例如:
<insert id="saveUser" parameterType="hashmap" statementType="PREPARED">
<selectKey resultType="java.lang.Integer" order="AFTER" keyProperty="id">
SELECT LAST_INSERT_ID()
</selectKey>
INSERT INTO USER_TB (name,age)
VALUES(#{name},#{age})
</insert>