1
@Data
@MappedSuperclass
public class BaseModel implements Serializable {
 private static final Long serialVersionUID = -1442801573244745790L;

 @Id
 @GeneratedValue(strategy = GenerationType.IDENTITY)
 private Long id;

 @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
 @Convert(converter = LocalDateTimeConverter.class)
 private LocalDateTime createAt = LocalDateTime.now();

 @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
 @Convert(converter = LocalDateTimeConverter.class)
 private LocalDateTime updateAt = LocalDateTime.now();
}


@Data
@Entity
@Table(name = "tb_vip_code")
@SQLInsert(sql = "insert ignore into tb_vip_code (code, duration) values (?, ?)")
public class VipCode extends BaseModel {
private static final Long serialVersionUID = -4697221755301869573L;

private String code;
private Integer duration;
private Integer status = 0;
private Long userId;

public VipCode() {}
}

@Test
public void addOne() throws Exception {
    VipCode vipCode = new VipCode();
    vipCode.setCode("123456");
    vipCode.setDuration(1);
    service.addOne(vipCode);
}

create table tb_vip_code
(
id bigint auto_increment
    primary key,
code varchar(20) not null,
status int default '0' not null,
user_id bigint null,
duration int not null,
create_at datetime default CURRENT_TIMESTAMP not null,
update_at timestamp default CURRENT_TIMESTAMP not null,
constraint tb_vip_code_code_uindex unique (code)
);

上面的所有代码,我都在尝试使用自定义的sql来保存对象,但是它抛出了一个异常: Caused by: java.sql.SQLException: Parameter index out of range (3 > number of parameters, which is 2)。

我只有两个参数,为什么说需要三个?

4

1 回答 1

0

指定的 SQL 语句@SQLInsert获取绑定为参数的实体的字段。

您的VipCode实体中至少有 4 个属性,我怀疑BaseModel它继承自的类甚至更多。但是您的 SQL 语句只需要两个参数。

要解决此问题,请从实体中删除多余的属性,将它们的参数添加到查询中或将它们标记为@Transient.

于 2018-11-15T06:54:02.140 回答