Article directory
Mybaits primary key value updating
Sometimes you need to get the primary key value of the newly inserted row after sql execution, which is generated automatically. You need to use the following methods to get the value.
- Return the primary key value generated by the current session (supported by MySQL and Oracle)
- You can write back the primary key value by setting the attribute. The principle is unknown (only supported by MySQL)
Return the primary key value generated by the current session (supported by MySQL and Oracle)
<insert id="addUser2" parameterType="user" > <!-- Get the primary key existing in the current session --> <selectKey keyProperty="id" resultType="int"> select LAST_INSERT_ID() </selectKey> insert into t_user (name,age)values(#{name},#{age}) </insert>
Test it.
@Test public void test2() throws IOException { InputStream in = Resources.getResourceAsStream("mybatis-cfg.xml"); SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in); SqlSession session = factory.openSession(true); // Get proxy class UserMapper dao = session.getMapper(UserMapper.class); User user = new User(); user.setName("cads"); user.setAge(12); dao.addUser2(user); System.out.println("Generated id:"+user.getId()); session.close(); }
Print result paste
[QC] DEBUG [main] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(159) | ==> Preparing: insert into t_user (name,age)values(?,?) [QC] DEBUG [main] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(159) | ==> Parameters: cads(String), 12(Integer) [QC] DEBUG [main] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(159) | <== Updates: 1 [QC] DEBUG [main] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(159) | ==> Preparing: select LAST_INSERT_ID() [QC] DEBUG [main] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(159) | ==> Parameters: [QC] DEBUG [main] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(159) | <== Total: 1 //Generated id:56
You can write back the primary key value by setting the attribute. The principle is unknown (only supported by MySQL)
<!-- useGeneratedKeys="true" Use living key keyProperty="id" Survival key Bound to user Object id Attribute --> <insert id="addUser1" parameterType="user" useGeneratedKeys="true" keyProperty="id"> insert into t_user (name,age)values(#{name},#{age}) </insert>
Test it.
@Test public void test1() throws IOException { InputStream in = Resources.getResourceAsStream("mybatis-cfg.xml"); SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in); SqlSession session = factory.openSession(true); // Get proxy class UserMapper dao = session.getMapper(UserMapper.class); User user = new User(); user.setName("cads"); user.setAge(12); dao.addUser1(user); System.out.println("Generated id:"+user.getId()); session.close(); }
Print result paste
[QC] DEBUG [main] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(159) | ==> Preparing: insert into t_user (name,age)values(?,?) [QC] DEBUG [main] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(159) | ==> Parameters: cads(String), 12(Integer) [QC] DEBUG [main] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(159) | <== Updates: 1 //Generated id:57
Pack Project Extraction code: ix65 environment: eclipse, maven