Mybatis 3.1.1 insert function Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Mybatis 3.1.1 insert function without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I work Mybatis 3.1.1 and Postgresql 9.2. I Have Class Student (id, name). I want to insert a new record in the database and get its Id.

my code is

Student s=new Student();
s.setName("javagc");
studentMapper.insertSelective(s);

but s.getId() is null.

Can anybody help me?

Answer

Which database are you using?

It should work on MySql, as mentioned in these questions; Get the id of last inserted record in mybatis, How to obtain last insert id in Oracle using MyBatis?

I could not be able to make it work on my Oracle DB. So I manually took the current value of the sequence after the insertion.

<insert id="insertSelective" parameterType="myObject">
        <selectKey keyProperty="seq_id" order="AFTER" resultType="int">
              SELECT mySchema.myTable_seq.currval FROM dual
        </selectKey>
            INSERT INTO mySchema.myTable . . . 
</insert>
We are here to answer your question about Mybatis 3.1.1 insert function - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji