首先设置id为递增
@Insert("INSERT INTO d_delivery (id,deliveryNo,groupCode,orderNo,quantity,expressNo,notes,createTime,expressTime,receiveTime,`status`,productType)" +
"VALUES (null,#{deliveryNo},#{groupCode},#{orderNo},#{quantity},#{expressNo},#{notes},#{createTime},#{expressTime},#{receiveTime},#{status},#{productType})")
@Options(useGeneratedKeys = true, keyProperty = "id")
void createDelivery(Delivery delivery);
@Options()中useGeneratedKeys 默认为flase
如果需要在mapper文件中写sql语句:
keyProperty 属性的作用是指定主键 ID 赋值的目标属性。在配置文件中,我们可以在 insert 元素中设置 keyProperty 属性:
<insert id="insertUser" parameterType="Delivery " useGeneratedKeys="true" keyProperty="id">
//sql语句
</insert>
或者使用SELECT LAST_INSERT_ID() 函数
<insert id="insertUser" parameterType="Delivery " useGeneratedKeys="false">
//sql语句
<selectKey resultType="java.lang.Integer" keyProperty="id" order="AFTER">
SELECT LAST_INSERT_ID()
</selectKey></insert>