我想使用MyBatis从Oracle数据库10g中调用序列号,但是我只收到如下错误消息:

ORA-02289: Sequence is not available.


如何从Oracle数据库调用序列号?

这是有关当前MyBatis版本的Maven项目依赖关系:

<dependency>
   <groupId>org.mybatis</groupId>
   <artifactId>mybatis-spring</artifactId>
   <version>1.1.1</version>
</dependency>


这是我的Dao Java类:

long mySeqNumber = myDaoClass.getNewNumber(); // here I get an exception


这是我的xml声明:

<select id="getNewNumber" resultType="java.lang.Long" >
    SELECT mySeq.nextval
    FROM dual
</select>

最佳答案

我认为您使用nextval进行插入。请尝试以下操作:

<insert id="insertPerson" parameterType="Person" useGeneratedKeys="true">
  <selectKey keyProperty="personId" resultType="int" order="BEFORE">
    SELECT nextVal('mySeq')
  </selectKey>
  INSERT INTO person (personId,PersonName) VALUES (#{personId},#{personName})
</insert>


另外,也可以使用SELECT nextVal('mySeq')代替SELECT mySeq.nextVal from dual

10-06 14:01