標籤:
最近做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>
mybatis+oracle添加一條資料並返回所添加資料的主鍵問題