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);
Can anybody help me?
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>