[Java][ibatis]Ibatis TypeHandler使用總結

來源:互聯網
上載者:User
Ibatis TypeHandler使用總結ibatis中有一個TypeHandler(準確的說應該是TypeHandlerCallback), 這個介面一般用的比較少, google一下, 大部分就是用來將資料庫中的值與java的枚舉值或者clob與字串之間的轉換. 最近本人也用到了這個東東. 不過我們使用的是將儲存在資料庫中以一定分隔字元串連的字串轉換成List類型. 開始不知道有TypeHandler這個東東, 於是在JavaBean中定義了兩個屬性, 一個string類型的, 一個List類型的,
然後內部之間轉換, 這種做法有一個弊端, 就是實際上在對一個屬性操作的時候, 會有兩個介面, 一方面給使用方造成了困惑, 另一個維護起來也不方便, 於是將轉換過程完全封裝, 對外提供提一個提供者, 成了一個必要的選擇.

先來看看TypeHandlerCallback的定義: Java代碼  
  1. public interface TypeHandlerCallback {   
  2.   public void setParameter(ParameterSetter setter, Object parameter)   
  3.       throws SQLException;   
  4.   
  5.   public Object getResult(ResultGetter getter)   
  6.       throws SQLException;   
  7.   
  8.   public Object valueOf(String s);  
public interface TypeHandlerCallback {  public void setParameter(ParameterSetter setter, Object parameter)      throws SQLException;  public Object getResult(ResultGetter getter)      throws SQLException;  public Object valueOf(String s);

代碼很好理解, 而且為了說明如何使用, 作者不惜在注視中給出了一個example.
代碼很好理解, setParameter()方法主要是給PrepareStatement賦值. 因此是在insert, update, delete這些操作的時候, 指定傳遞參數用的.

getResult()方法用來將ResultSet結果集中的內容轉換到JavaBean中對應的屬性.
valueOf()主要是用來當沒有指定的值的時候, 指定預設值的. 主要是ResultSet到JavaBean之間的轉換的時候會用到. 最好不要返回null值, 它跟nullValue相關.

下面是一個將資料庫中";"分隔的字串與DO中的list對象之間的轉換的實現: Java代碼  

  1. public class PropertiesTypeHandlerCallback implements TypeHandlerCallback {   
  2.     private static final String LIST_SPLIT_FLAG = ";";   
  3.   
  4.     public Object getResult(ResultGetter getter) throws SQLException {   
  5.         String properties = getter.getString();   
  6.         return CollectionUtils.stringToList(properties, LIST_SPLIT_FLAG, new  
  7.                 StringConvertor<Property>() {   
  8.             public Property convert(String str) {   
  9.                 return Property.valueOf(str);   
  10.             }   
  11.         });   
  12.     }   
  13.   
  14.     @SuppressWarnings("unchecked")   
  15.     public void setParameter(ParameterSetter setter, Object parameter) throws SQLException {   
  16.         List<String> propertyList = (List<String>) parameter;   
  17.         setter.setString(CollectionUtils.listToString(propertyList, LIST_SPLIT_FLAG));   
  18.     }   
  19.   
  20.     public Object valueOf(String s) {   
  21.         return Collections.EMPTY_LIST;   
  22.     }   
  23. }  
public class PropertiesTypeHandlerCallback implements TypeHandlerCallback {    private static final String LIST_SPLIT_FLAG = ";";    public Object getResult(ResultGetter getter) throws SQLException {        String properties = getter.getString();        return CollectionUtils.stringToList(properties, LIST_SPLIT_FLAG, new                StringConvertor<Property>() {            public Property convert(String str) {                return Property.valueOf(str);            }        });    }    @SuppressWarnings("unchecked")    public void setParameter(ParameterSetter setter, Object parameter) throws SQLException {        List<String> propertyList = (List<String>) parameter;        setter.setString(CollectionUtils.listToString(propertyList, LIST_SPLIT_FLAG));    }    public Object valueOf(String s) {        return Collections.EMPTY_LIST;    }}

接下來是sqlmap的對應檔中進行配置.
針對sql返回結果的轉換, 需要在對應的resultMap中指定一下, 比如這樣寫: Xml代碼  

  1. <result property="propertyList" column="PROPERTIES"  typeHandler="com.mysoft.dao.ibatis.support.PropertiesTypeHandlerCallback"/>  
<result property="propertyList" column="PROPERTIES"  typeHandler="com.mysoft.dao.ibatis.support.PropertiesTypeHandlerCallback"/>
針對賦值的參數的設定, 有兩種做法, 一種是在parameterMap中的parameter標籤中的設定typeHandler屬性為對應的callback, 這種做法有一種不好的地方, 就是不同的賦值map, 需要單獨定義, 這個反而增加了工作量. 而且使用parameterMap之後, sql語句中的原來在##中指定賦值名稱需要改成?, 賦值遵循parameterMap中的定義順序, 這種做法既繁瑣, 可維護性也不好, 因此推薦採用inlineParameter的做法, 比如這樣寫:
Sql代碼  
  1. #propertyList,handler=com.taobao.item.dao.ibatis.support.ItemVerticalPropertiesTypeHandlerCallback#  
#propertyList,handler=com.taobao.item.dao.ibatis.support.ItemVerticalPropertiesTypeHandlerCallback#

對於TypeHandler的配置, 還有一種做法, 就是在sqlmap中對某一種類型, 使用typeHandler標籤進行定義, 比如這樣寫: Java代碼  

  1. <typeHandler javaType="" callback=""/>  
<typeHandler javaType="" callback=""/>
注意這個需要定義在sqlMapConfig標籤下.

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.