O'Reilly logo

Java Persistence with MyBatis 3 by K. Siva Prasad Reddy

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Mapped statements

MyBatis provides various annotations to configure different types of statements such as SELECT, INSERT, UPDATE, and DELETE. Let us see how to configure mapped statements in detail.

@Insert

We can define an INSERT mapped statement using the @Insert annotation.

package com.mybatis3.mappers;
public interface StudentMapper
{
@Insert("INSERT INTO STUDENTS(STUD_ID,NAME,EMAIL,ADDR_ID, PHONE) VALUES(#{studId},#{name},#{email},#{address.addrId},#{phone})")
int insertStudent(Student student);
}

The insertStudent() method with the @Insert annotation returns the number of rows affected by this insert statement.

Autogenerated keys

As discussed in the previous chapter, there can be autogenerated primary key columns. We can use the useGeneratedKeys ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required