最近做mybatis+oracle項目的時候解決添加一條數據並返回所添加數據的主鍵問題
controller層
@RequestMapping("/addplan") public @ResponseBody OnlineAddplanWithBLOBs insertOnlineAddplan(OnlineAddplanWithBLOBs plan) throws Exception{
//plan是添加的數據,planid為數據主鍵,此時對象中主鍵為null int n = service.insertOnlineAddplan(plan);
//獲取對象主鍵 System.out.println("返回的主鍵值是"+plan.getPlanid()); return plan; }
在mapperx.xml中
<insert id="insert" parameterType="com.online.pojo.OnlineAddplanWithBLOBs" > //獲取序列值,並賦值到對象的planid字段 <selectKey keyProperty="planid" resultType="DECIMAL" order="BEFORE"> select online_sequence.nextval from dual </selectKey> insert into ONLINE_ADDPLAN (PLANID, COMPLETETIME, PERSON, OPERATION, USERNAME, EVENTNODE, WORKPLAN, CHENGGUOMIAOSHU)
//獲取上面對象中planid字段的值 values (#{planid,jdbcType=DECIMAL}, #{completetime,jdbcType=TIMESTAMP}, #{person,jdbcType=VARCHAR}, #{operation,jdbcType=VARCHAR}, #{username,jdbcType=VARCHAR}, #{eventnode,jdbcType=CLOB}, #{workplan,jdbcType=CLOB}, #{chengguomiaoshu,jdbcType=CLOB}) </insert>