how to get the generate key using mybatis in springboot program?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















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.










share|improve this question

























  • You are trying to implement the merge a.k.a. upsert. It is not easy to make it right. You might want to check how to do that.

    – Roman Konoval
    Jan 3 at 9:44











  • When you say that cord_id is null do you mean that it is null in the map after the mapper method returns? Is it null for both cases (insert and update) in the wrong query?

    – Roman Konoval
    Jan 3 at 9:46











  • Yes, i mean it is null in the map after the mapper method returns.Actually the data is already in the database.

    – SixSixSix
    Jan 3 at 9:51


















1















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.










share|improve this question

























  • You are trying to implement the merge a.k.a. upsert. It is not easy to make it right. You might want to check how to do that.

    – Roman Konoval
    Jan 3 at 9:44











  • When you say that cord_id is null do you mean that it is null in the map after the mapper method returns? Is it null for both cases (insert and update) in the wrong query?

    – Roman Konoval
    Jan 3 at 9:46











  • Yes, i mean it is null in the map after the mapper method returns.Actually the data is already in the database.

    – SixSixSix
    Jan 3 at 9:51














1












1








1








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.










share|improve this question
















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.







java postgresql spring-boot mybatis






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 4 at 1:28







SixSixSix

















asked Jan 3 at 4:05









SixSixSixSixSixSix

106




106













  • You are trying to implement the merge a.k.a. upsert. It is not easy to make it right. You might want to check how to do that.

    – Roman Konoval
    Jan 3 at 9:44











  • When you say that cord_id is null do you mean that it is null in the map after the mapper method returns? Is it null for both cases (insert and update) in the wrong query?

    – Roman Konoval
    Jan 3 at 9:46











  • Yes, i mean it is null in the map after the mapper method returns.Actually the data is already in the database.

    – SixSixSix
    Jan 3 at 9:51



















  • You are trying to implement the merge a.k.a. upsert. It is not easy to make it right. You might want to check how to do that.

    – Roman Konoval
    Jan 3 at 9:44











  • When you say that cord_id is null do you mean that it is null in the map after the mapper method returns? Is it null for both cases (insert and update) in the wrong query?

    – Roman Konoval
    Jan 3 at 9:46











  • Yes, i mean it is null in the map after the mapper method returns.Actually the data is already in the database.

    – SixSixSix
    Jan 3 at 9:51

















You are trying to implement the merge a.k.a. upsert. It is not easy to make it right. You might want to check how to do that.

– Roman Konoval
Jan 3 at 9:44





You are trying to implement the merge a.k.a. upsert. It is not easy to make it right. You might want to check how to do that.

– Roman Konoval
Jan 3 at 9:44













When you say that cord_id is null do you mean that it is null in the map after the mapper method returns? Is it null for both cases (insert and update) in the wrong query?

– Roman Konoval
Jan 3 at 9:46





When you say that cord_id is null do you mean that it is null in the map after the mapper method returns? Is it null for both cases (insert and update) in the wrong query?

– Roman Konoval
Jan 3 at 9:46













Yes, i mean it is null in the map after the mapper method returns.Actually the data is already in the database.

– SixSixSix
Jan 3 at 9:51





Yes, i mean it is null in the map after the mapper method returns.Actually the data is already in the database.

– SixSixSix
Jan 3 at 9:51












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54016212%2fhow-to-get-the-generate-key-using-mybatis-in-springboot-program%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54016212%2fhow-to-get-the-generate-key-using-mybatis-in-springboot-program%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

SQL update select statement

'app-layout' is not a known element: how to share Component with different Modules