Java程序員面對的最痛苦的事情之一就是在Java代碼中嵌入SQL語句。這麼來作一般是因爲SQL語句須要動態來生成-不然能夠將它們放到外部文件或者存儲過程當中。正如你已經看到的那樣,MyBatis在它的XML映射特性中有一個強大的動態SQL生成方案。但有時在Java代碼內部建立SQL語句也是必要的。此時,MyBatis有另一個特性能夠幫到你,在減小典型的加號,引號,新行,格式化問題和嵌入條件來處理多餘的逗號或 AND 鏈接詞以前。事實上,在Java代碼中來動態生成SQL代碼就是一場噩夢。例如: java
String sql = "SELECT P.ID, P.USERNAME, P.PASSWORD, P.FULL_NAME, " "P.LAST_NAME,P.CREATED_ON, P.UPDATED_ON " + "FROM PERSON P, 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 AND P.FIRST_NAME like ?) " + "OR (P.LAST_NAME like ?) " + "GROUP BY P.ID " + "HAVING (P.LAST_NAME like ?) " + "OR (P.FIRST_NAME like ?) " + "ORDER BY P.ID, P.FULL_NAME";
MyBatis 3提供了方便的工具類來幫助解決該問題。使用SQL類,簡單地建立一個實例來調用方法生成SQL語句。上面示例中的問題就像重寫SQL類那樣: 程序員
private String selectPersonSql() { return new SQL() {{ 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"); }}.toString(); }
該例中有什麼特殊之處?當你仔細看時,那不用擔憂偶然間重複出現的"AND"關鍵字,或者在"WHERE"和"AND"之間的選擇,抑或什麼都不選。該SQL類很是注意"WHERE"應該出如今何處,哪裏又應該使用"AND",還有全部的字符串連接。 web
這裏給出一些示例:sql
// Anonymous inner class public String deletePersonSql() { return new SQL() {{ DELETE_FROM("PERSON"); WHERE("ID = #{id}"); }}.toString(); } // Builder / Fluent style public String insertPersonSql() { String sql = new SQL() .INSERT_INTO("PERSON") .VALUES("ID, FIRST_NAME", "#{id}, #{firstName}") .VALUES("LAST_NAME", "#{lastName}") .toString(); return sql; } // With conditionals (note the final parameters, required for the anonymous inner class to access them) public String selectPersonLike(final String id, final String firstName, final String lastName) { return new SQL() {{ SELECT("P.ID, P.USERNAME, P.PASSWORD, P.FIRST_NAME, P.LAST_NAME"); FROM("PERSON P"); if (id != null) { WHERE("P.ID like #{id}"); } if (firstName != null) { WHERE("P.FIRST_NAME like #{firstName}"); } if (lastName != null) { WHERE("P.LAST_NAME like #{lastName}"); } ORDER_BY("P.LAST_NAME"); }}.toString(); } public String deletePersonSql() { return new SQL() {{ DELETE_FROM("PERSON"); WHERE("ID = #{id}"); }}.toString(); } public String insertPersonSql() { return new SQL() {{ INSERT_INTO("PERSON"); VALUES("ID, FIRST_NAME", "#{id}, #{firstName}"); VALUES("LAST_NAME"