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, static SQL queries may not be sufficient for application requirements. We may have to build queries dynamically, based on some criteria.

For example, in web applications there could be search screens that provide one or more input options and perform searches based on the chosen criteria. While implementing this kind of search functionality, we may need to build a dynamic query based on the selected options. If the user provides any value for input criteria, we'll need to add that field in the WHERE clause of the query.

MyBatis provides first-class support for building dynamic SQL queries using elements such as <if>, <choose>, <where>, <foreach>, and <trim>.

The If condition

The <if> element can be used to conditionally embed ...

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