Mybatis中insert返回主键id
Mapper.xml中修改insert,添加<selectKey></selectKey>
<insert id="insert" parameterType="com.test.TestObjModel">
<selectKey resultType="java.lang.Long" order="AFTER" keyProperty="id">
SELECT LAST_INSERT_ID()
</selectKey>
insert into d_test (id, name)
values (#{id,jdbcType=BIGINT}, #{name,jdbcType=VARCHAR})
</insert>
Dao.java中调用insert时testObjModel对象中的ID就有值了。
int row = testDao.insert(testObjModel);