1.Mapper的写法,返回的这个int是受影响的行号

int insertNewUser(User newUser);

2.xml的写法

	<!--返回主键 形式1 -->
    <insert >
        INSERT INTO `test`.`tb_user`(`username`, age) VALUES(#{username}, #{age})
    </insert>

    	<!-- 返回主键 形式2 -->
    <insert >
        <selectKey keyProperty="id" resultType="int" order="AFTER">
            SELECT LAST_INSERT_ID()
        </selectKey>
        INSERT INTO `test`.`tb_user`(`username`, age) VALUES(#{username}, #{age})
    </insert>

3.如何拿到我们刚插入的这个类呢?还是用我们之前插入时的那个newUser,mybatis会给它加上返回的主键的,Mapper方法中返回的那个int只是受影响的行号而已,此时,只会返回0或1

newUser.getId();  这个不再是空的了

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-07-28
  • 2021-10-02
  • 2021-07-19
  • 2022-02-12
猜你喜欢
  • 2022-12-23
  • 2021-08-26
  • 2022-02-14
  • 2021-07-25
  • 2022-02-01
  • 2021-07-27
相关资源
相似解决方案