<> be careful : This method is only applicable to MySQL and SQLServer database
<> Using properties useGeneratedKeys and keyProperty
*
useGeneratedKeys(true/false): Set whether to use JDBC Of getGenereatedKeys Method to get the primary key and assign it to keyProperty Set the domain model properties
* keyProperty : fill in java The field name of the object
<> Using examples
mapper
<insert id="addUser" parameterType="xxx.xxx.model.xx" useGeneratedKeys="true"
keyProperty="id"> insert into user(username, name, password) values
(#{item.username}, #{item.name}, #{item.password})</insert>
service
public void addUser(User user) { systemMapper.addUser(user); user.getId(); }
<>user.getId() Cannot get the latest primary key id
inspect dao Is the layer interface used @Param annotation
for example :
void addUser(@Param("item") User user);
So change it mapper.xml
<insert id="addUser" parameterType="xxx.xx.xx" useGeneratedKeys="true"
keyProperty="item.id"> insert into user(username, name, password) values
(#{item.username}, #{item.name}, #{item.password})</insert>
Technology