jdbi @GetGeneratedKeys

@GetGeneratedKeys在H2, MySQL下都没有问题,但是在SQLServer下需要变通一下才行。

假设有这么一张表(MySQL/H2)

drop table if exists contact;

create table contact(
    id int(11) not null auto_increment,
    firstName varchar(255) not null,
    lastName varchar(255) not null,
    phone varchar(30) not null,
    memo varchar(255),
    primary key(id)
)

JDBI的DAO可以这么写

public interface InstallmentDAO {
    @GetGeneratedKeys
    @SqlUpdate("insert into contact(firstName, lastName, phone, memo) values(:firstName, :lastName, :phone, :memo)"
    String save(@BindBean Contact contact);
}

但是在SQLServer中需要这么写

public interface InstallmentDAO {
    @GetGeneratedKeys
    @SqlQuery("insert into contact(firstName, lastName, phone, memo) values(:firstName, :lastName, :phone, :memo) OUTPUT INSERTED.Id"
    String save(@BindBean Contact contact);
}

Published: December 09 2015