@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来保存对象,但它抛出了一个异常:
原因:java.sql.SQLException:参数索引超出范围(3〉参数数,即2)。
我只有两个参数为什么说需要三个
1条答案
按热度按时间wlp8pajw1#
由
@SQLInsert
指定的SQL语句将实体的字段绑定为参数。您的
VipCode
实体中至少有4个属性,我怀疑它继承的BaseModel
类中有更多属性,但是您的SQL语句只需要两个参数。若要解决此问题,请从实体中删除多余的属性,将它们的参数添加到查询中或将它们标记为
@Transient
。