Get Oracle sequence in MyBatis

1. Application scenario:

      When mybatis is used in the application system, and first need to get the next value of the sequence to complete some functional operations, and then add data.

Two, the code:

<select id="findMaxId" parameterType="pd" resultType="java.lang.Integer" useCache="false" flushCache="true" >
       select APP_CUSTOM_ID_SEQ.NEXTVAL FROM DUAL
</select>
<sql id='APP_CUSTOM_ID_SEQ'>APP_CUSTOM_ID_SEQ.currval</sql>
<insert id="save" parameterType="pd">
   <selectKey keyProperty="Id" resultType="int" order="BEFORE">
	   select <include refid="APP_CUSTOM_ID_SEQ" /> from dual
   </selectKey>
   insert into APP_CUSTOM(
          ID,
          CREATE_NAME,
	  CREATE_TIME,
	  ALTER_TIME,
	  CREATE_USERNAME,		     
	  APP_DES
   )values(
          #{Id},
          #{CREATE_NAME},
	  #{CREATE_TIME},
	  #{ALTER_TIME},
	  #{CREATE_USERNAME},
	  #{APP_DES}
   )
</insert>
Note: APP_CUSTOM_ID_SEQ in the data insertion above is the current value of the sequence, which cannot be executed separately. It needs to be executed after obtaining the nextval, otherwise an error will be reported.


Guess you like

Origin blog.csdn.net/joyksk/article/details/79710423