Java-ORM-MyBatis:MyBatis 3 | SQL 語句構建器類
阿新 • • 發佈:2019-01-07
ylbtech-Java-ORM-MyBatis:MyBatis 3 | SQL 語句構建器類 |
1.返回頂部 |
SQL語句構建器類
問題
Java程式設計師面對的最痛苦的事情之一就是在Java程式碼中嵌入SQL語句。這麼來做通常是由於SQL語句需要動態來生成-否則可以將它們放到外部檔案或者儲存過程中。正如你已經看到的那樣,MyBatis在它的XML對映特性中有一個強大的動態SQL生成方案。但有時在Java程式碼內部建立SQL語句也是必要的。此時,MyBatis有另外一個特性可以幫到你,在減少典型的加號,引號,新行,格式化問題和嵌入條件來處理多餘的逗號或 AND 連線詞之前
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";
The Solution
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",還有所有的字串連結。
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(