实体类
Mapper接口
void addUser(User user);
Mapper.xml
<insert id="addUser" useGeneratedKeys="true" keyProperty="id">
insert into user_t(user_name,password,age)
values(#{userName},#{password},#{age})
</insert>
如果你的数据库支持自动生成主键的字段(比如 MySQL 和 SQL Server),那么你可以设置 useGeneratedKeys=”true”,然后再把 keyProperty 设置到目标属性上就OK了。
这样插入的实体可以获得ID了。