MyBatis, dynamically generating SQL code in Java: the SQL Builder Class.
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Mon, 23 Feb 2015 22:49:08 +0000 (23:49 +0100)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Mon, 23 Feb 2015 22:49:08 +0000 (23:49 +0100)
MyBatis/MyBatis-Spring/src/main/java/de/example/mybatis/spring/service/ExampleBuilderService.java [new file with mode: 0644]

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 (file)
index 0000000..a910ee2
--- /dev/null
@@ -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();
+       }
+}