oracle主键需预先生成,通过sequence;mysql和mssql是插入后生成。
mysql:
<insert id="insertStu_mysql" parameterClass="stu"> insert into stu(name) values (#name#) <selectKey resultClass="int" keyProperty="id" > select LAST_INSERT_ID()
selectKey> insert>
ibatis测试代码:
SqlMapClient client = SqlConfig.getSqlClient(); Stu s = new Stu();
s.setName(“test”); client.insert("insertStu-mysql",s); //获取主键 System.out.print(" pk = " + s.getId());
------------------------------------------------------------------
oracle:预先通过sequence获取
insertStu-oracle" parameterClass=" stu"> int" type=" pre" keyProperty=" id" > SELECT STOCKIDSEQUENCE.NEXTVAL AS VALUE FROM DUAL insert into STU (ID,NAME) values (#id#,#name#)
mysql :插入后生成
<insert id="insertStu-mssql" parameterClass="stu"> insert into STU(name) values (#name#) <selectKey resultClass="int" keyProperty="id" > select @@IDENTITY as value selectKey> insert>