在mapper的xml文件中配置 useGeneratedKeys
以及 keyProperty 返回Id即可
insert into orders values OrderNumber, orderTime, #{number}, #{orderTime},
PS:Mybatis中insert中返回主键ID的方法
1、XyzMapper.xml
...
或
...
2、XyzMapper.java
public int doSomething(Mapparameters); or public int doSomething(YourClass c);
3、要在map或c中有一个字段名为yourId,Mybatis会自动把主键值赋给这个字段。
Mapparameters = new HashMap (); parameters.put(“yourId”, 1234); ... mapper.doSomething(parameters); System.out.println(“id of the field that is primary key” + parameters.get(“yourId"));
或
YourClass c = new YourClass(); ... mapper.doSomething(c); System.out.println(“id of the field that is primary key” + c.yourId);
好了,到此结束,希望对大家有所帮助!