先举个mysql的例子
<insert id="insertRecord" parameterType="map" >
INSERT INTO T_FILEDETAIL
(STATUS, CREATE_ID, CREATE_DATE, FIDA_TYPE, REMARK, CUST_ID)
VALUES(
0, null,null, 0, null, 1000
);
<selectKey resultType="int" keyProperty="FIDA_ID" >
SELECT LAST_INSERT_ID() AS FIDA_ID;
</selectKey>
</insert>
SqlMapper.insert("*.insertRecord",map);
//获取id
int a = Integer.valueOf(m.get("FIDA_ID").toString());
a 就是返回的id的值。
加入selectKey这段代码就可以实现id自增和获取
针对不同数据库的写法如下:
- Cloudscape VALUES IDENTITY_VAL_LOCAL()
- DB2 VALUES IDENTITY_VAL_LOCAL()
- Derby VALUES IDENTITY_VAL_LOCAL()
- HSQLDB CALL IDENTITY()
- MySql SELECT LAST_INSERT_ID()
- SqlServer SELECT SCOPE_IDENTITY()
- SYBASE SELECT @@IDENTITY
- ORACLE SELECT CUSTOM_SQL.NEXTVAL AS ID FROM DUAL