mybatis -- 插入数据并获取自增长Id

xml 方式:

  <insert id="insertUserAndGetId" parameterType="com.bl.crud01.mapper.User">
    <selectKey keyProperty="id" keyColumn="id" resultType="int" order="AFTER">
      select last_insert_id();
    </selectKey>
    insert into user(username, birthday, sex, address)
    values(#{username}, #{birthday}, #{sex}, #{address});
  </insert>

注解方式: 

 @Insert("insert into user(username,address,sex,birthday) values(#{username},#{address},#{sex},#{birthday})")
  @Options(useGeneratedKeys=true,keyProperty = "id", keyColumn = "id")
  void insertUser(User user);

猜你喜欢

转载自blog.csdn.net/qq_38238041/article/details/103019741
今日推荐