The code for mybatis oracle self-incrementing sequence is as follows:
<insert id="insert " useGeneratedKeys="true" keyProperty="s_id" parameterType="xxxx" > <selectKey resultType="int" order="BEFORE" keyProperty="s_id"> SELECT SEQ_TABLE.NEXTVAL FROM dual </selectKey> INSERT INTO <span style="font-family: Arial, Helvetica, sans-serif;">s_id</span><span style="font-family: Arial, Helvetica, sans-serif;">,name,age</span> VALUES (#{s_id} #{name}, #{age} ) </insert>resultType="int" returns an int type
keyProperty assigns the return value to: parameterType's object's property, that is, the s_id property in the xxxxx class
useGeneratedKeys="true" keyProperty="s_id"
Return the sequence id, if not required, you can not fill it out
Let's take a look at the primary key growth of mybatis configuration oracle
MySQL, SQLServer and other databases themselves have primary key self-growth functions like auto_increment. They can be used directly
useGeneratedKeys="true" to implement, such as the following configuration
insert into s_user_auth (id,user_id, user_name) values(#{id},#{userid},#{username})However, oracle does not work. The self-growth of the oracle primary key is achieved through sequence, so this method is not possible and you need to use:
1. First find out the id through sequence, then insert
select yoursequence.nextval as id from dualinsert into s_user_auth (id,user_id, user_name) values(#{id},#{userid},#{username})2. Direct insertsequence.nextval, as follows
insert into s_user_auth (id,user_id, user_name) values(yoursequence.nextval,#{userid},#{username})