mybatis just recorded (b) insert the primary key acquisition method

A, using JDBC return values ​​from the growing primary key (primary key adapted to allow only increment the database)

The main change is in the insert disposed on the label the following two properties:
useGeneratedKeys = " to true"
the keyProperty = "ID"

useGeneratedKeys set back to true, MyBat IS uses the JDBC getGeneratedKeys to square method to fetch the primary key generated by the internal database.

Will get its primary key value assigned to the id attribute keyProperty configuration.

When it is desired setting time is set a plurality of attributes, separated by commas, usually required in this case provided keyCo LU Mn properties , press cis specified number of sequence data database column, and the property value keyProperty herein columns arranged eleven corresponds .

Second, using the return value between selectKey

Note the following code, in <insert> tag label selectKey increased.

<selectKey keyColumn=” id” resultType=” long” keyProperty=” id” order=” AFTER” >

SELECT LAST INSERT ID ()

</selectKey>

keyColumn selectKey label, keyProperty above useGeneratedKeys usage of the same meaning,

resu ltType return value is set for the type.

Set order and property database using.

In the MySQL database, the value of the order property set is AFTER, because the primary key of the current record in order to get to the insert statement executed successfully.

In an Oracle database, the value of the order to be set to BEFORE, because it is necessary to get the value of the start sequence Oracle, and inserting values ​​into the database as a primary key.

selectKey  different databases will be different, depending on the database.

Oracle Vice SERT statement in a clear way to write a column and id id value {# } , because the execution of selectKey

After the statement id value there, and we need to put this sequence of values ​​into the database as the primary key, so you must specify the id

 

Column, if this column is not specified, the database will be because the primary key can not be null and throw an exception.

 

@Test
public void testInsert() {
SqlSession sqlSession = getSqlSession();
try {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

SysUser sysUser = new SysUser();
sysUser.setUserName("testl ");
sysUser.setUserPassword(" 123456");
sysUser.setUserEmail("[email protected]");
sysUser.setUserInfo("test info");
//正常情况下应该读入一张图片存到 byte 数纽中
sysUser.setHeadImg(new byte[]{1, 2, 3});
sysUser.setCreateTime ( new new ; a Date ())
the number of rows // the new object into the database, special attention here is the return value of SQL execution result of the influence
int result = userMapper.insert2 (SYSUSER);

. the Assert the assertEquals ( . 1 , Result);
// id is null, no id assigned to, and no configuration id value is written back
the Assert. assertNull (sysUser.getId ());

} the finally {
// order not to affect other tests, where said selection: agricultural
// Since the default sqlSessionFactory openSession () is not automatically submitted
// performed manually is not committed to the database will not commit
sqlSession.rollback ();
sqlSession.close ();
}
}

Guess you like

Origin www.cnblogs.com/lovechengyu/p/11239573.html