mybatis如何获取oracle新插入数据记录的主键?

来源:互联网 发布:怎样在淘宝分期付款 编辑:程序博客网 时间:2024/05/20 21:23

第一    用序列

  <insert id="insertSelective" parameterType="com.zehin.vpaas.base.domain.SfyHazardAnalysis"><selectKey resultType="java.lang.Integer" order="BEFORE" keyProperty="hazardId">SELECT SEQUENCE_1.NEXTVAL FROM DUAL</selectKey>insert into SFY_HAZARD_ANALYSIS<trim prefix="(" suffix=")" suffixOverrides=",">HAZARD_ID,<if test="hazardTime != null"> HAZARD_TIME,</if><if test="hazardTitle != null"> HAZARD_TITLE,</if><if test="hazardMeasure != null"> HAZARD_MEASURE,</if><if test="buildId != null"> BUILD_ID,</if></trim><trim prefix=" values(" suffix=")" suffixOverrides=",">#{hazardId,jdbcType=INTEGER},<if test="hazardTime != null">#{hazardTime,jdbcType=VARCHAR},</if><if test="hazardTitle != null"> #{hazardTitle,jdbcType=VARCHAR},</if><if test="hazardMeasure != null"> #{hazardMeasure,jdbcType=VARCHAR},</if><if test="buildId != null"> #{buildId,jdbcType= INTEGER},</if></trim></insert>


上面就能实现,当你在java中往数据库中插入一个对象,即一条数据时,可以获取主键值,如下所示

@RequestMapping(value = "/addConstruManageInfo")@ResponseBodypublic Result addConstruManageInfo(SfyBuild sfyBuild){System.out.println("查看传入的值====================================="+JSONObject.fromObject(sfyBuild).toString());System.out.println("===========================================================输入前主键值"+sfyBuild.getBuildId());//NULLint rows = sfyConstruManageService.insert(sfyBuild);System.out.println("===========================================================输入后主键值"+sfyBuild.getBuildId());//数值

第二种  guid

<insert id="insertUser" parameterType="com.danny.mybatis.po.User"> <selectKey keyProperty="userId" order="BEFORE" resultType="java.lang.Integer"> select SYS_GUID() as userId from DUAL </selectKey> insert into T_USER(userId,userName,birthday,sex,address) values (#{userId},#{userName},#{birthday},#{sex},#{address}) </insert>





以下是mysql中获取插入数据主键的配置方法

<insert id="add" parameterType="EStudent" useGeneratedKeys="true" keyProperty="id">  insert into TStudent(name, age) values(#{name}, #{age})</insert>


1 0
原创粉丝点击