I am new to mybatis. I am trying to get the id of the last inserted record. My database is mysql and my xperter mapper
<insert id="insertSelective" parameterType="com.mycom.myproject.db.mybatis.model.FileAttachment" > <selectKey resultType="java.lang.Long" keyProperty="id" order="AFTER" > SELECT LAST_INSERT_ID() as id </selectKey> insert into fileAttachment <trim prefix="(" suffix=")" suffixOverrides="," > <if test="name != null" > name, </if> <if test="attachmentFileSize != null" > size, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides="," > <if test="name != null" > #{name,jdbcType=VARCHAR}, </if> <if test="attachmentFileSize != null" > #{attachmentFileSize,jdbcType=INTEGER}, </if> </trim> </insert>
I thought that the instruction is written here 'SELECT LAST_INSERT_ID (), since id' should return the identifier of the last inserted record, but I always get 1 after inserting the record.
My mapper.java class I have a method
int insertSelective(FileAttachment record);
In my dao class, I use
int id = fileAttachmentMapper.insertSelective (fileAttachment);
I get the Id value always 1 when a new record is inserted. my Id field is automatically incremented and the records are inserted correctly.
source share