问题复现:
实体类:
package com.sky.entity;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
/**
* 套餐
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Setmeal implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
//分类id
private Long categoryId;
//套餐名称
private String name;
//套餐价格
private BigDecimal price;
//状态 0:停用 1:启用
private Integer status;
//描述信息
private String description;
//图片
private String image;
private LocalDateTime createTime;
private LocalDateTime updateTime;
private Long createUser;
private Long updateUser;
}
Service中的方法:
public void saveSetmeal(SetmealDTO dto) {
setmealMapper.insert(dto);
System.out.println(build.getId());
}
Mapper中的方法:
@AutoFill(OperationType.INSERT)
void insert(Setmeal build);
xml中的sql:
<insert id="insert">
insert into
setmeal(`category_id`,`name`,`price`,`status`,`description`,`image`,`create_time`,`update_time`,`create_user`,`update_user`)
<trim prefix="VALUES (" suffix=")" suffixOverrides=",">
<if test="categoryId != null">#{categoryId},</if>
<if test="name != null">#{name},</if>
<if test="price != null">#{price},</if>
<if test="status != null">#{status},</if>
<if test="description != null">#{description},</if>
<if test="image != null">#{image},</if>
<if test="createTime != null">#{createTime},</if>
<if test="updateTime != null">#{updateTime},</if>
<if test="createUser != null">#{createUser},</if>
<if test="updateUser != null">#{updateUser},</if>
</trim>
</insert>j
解决方法:
方案一:在xml中的sql标签添加
<insert id="insert" useGeneratedKeys="true" keyProperty="id">
insert into
setmeal(`category_id`,`name`,`price`,`status`,`description`,`image`,`create_time`,`update_time`,`create_user`,`update_user`)
<trim prefix="VALUES (" suffix=")" suffixOverrides=",">
<if test="categoryId != null">#{categoryId},</if>
<if test="name != null">#{name},</if>
<if test="price != null">#{price},</if>
<if test="status != null">#{status},</if>
<if test="description != null">#{description},</if>
<if test="image != null">#{image},</if>
<if test="createTime != null">#{createTime},</if>
<if test="updateTime != null">#{updateTime},</if>
<if test="createUser != null">#{createUser},</if>
<if test="updateUser != null">#{updateUser},</if>
</trim>
</insert>
成功解决!
方案二:使用注解方式,同时insert语句也要使用注解形式,抛弃xml中的sql语句
@Insert("INSERT INTO setmeal (category_id, name, price, status, description, image, create_time, update_time, create_user, update_user) " +
"VALUES (#{categoryId}, #{name}, #{price}, #{status}, #{description}, #{image}, #{createTime}, #{updateTime}, #{createUser}, #{updateUser})")
@Options(useGeneratedKeys = true, keyProperty = "id")
void insert(Setmeal build);
成功解决!!!
但是千万注意!!!
不可以混用,
错误案例:sql语句在xml中,useGenerateKey在注解中;同理,注解中使用sql,xml使用useGenerateKey也不行!!!如下:
@Options(useGeneratedKeys = true, keyProperty = "id")
void insert(Setmeal build);
<insert id="insert">
insert into
setmeal(`category_id`,`name`,`price`,`status`,`description`,`image`,`create_time`,`update_time`,`create_user`,`update_user`)
<trim prefix="VALUES (" suffix=")" suffixOverrides=",">
<if test="categoryId != null">#{categoryId},</if>
<if test="name != null">#{name},</if>
<if test="price != null">#{price},</if>
<if test="status != null">#{status},</if>
<if test="description != null">#{description},</if>
<if test="image != null">#{image},</if>
<if test="createTime != null">#{createTime},</if>
<if test="updateTime != null">#{updateTime},</if>
<if test="createUser != null">#{createUser},</if>
<if test="updateUser != null">#{updateUser},</if>
</trim>
</insert>