Entity class UserInfo, attribute String username,string password,int usercode,int userId
1.ORACLE Database
1-1 self-increment primary key
To implement a self-increment primary key in Oracle
CREATE SEQUENCE If_kikaku. Seq_user_id_wing
INCREMENT by 1
START with 1
MAXVALUE 999999999
nominvalue
nocycle
Noorder
NOCACHE
/
Mapper.xml
Use If_kikaku. Seq_user_id_wing. Nextval gets the primary key to insert data into.
<insert id= "GetPrimaryKey" parametertype= "Com.test.model.UserInfo" >
<selectkey keyproperty= "UserId" Order= "Before" resulttype= "Java.lang.Integer" >
select If_kikaku. Seq_user_id_wing. Nextval from DUAL
</selectKey>
inserts into If_kikaku.test_user_m (
user_id,
user_cd,
Password
) VALUES (
${userid},
${usercode},
#{password}
)
</insert>
ParameterType: The parameter type of the SQL statement that performs the insert operation.
Keyproperty: Sets the queried primary key value to parametertype which property of the specified object.
Order: The sequence in which the SQL statements inside the label <selectKey> are executed relative to the INSERT statement. Before the Before:insert statement executes. After the After:insert statement executes.
1-2uuid
Get the random GUID as the primary key using the Oracle Sys_guid () method.
<insert id= "method name" Parametertype= "entity class" >
<selectkey keyproperty= "entity class Properties" order= "before" resulttype= " Java.lang.Integer ">
Select Sys_guid () from DUAL
</selectKey>
INSERT to If_kikaku.test_user_m (
primary key,
...
) VALUES (
${entity class properties},
...
)
</insert>
2.MYSQL Database
2-1 self-increment primary key
USER_ID NOT NULL Auto_increment
Mapper.xml
Using the MySQL last_insert_id () method to get the primary key after the INSERT operation, which is not the same as Oracle, Oracle takes the primary key value first and then performs the insert operation.
Simply executing the Select LAST_INSERT_ID () statement returns only 0.
<insert id= "GetPrimaryKey" parametertype= "Com.test.model.UserInfo" >
<selectkey keyproperty= "UserId" Order= "after" resulttype= "Java.lang.Integer" >
Select last_insert_id ()
</selectKey>
INSERT Into If_kikaku.test_user_m (
user_cd,
password,
user_nm,
upd_time,
ins_time
) VALUES (
${usercode},
#{password},
#{username},
sysdate (),
sysdate ()
)
</insert >
2-2uuid
<insert id= "method name" Parametertype= "entity class" >
<selectkey keyproperty= "entity class Properties" order= "before" resulttype= " Java.lang.Integer ">
Select UUID ()
</selectKey>
INSERT into if_kikaku.test_user_m (
primary key ,
...
) VALUES (
${entity class properties},
...
)
</insert>
Gets the call of the primary key
public void GetPrimaryKey () {
UserInfo user = new UserInfo ("name", "Pawd", 4536);
Mytestservice.getprimarykey (user);
System.out.println ("The primary key for the newly inserted record is:" + User.getuserid ());
}