1.首先是定义一个sequence,具体的创建和参数不列出。参考相关SQL语法:
drop sequence S_service_close_info;
create sequence S_service_close_info start with 1;
sequence名称: S_service_close_info
2.代码中需要判定插入或者更新是否成功,具体代码如下。
int val=eventService.insertCloseEntity(closeEntity,eventsEntity);
if(val > 0){
logger.debug("插入手动创建事件关闭信息成功");
}else{
logger.debug("插入手动创建事件关闭信息失败");
}
在Service层实现方法如下:
public Integer insertCloseEntity(CloseEntity closeEntity) throws Exception {
Object val=(Object)this.getSqlMapClientTemplate().insert("insertCloseEntity",closeEntity);
return ((BigDecimal)val).intValue();
}
3.ibatis中的配置如下:
<insert id="insertCloseEntity" parameterClass="closeEntity">
insert into service_close_info(EVENT_ID,CLOSE_MAN,CLOSE_TIME,CLOSE_TYPE,CLOSE_REASON)
values(#eventId#,#closeMan#,#closeTime#,#closeType#,#closeReason#)
<selectKey>
SELECT S_service_close_info.currval as id FROM DUAL
</selectKey>
</insert>
4.这里解释一下currval ,nextvar ,简单说明一下,在没有执行过 S_service_close_info.nextvar 而执行 S_service_close_info.currval 是报错的。currval 是当前会话有效。用的时候需要注意,详细了解可以参考相关资料,currval,nextvar 。