使用场景:将对象插入id为自增的表中,同时在程序中仍需要用到该对象插入后自增的ID

方法:配置 useGeneratedKeys="true"  keyProperty="对应的主键的对象"

<!--   主要是在主键是自增的情况下,添加成功后可以直接使用主键值,其中keyProperty的值是对象的属性值不是数据库表中的字段名-->
 <insert id="insert" parameterType="user" useGeneratedKeys="true" keyProperty="id">
     insert into User(name,sex,year,create_time) values (#{name},#{sex},#{year},#{create_time})
</insert>

这样在之后的java代码中我们就可以获取该主键对应的对象的属性值(id)

相关文章:

  • 2021-05-16
  • 2022-12-23
  • 2022-01-14
  • 2022-12-23
  • 2021-08-14
  • 2022-02-19
  • 2021-08-27
  • 2022-12-23
猜你喜欢
  • 2021-09-16
  • 2022-12-23
  • 2021-09-22
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案