简体   繁体   中英

How to call a Oracle database sequence number by using MyBatis?

I want to call a sequence number from my Oracle Database 10g by using MyBatis, but I only get an error message like the following:

ORA-02289: Sequence is not available.

How can I call a sequence number from an Oracle Database?

Here is my Maven Project Dependency concerning the current MyBatis Version:

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

Here is my Dao Java class:

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

Here is my xml declaration:

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

I think you use nextval for inserting.Try following:

<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>

Also instead of SELECT nextVal('mySeq') you can use this SELECT mySeq.nextVal from dual

I was able to achieve this,

<dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.5.2</version>
    </dependency> 



<select id="getUploadId" resultType="int">
    select {schema}.SEQUENCE_NAME.NEXTVAL from dual
</select>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM