Mybatis中insert中返回主键ID的方法——MySQL

1、XyzMapper.xml(在插入过程自动获取一个自增的主键)


...


...

2、XyzMapper.java

public int doSomething(Map parameters);

public int doSomething(YourClass c);

3、要在map或c中有一个字段名为yourId,Mybatis会自动把主键值赋给这个字段。

Map parameters = 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);

 

转载自:https://blog.csdn.net/prevention/article/details/32825081

你可能感兴趣的:(JAVA,mybatis,数据库,mybatis,insert,返回主键ID)