Return primary key ID value after insert insertion in MyBatis

Source: Internet
Author: User
Tags postgresql

MyBatis the interface that returns the primary Key ID value mapper After Insert insertion: Method 1: Method 2:

return primary Key ID value after insert insertion in MyBatis

PostgreSQL how to set up a self-adding sequence can point here: Mapper Interface:

Public interface Thesismapper {
    int insertselective (thesis record);
}

In any case, the int in the interface interface returns the number of affected bars (which can be used to identify successful inserts).
And the return of the MyBatis primary key ID value, the use of reflection mechanism, automatically help us backfill into the model entity class. We can use Model.getid () to get the ID value.
(Of course, before we insert, we use GetID () to get the return null)

Two ways:

In the Mapper.xml file, Method 1:

    <insert id= "insertselective" parametertype= "site.gaoyisheng.pojo.Thesis"
             usegeneratedkeys= "true" keyproperty= "id" >
        insert INTO public.xxx
        <trim prefix= "(" suffix= ")" suffixoverrides= "," >
            < If test= "type!= null" >
                type,
            </if>
            <if test= "type!= null" >
                ...,
            </if>
        </trim>
        <trim prefix= "VALUES (" suffix= ")" suffixoverrides= "," >
            <if test= "type!= null ">
                #{type,jdbctype=varchar},
            </if>
            <if test=" type!= null ">
                #{...},
            </ if>
        </trim>
    </insert>

which
usegeneratedkeys= "True" indicates the value of the self-added primary key
keyproperty= "id" is used to identify the primary key colum is ' ID ' Method 2:

    <insert id= "insertselective" parametertype= "site.gaoyisheng.pojo.Thesis"
             usegeneratedkeys= "true" keyproperty= "id" >
        <selectkey resulttype= "int" keyproperty= "id" order= "before" >
             select Currval (" Name of the self-added sequence ") as ID
        </selectKey>

        insert into public.xxx
        <trim prefix=" ("suffix=") "suffixoverrides= "," >
            <if test= "type!= null" >
                type,
            </if>
            <if test= "type!= null" >
                ...,
            </if>
        </trim>
        <trim prefix= "VALUES (" suffix= ")" suffixoverrides= "," >
            < If test= "type!= null" >
                #{type,jdbctype=varchar},
            </if>
            <if test= "type!= null" >
                #{...},
            </if>
        </trim>
    </insert>

which

<selectkey resulttype= "int" keyproperty= "id" order= "before" >
             select Currval ("Name of self-added sequence") as ID
</ Selectkey>

Currval (the name of the "self-added sequence") is a sequence function of the PostgreSQL,
In MySQL or Oracle, functions are used in this way:

MySQL, SQL Server:

  <selectkey resulttype= "_long" keyproperty= "id" order= "after" >
    SELECT @ @IDENTITY as ID
  </selectkey >

Oracle

  <selectkey keyproperty= "id" resulttype= "_long" order= "before" >
    select CAST (RANDOM * 100000 as INTEGER) a from S Ystem. SYSDUMMY1
  </selectKey>

Appendix:

Resources:

https://blog.csdn.net/timo1160139211/article/details/78193816
https://blog.csdn.net/sinat_30474567/article/details/51395247
https://blog.csdn.net/hardworking0323/article/details/51105136

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.