mybatis+oracle添加一条数据并返回所添加数据的主键问题

时间:2024-05-12 00:06:56

最近做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>