Mybatis oracle self-growth and UUID two primary key generation strategies

 

The self-growth of oracle database is not as simple as that of mysql database. MySQL can set auto_increment when creating a table, but Oracle cannot do this. Oracle database must establish a sequence, and then use this sequence when inserting id in mybatis, in order to realize oracle id Self Growth.

Here is the creation statement for the sequence:

 

create sequence TBAL_OA_HOLIDAY_TYPE //sequence name
increment by 1 // increment by 1, you can also set other numbers
start with 1 // start growing from id=1
 maxvalue 9999 //The maximum value is 9999, according to the situation
 minvalue 1 //minimum value 1
 nocycle //No cycle, that is, it keeps growing       
 cache 20 //Set the cache cache sequence, if the system is down or other circumstances will cause the sequence to be discontinuous, it can also be set to ---------NOCACHE
north;

 

 

After the oracle sequence is established, we can insert the id in mybatis using it like this:

 

    insert into TBL_OA_HOLIDAY_TYPE (ID, TYPE_NAME, REMARK
      )
    values (TBAL_OA_HOLIDAY_TYPE.NEXTVAL, #{typeName,jdbcType=VARCHAR}, #{remark,jdbcType=VARCHAR}
      )

 

 

We replace the id in the form of (sequence name + .NEXTVAL) in the position where the id is inserted into mybatis, instead of taking the value from the page like #{xxx}, it's that simple, try it yourself, of course, the id must be It is of type number.

If UUID is used as the primary key, then the primary key is of varchar2 type, so that the string uuid can be used. The following is the use of uuid of mybatis oracle, which is much easier than oracle self-growth:

Using uuid as the primary key, there is a sys_guid function in oracle that can generate uuid.

 

<insert id="insert" parameterType="com.xxx.SystemDepartment">
    <selectKey keyProperty="id" resultType="String" order="BEFORE">
        select sys_guid() from dual
    </selectKey>
     insert into TBL_OA_HOLIDAY_TYPE (ID, TYPE_NAME, REMARK
      )
    values (#{id,jdbcType=VARCHAR}, #{typeName,jdbcType=VARCHAR}, #{remark,jdbcType=VARCHAR}
      )
</insert>

 

The SelectKey property works as follows:

Property description
keyProperty The target property to which the result of the selectKey statement should be set. It can also be a comma-separated list of property names if multiple generated columns are desired.
keyColumn Column names in the returned result set that match the attribute. It can also be a comma-separated list of property names if multiple generated columns are desired.
resultType The type of result. MyBatis can usually be calculated, but there is no problem in writing it for more certainty. MyBatis allows any simple type to be used as a primary key type, including strings. If you want to act on multiple generated columns, you can use an Object or a Map containing the desired properties.
order This can be set to BEFORE or AFTER. If set to BEFORE, then it will first select the primary key, set the keyProperty and then execute the insert statement. If set to AFTER, the insert statement is executed first, followed by the selectKey element - this is similar to databases like Oracle, which may have embedded index calls inside the insert statement.
statementType As before, MyBatis supports the mapping types of STATEMENT, PREPARED and CALLABLE statements, representing PreparedStatement and CallableStatement types respectively.

 

 

In mybatis, insert, update, and delete all return int, which is the number of records that have been successfully operated. The function of SelectKey is to insert the query value into the object property.

For example, the parameter of insert is a Person object, and the keyProperty value of SelectKey is the id property of Person. After execution, the id of the parameter object Person will be set to the SelectKey query value.

can be obtained in the parameter object

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326247359&siteId=291194637