问题描述
我测试了很多种配置方法都不能实现1.我的数据库是mysql 配置insert语句 <insert id="addUser" parameterType="org.springframework.samples.domain.User" useGeneratedKeys="true" keyProperty="id" > insert into users (id,name,rdate,age) values (#{id},#{name},#{rdate,jdbcType=INTEGER},#{age}) </insert>用mybatis执行 userDao.addUser(user)后为什么user.getId还是为空2.我的数据库是mysql 配置insert语句 <insert id="addUser" parameterType="org.springframework.samples.domain.User"> insert into users (id,name,rdate,age) values (#{id},#{name},#{rdate,jdbcType=INTEGER},#{age}) <selectKey keyProperty="id" resultType="long" order="AFTER"> SELECT @@IDENTITY AS id 或者 SELECT LAST_INSERT_ID()</selectKey> </insert>用mybatis执行 userDao.addUser(user)后,为什么user.getId还是为空并且SELECT @@IDENTITY AS id 或者 SELECT LAST_INSERT_ID() 没有被执行。如何设置为 order="BEFORE" 会执行,但是会包主键重复日志如下 [DEBUG][2012-03-08 14:14:17,924][java.sql.Connection]ooo Connection Opened[DEBUG][2012-03-08 14:14:17,955][org.mybatis.spring.SqlSessionUtils]Registering transaction synchronization for SqlSession[DEBUG][2012-03-08 14:14:18,127][jdbc.audit]2. PreparedStatement.new PreparedStatement returned org.apache.commons.dbcp.DelegatingConnection.prepareStatement(DelegatingConnection.java:255)[DEBUG][2012-03-08 14:14:18,127][jdbc.audit]2. Connection.prepareStatement(insert into users (id,name,rdate,age) values (?,?,?,?)) returned net.sf.log4jdbc.PreparedStatementSpy@857066 org.apache.commons.dbcp.DelegatingConnection.prepareStatement(DelegatingConnection.java:255)[DEBUG][2012-03-08 14:14:18,158][jdbc.audit]2. PreparedStatement.setQueryTimeout(25000) returned org.apache.commons.dbcp.DelegatingStatement.setQueryTimeout(DelegatingStatement.java:249)[DEBUG][2012-03-08 14:14:18,158][jdbc.audit]2. PreparedStatement.setNull(1, 1111) returned org.apache.commons.dbcp.DelegatingPreparedStatement.setNull(DelegatingPreparedStatement.java:105)[DEBUG][2012-03-08 14:14:18,158][jdbc.audit]2. PreparedStatement.setString(2, "2") returned org.apache.commons.dbcp.DelegatingPreparedStatement.setString(DelegatingPreparedStatement.java:132)[DEBUG][2012-03-08 14:14:18,158][jdbc.audit]2. PreparedStatement.setLong(3, 1331187257) returned org.apache.commons.dbcp.DelegatingPreparedStatement.setLong(DelegatingPreparedStatement.java:120)[DEBUG][2012-03-08 14:14:18,174][jdbc.audit]2. PreparedStatement.setInt(4, 1) returned org.apache.commons.dbcp.DelegatingPreparedStatement.setInt(DelegatingPreparedStatement.java:117)[DEBUG][2012-03-08 14:14:18,174][jdbc.audit]2. PreparedStatement.addBatch() returned org.apache.commons.dbcp.DelegatingPreparedStatement.addBatch(DelegatingPreparedStatement.java:172)-2147482646null[DEBUG][2012-03-08 14:14:18,174][org.springframework.jdbc.datasource.DataSourceTransactionManager]Initiating transaction commit[DEBUG][2012-03-08 14:14:18,174][jdbc.audit]2. Connection.isClosed() returned false org.apache.commons.dbcp.DelegatingConnection.toString(DelegatingConnection.java:95)[DEBUG][2012-03-08 14:14:18,174][jdbc.audit]2. Connection.getMetaData() returned com.mysql.jdbc.DatabaseMetaData@fb1135 org.apache.commons.dbcp.DelegatingConnection.toString(DelegatingConnection.java:99)[DEBUG][2012-03-08 14:14:18,174][org.springframework.jdbc.datasource.DataSourceTransactionManager]Committing JDBC transaction on Connection [jdbc:mysql://127.0.0.1:3306/sdsp?useUnicode=true&characterEncoding=utf-8, UserName=root@localhost, MySQL-AB JDBC Driver][DEBUG][2012-03-08 14:14:18,174][jdbc.audit]2. Connection.commit() returned org.apache.commons.dbcp.DelegatingConnection.commit(DelegatingConnection.java:308)[DEBUG][2012-03-08 14:14:18,174][jdbc.sqlonly] org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297)2. batching 1 statements: 1: insert into users (id,name,rdate,age) values (NULL,'2',1331187257,1) [DEBUG][2012-03-08 14:14:18,174][jdbc.sqltiming] org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297)2. batching 1 statements: 1: insert into users (id,name,rdate,age) values (NULL,'2',1331187257,1) {executed in 0 msec}[DEBUG][2012-03-08 14:14:18,174][jdbc.audit]2. PreparedStatement.executeBatch() returned [I@1e3e7d2 org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297)[DEBUG][2012-03-08 14:14:18,174][jdbc.audit]2. PreparedStatement.close() returned org.apache.commons.dbcp.DelegatingStatement.close(DelegatingStatement.java:168)[DEBUG][2012-03-08 14:14:18,190][org.mybatis.spring.SqlSessionUtils]Transaction synchronization committed SqlSession[DEBUG][2012-03-08 14:14:18,190][org.mybatis.spring.SqlSessionUtils]Transaction synchronization closed SqlSession[DEBUG][2012-03-08 14:14:18,440][jdbc.audit]2. Connection.setAutoCommit(true) returned org.apache.commons.dbcp.DelegatingConnection.setAutoCommit(DelegatingConnection.java:338)[DEBUG][2012-03-08 14:14:18,440][jdbc.audit]2. Connection.isReadOnly() returned false org.apache.commons.dbcp.DelegatingConnection.isReadOnly(DelegatingConnection.java:329)[DEBUG][2012-03-08 14:14:18,440][jdbc.audit]2. Connection.isClosed() returned false org.apache.commons.dbcp.DelegatingConnection.toString(DelegatingConnection.java:95)[DEBUG][2012-03-08 14:14:18,455][jdbc.audit]2. Connection.getMetaData() returned com.mysql.jdbc.DatabaseMetaData@1a81bd5 org.apache.commons.dbcp.DelegatingConnection.toString(DelegatingConnection.java:99)[DEBUG][2012-03-08 14:14:18,455][org.springframework.jdbc.datasource.DataSourceTransactionManager]Releasing JDBC Connection [jdbc:mysql://127.0.0.1:3306/sdsp?useUnicode=true&characterEncoding=utf-8, UserName=root@localhost, MySQL-AB JDBC Driver] after transaction[DEBUG][2012-03-08 14:14:18,455][org.springframework.jdbc.datasource.DataSourceUtils]Returning JDBC Connection to DataSource[DEBUG][2012-03-08 14:14:18,455][jdbc.audit]2. Connection.isClosed() returned false org.apache.commons.dbcp.DelegatingConnection.isClosed(DelegatingConnection.java:353)[DEBUG][2012-03-08 14:14:18,455][jdbc.audit]2. Connection.getAutoCommit() returned true org.apache.commons.dbcp.DelegatingConnection.getAutoCommit(DelegatingConnection.java:311)[DEBUG][2012-03-08 14:14:18,455][jdbc.audit]2. Connection.clearWarnings() returned org.apache.commons.dbcp.DelegatingConnection.clearWarnings(DelegatingConnection.java:305)[DEBUG][2012-03-08 14:14:18,455][jdbc.audit]2. Connection.getAutoCommit() returned true org.apache.commons.dbcp.DelegatingConnection.getAutoCommit(DelegatingConnection.java:311)[DEBUG][2012-03-08 14:14:18,502][org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor]Written [Hello world! ] as "text/html" using [org.springframework.http.converter.StringHttpMessageConverter@14a616]
解决方案
我设置mybatis事务自动提交,你提交事务了?
解决方案二:
我的环境是好用的啊,看你代码没看出什么问题 <insert id="insertUserInfo" parameterType="UserInfoEntity"> INSERT INTO user_info ( user_id, user_name, phone_number, birth_date) VALUES ( #{userId}, #{userName}, #{phoneNumber}, #{birthDate}) <selectKey resultType="string" keyProperty="userId" order="AFTER"> SELECT LAST_INSERT_ID() AS userId </selectKey> </insert> UserInfoEntity entity = new UserInfoEntity(); entity.setUserName("aaa"); entity.setPhoneNumber("123123"); entity.setBirthDate("20100101"); dao.insertUserInfo2(entity); System.out.println(entity.getUserId());