gpt4 book ai didi

jdbi - 如何结合 JDBI @GetGeneratedKeys 和 Mapper

转载 作者:行者123 更新时间:2023-12-01 13:50:07 24 4
gpt4 key购买 nike

我希望 JDBI 将自动生成的主键(Long 值)转换为另一个类。

我的 DAO:

@RegisterMapper(SystemIdMapper.class)
public interface SystemDao {
@SqlUpdate("insert into systems(device_id, user_id) values(:deviceId.value, :userId.value)")
@GetGeneratedKeys
@Mapper(SystemIdMapper.class)
SystemId insert(@BindBean("deviceId") DeviceId deviceId, @BindBean("userId") UserId userId);

我的映射器:

public class SystemIdMapper implements ResultSetMapper<SystemId> {
@Override
public SystemId map(int index, ResultSet r, StatementContext ctx) {
return new SystemId(0L); //fake mapping to simplify example
}
}

当我运行我的代码时,我在 FigureItOutResultSetMapper.map(..) 中遇到了 NullPointerException,因为

f = factory.mapperFor(rt, ctx);

将 f 设置为空。所以我的猜测是我的映射器注册不正确。

除了使用@RegisterMapper 和@Mapper(SystemIdMapper.class) 注解我也尝试过:

dbi.registerMapper(new SystemIdMapper());

但仍然没有运气。

最佳答案

需要在GetGeneratedKeys注解中指定mapper。

@GetGeneratedKeys(SystemIdMapper.class)

@Mapper 或@RegisterMapper 不用于取回 id。它仅在从表中选择值时使用。

关于jdbi - 如何结合 JDBI @GetGeneratedKeys 和 Mapper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32478300/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com