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

Dynamic SQL

Sometimes we may need to build queries dynamically based on input criteria. MyBatis provides various annotations such as @InsertProvider, @UpdateProvider, @DeleteProvider, and @SelectProvider, which facilitates building dynamic queries and lets MyBatis execute those queries.

Now let us look at an example of how to create a simple SELECT mapped statement using @SelectProvider.

Create the TutorDynaSqlProvider.java class with the findTutorByIdSql() method as follows:

package com.mybatis3.sqlproviders;
import org.apache.ibatis.jdbc.SQL;

public class TutorDynaSqlProvider 
{
public String findTutorByIdSql(int tutorId) 
{
return "SELECT TUTOR_ID AS tutorId, NAME, EMAIL FROM TUTORS WHERE TUTOR_ID="+tutorId;		
}
}

Create a mapped statement in the ...

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