Mybatis插入数据返回主键为NULL的问题
Closed this issue · 0 comments
coderqianlq commented
/**
* 插入用户,返回用户id
* 需要注意的是在mapper接口中不能使用@Param("")绑定传入实体参数,这样主键会绑定到新建的实体中。
* 如@Param("user") UserEntity user主键会绑定到新建的实体,即getId() = null。
*
* @param user 用户实体
* @return Long
*/
Long insertUser(UserEntity user);
// Long insertUser(@Param("user") UserEntity user) 使用@Param("")绑定传入实体参数,会使主键绑定到新建的实体中
<insert id="insertUser" parameterType="User">
<selectKey keyProperty="id" order="BEFORE" resultType="java.lang.String">
select replace(uuid(), '-', '') as id from dual
</selectKey>
insert into user(id, name, birth)
values (#{id}, #{name}, #{birth})
</insert>