<insert id="insert" useGeneratedKeys="true" keyProperty="id" parameterType="com.core.entity.Problem"> INSERT INTO problem(u_id,data,date,img_url) VALUES (#{uId},#{data},sysdate(),#{imgUrl}) </insert>
可以看到,加了 useGeneratedKeys="true" keyProperty="id" 其中keyProperty="id" id就是你传入进来对象中存在的属性值,执行成功后,会自动将主键值赋值给对象中的id属性。 怎么获取:直接调用传入对象的getId方法即可。