I'm inserting a map data into postgresql using mybatis in a springboot program and I want mybatis to return the generated key. The key is auto generated by a sequence and it's name is 'corp_id'. But there comes out a problem that makes me confused. Here is the right way:
<insert id="addOrUpdCorp" parameterType="java.util.Map" useGeneratedKeys="true" keyProperty="corp_id">
insert into t_corporation
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="corp_name != null and corp_name != ''">
corp_name,
</if>
<if test="corp_note != null and corp_note != ''">
corp_note,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="corp_name != null and corp_name != ''">
#{corp_name},
</if>
<if test="corp_note != null and corp_note != ''">
#{corp_note},
</if>
</trim>
</insert>
And here is the wrong way:
<insert id="addOrUpdCorp" parameterType="java.util.Map" useGeneratedKeys="true" keyProperty="corp_id">
<selectKey keyProperty="count" resultType="java.lang.Integer" order="BEFORE">
select count(corp_id) from t_corporation where corp_id = #{corp_id}
</selectKey>
<if test="count > 0">
update t_corporation
<set>
<if test="corp_name != null and corp_name != ''">
corp_name = #{corp_name},
</if>
<if test="corp_note != null and corp_note != ''">
corp_note = #{corp_note},
</if>
</set>
where corp_id = #{corp_id}
</if>
<if test="count == 0">
insert into t_corporation
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="corp_name != null and corp_name != ''">
corp_name,
</if>
<if test="corp_note != null and corp_note != ''">
corp_note,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="corp_name != null and corp_name != ''">
#{corp_name},
</if>
<if test="corp_note != null and corp_note != ''">
#{corp_note},
</if>
</trim>
</if>
</insert>
Why the value of 'corp_id' in the map(after the mapper method returns) is null in the wrong way above? I just want to use update and insert operation together. Besides, the value of 'corp_id' in the map is a real number in right way. Any opinion will be appreciated.