MyBatis’ Wicked Statement Builders


Now here’s one of the most wicked API’s I’ve seen in a while!

MyBatis is well-known as a database abstraction framework on top of JDBC, allowing for externalising SQL into files, loading them at appropriate places in your Java code. For those of you who like this approach, you may be used to statements similar to this one here, taken from the MyBatis documentation:

<select id="selectPerson" parameterType="int" resultType="hashmap">
  SELECT * FROM PERSON WHERE ID = #{id}
</select>

This is MyBatis alright, you think? You haven’t seen everything. MyBatis also has statement builders. Not just any type of statement builders. They make your Java code look like SQL. Check this out:

private String selectPersonSql() {
  BEGIN(); // Clears ThreadLocal variable
  SELECT("P.ID, P.USERNAME, P.PASSWORD, P.FULL_NAME");
  SELECT("P.LAST_NAME, P.CREATED_ON, P.UPDATED_ON");
  FROM("PERSON P");
  FROM("ACCOUNT A");
  INNER_JOIN("DEPARTMENT D on D.ID = P.DEPARTMENT_ID");
  INNER_JOIN("COMPANY C on D.COMPANY_ID = C.ID");
  WHERE("P.ID = A.ID");
  WHERE("P.FIRST_NAME like ?");
  OR();
  WHERE("P.LAST_NAME like ?");
  GROUP_BY("P.ID");
  HAVING("P.LAST_NAME like ?");
  OR();
  HAVING("P.FIRST_NAME like ?");
  ORDER_BY("P.ID");
  ORDER_BY("P.FULL_NAME");
  return SQL();
}

… jeez, that is one of the funniest ideas I’ve ever seen. A ThreadLocal to keep the SQL statement currently being rendered!! And an API just … err … adding Strings to an internal StringBuilder? Wow.

You can decide for yourself whether you like this or not. But you can’t say these guys aren’t creative ;-) Be sure to read the full docs to learn also about how to write INSERT, UPDATE and DELETE:

http://www.mybatis.org/core/statement-builders.html

Tags: , , , , , ,

7 responses to “MyBatis’ Wicked Statement Builders”

  1. Stephen says :

    Oh man. When you first said “wicked”, I thought you meant “wicked” in the slang sense of “good” or “cool”. …then I saw the example and realized, no, no, you really did mean “wicked” as in “evil”. :-)

  2. Michal Kreuzman says :

    I think you are too hard to MyBatis developers. Statement builders are just helper classes and last options if needed (and better then build SQL statements from scratch or write builder on your own). I think first sentence in MyBatis documentation explains it all – “One of the nastiest things a Java developer will ever have to do is embed SQL in Java code.” ;-)

    • lukaseder says :

      Well, if you ask me, I’d really prefer plain SQL embedded in Java over some obscure builder like this…

      • Michal Kreuzman says :

        I’ve always ended with code duplication with plain SQL. So I write my own builder for dynamic queries (filters for example). But I have to say that MyBatis static style looks somehow strange to me also…Anyway thanks for post about MyBatis. It is really small amount of articles about this framework (laudatory or critical).

        • lukaseder says :

          Well, since you ended on my blog, have a look at jOOQ for future dynamic query builders… :-)

          Is that true, that MyBatis doesn’t have a lot of articles about it? I was thinking that MyBatis was quite popular and that it had a lot of traction? Anyway, my own opinion is obviously biased…

Trackbacks / Pingbacks

  1. The Jodd SQL Generator | Java, SQL and jOOQ. - December 2, 2013

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 1,738 other followers

%d bloggers like this: