1. 程式人生 > >MyBatis+MySQL map返回插入的主鍵ID

MyBatis+MySQL map返回插入的主鍵ID

Mapper.xml

<!-- 插入一條資料 -->
	<insert id="insert"  parameterType="java.util.Map" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
		<selectKey resultType="int" order="AFTER" keyProperty="id"> 
	        SELECT LAST_INSERT_ID() as id
	    </selectKey>
		  
		insert into sys_organization_info (name,level,parent_id,create_time,name_path) 
		values (#{name,jdbcType=VARCHAR},
		#{level,jdbcType=TINYINT},
		#{parent_id,jdbcType=NUMERIC},
		#{create_time,jdbcType=DATE},
		#{name_path,jdbcType=VARCHAR})
	</insert>

Java service
sysOrganizationDao.insert(map);
int i = Integer.parseInt(map.get("id").toString());