12/3/13 IBatis (MyBatis): Working with Dynamic Queries (SQL) : Loiane Groner loianegroner.com/2011/03/ibatis-mybatis-working-with-dynamic-queries-sql/ 1/12 Loiane Groner My development notes Home Contact About Me ExtJS Plugins PagingToolbarResizer Enter Search Terms search Welcome to Loiane Groner IBatis (MyBatis): Working with Dynamic Queries (SQL) March 22, 2011 | By Loiane | Add a Comment This tutorial will walk you through how to setup iBatis (MyBatis ) in a simple Java project and will present how to work with dynamic queries (SQL). Pre-Requisites For this tutorial I am using: IDE: Eclipse (you can use your favorite one) DataBase: MySQL Libs/jars: Mybatis , MySQL conector and JUnit (for testing) This is how your project should look like:
12
Embed
IBatis (MyBatis)_ Working With Dynamic Queries (SQL) _ Loiane Groner
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
12/3/13 IBatis (MyBatis): Working with Dynamic Queries (SQL) : Loiane Groner
The goal of this tutorial is to demonstrate how to retrieve the article information from databaseusing dynamic sql to filter the data.
2 – Article Mapper – XML
One of the most powerful features of MyBatis has always been its DynamicSQL capabilities. If you have any experience with JDBC or any similar
framework, you understand how painful it is to conditionally concatenate stringsof SQL together, making sure not to forget spaces or to omit a comma at theend of a list of columns. Dynamic SQL can be downright painful to deal with.
While working with Dynamic SQL will never be a party, MyBatis certainlyimproves the situation with a powerful Dynamic SQL language that can be used
within any mapped SQL statement.
The Dynamic SQL elements should be familiar to anyone who has used JSTL orany similar XML based text processors. In previous versions of MyBatis, there
were a lot of elements to know and understand. MyBatis 3 greatly improvesupon this, and now there are less than half of those elements to work with.
MyBatis employs powerful OGNL based expressions to eliminate most of theother elements.
ifchoose (when, otherwise)
trim (where, set)foreach
Let’s explain each one with examples.
1 – First scenario: we want to retrieve all the articles from database with an optional filter:title. In other words, if user specify an article title, we are going to retrieve the articles that
match with the title, otherwise we are going to retrieve all the articles from database. So weare going to implement a condition (if):
123456789
<select id="selectArticleByTitle" parameterType="com.loiane.model.Article" resultType="Article"> SELECT id, title, author FROM article WHERE id_status = 1 <if test="title != null"> AND title LIKE #{title} </if></select>
?
Related Searches:
Flash Video
PHP Content
Management
Systems
David Mercer
Flash CS3
Best Open Source
CMS
Object-Oriented
Programming With
PHP5
Free CMS Software
Flash Components
CMS Systems
CMS Software
?
12/3/13 IBatis (MyBatis): Working with Dynamic Queries (SQL) : Loiane Groner
2 – Second scenario: Now we have two optional filters: article title and author. The user canspecify both, none or only one filter. So we are going to implement two conditions:
3 – Third scenario: Now we want to give the user only one option: the user will have tospecify only one of the following filters: title, author or retrieve all the articles from iBatis
category. So we are going to use a Choose element:
4 – Fourth scenario: take a look at all three statements above. They all have a condition in
common: WHERE id_status = 1. It means we are already filtering the active articles Let’s
remove this condition to make it more interesting.
123456789101112
<select id="selectArticleByTitleAndAuthor" parameterType resultType="Article"> SELECT id, title, author FROM article WHERE id_status = 1 <if test="title != null"> AND title LIKE #{title} </if> <if test="author != null"> AND author LIKE #{author} </if></select>
1234567891011121314151617
<select id="selectArticleByTitleOrAuthorOrCategory" parameterType="com.loiane.model.Article" resultType= SELECT id, title, author FROM article WHERE id_status = 1 <choose> <when test="title != null"> AND title LIKE #{title} </when> <when test="author != null"> AND author LIKE #{author} </when> <otherwise> AND id_category = 3 </otherwise> </choose></select>
1234567891011
<select id="selectArticleByTitleAndAuthor" parameterType resultType="Article"> SELECT id, title, author FROM article WHERE <if test="title != null"> title LIKE #{title} </if> <if test="author != null"> AND author LIKE #{author} </if>
?
?
?
12/3/13 IBatis (MyBatis): Working with Dynamic Queries (SQL) : Loiane Groner