Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mybatis插入数据返回主键为NULL的问题 #27

Closed
coderqianlq opened this issue Apr 21, 2019 · 0 comments
Closed

Mybatis插入数据返回主键为NULL的问题 #27

coderqianlq opened this issue Apr 21, 2019 · 0 comments

Comments

@coderqianlq
Copy link
Owner

/**
 * 插入用户,返回用户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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant