From df6b45647ec1472e9198d2375d93ff5b10cb03d6 Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Mon, 23 Feb 2015 23:49:08 +0100 Subject: [PATCH] MyBatis, dynamically generating SQL code in Java: the SQL Builder Class. --- .../spring/service/ExampleBuilderService.java | 49 ++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 MyBatis/MyBatis-Spring/src/main/java/de/example/mybatis/spring/service/ExampleBuilderService.java diff --git a/MyBatis/MyBatis-Spring/src/main/java/de/example/mybatis/spring/service/ExampleBuilderService.java b/MyBatis/MyBatis-Spring/src/main/java/de/example/mybatis/spring/service/ExampleBuilderService.java new file mode 100644 index 0000000..a910ee2 --- /dev/null +++ b/MyBatis/MyBatis-Spring/src/main/java/de/example/mybatis/spring/service/ExampleBuilderService.java @@ -0,0 +1,49 @@ +package de.example.mybatis.spring.service; + +import org.apache.ibatis.jdbc.SQL; + +/** + * Taken from: http://mybatis.github.io/mybatis-3/statement-builders.html + * + */ +public class ExampleBuilderService { + + // Builder / Fluent style + public String insertAdSql() { + return new SQL() + .INSERT_INTO("ad_description") + .VALUES("ad_name, ad_description", "${adName}, ${adDescription}") + .VALUES("ad_mobile_text", "${adMobileText}") + .toString(); + } + + // With conditionals (note the final parameters, required for the anonymous inner class to access them) + public String selectAdLike(final String id, final String adName, final String lastName) { + return new SQL() { + { // New block!!!! + SELECT("AD.id, AD.laguage_id, AD.ad_name, AD.ad_description, AD.ad_mobile_text"); + FROM("ad_description AD"); + if (id != null) { + WHERE("AD.id like ${id}"); // If id is number this query sucks!!! + } + if (adName != null) { + WHERE("AD.ad_name like ${adName}"); + } + if (lastName != null) { + WHERE("AD.ad_description like ${lastName}"); + } + ORDER_BY("AD.ad_name"); + } + }.toString(); + } + + public String updateAdSql() { + return new SQL() { + { // New block!!!! + UPDATE("ad_description"); + SET("ad_name = ${adName}"); + WHERE("id = ${id}"); + } + }.toString(); + } +} -- 2.1.4