如何在Spring中访问db2序列

时间:2011-05-02 20:49:47

标签: spring spring-mvc

如何在Spring中访问db2序列? 需要访问序列以生成id并使用spring使用jdbcTemplate将其传递给insert语句。

2 个答案:

答案 0 :(得分:1)

首先,您必须定义一个处理序列的bean。像这样:

<bean id="incrementer" class="org.springframework.jdbc.support.incrementer.DB2SequenceMaxValueIncrementer">
    <property name="dataSource" ref="dataSource" />
    <property name="incrementerName" value="YOUR_SEQUENCE_NAME" />
</bean>

然后,您必须将此bean连接到将要调用它的bean。 e.g:

@Autowired
private DataFieldMaxValueIncrementer incrementer;

最后,使用序列bean来获取值:

Long identifier = incrementer.nextLongValue();

答案 1 :(得分:0)

您是否尝试过使用SQL select sequence.nextval from dual

的queryForInt()

http://static.springsource.org/spring/docs/2.5.6/api/org/springframework/jdbc/core/simple/SimpleJdbcTemplate.html#queryForInt(java.lang.String,java.lang.Object ...)