1. 程式人生 > >mybatis 插入返回自增後的id

mybatis 插入返回自增後的id

//serviceImpl
int customerId = customerDao.insertDynamic(customer1);
System.out.println("id===================="+customer1.getId());

  

useGeneratedKeys:開啟獲取主鍵
keyProperty:那個欄位是主鍵
<insert id="insertDynamic" parameterType="com.wftdlx.carApi.entity.Customer"  useGeneratedKeys="true" keyProperty="id"
> insert into customer <trim prefix="(" suffix=")" suffixOverrides="," > <if test="id != null" > id, </if> <if test="activedate != null" > activedate, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides="," > <if test="id != null" > #{id,jdbcType=BIGINT}, </if> <if test="activedate != null" > #{activedate,jdbcType=VARCHAR}, </if> </trim> </insert>

  輸出:id====================195466