2011-07-21 59 views
0

我有一個resultMap,其中包含許多結果元素。我希望能夠把恆定作爲結果之一。所以,而不是在iBatis resultMap中返回一個常量

<result property="name" column="Name"/> 

我希望能夠確保名稱會返回字符串'喬'。在一個理想的世界中,我會改變查詢來返回這個常量,但不幸的是,這不是我的選擇。我掃描了iBatis dtd,無法找到合適的屬性。我知道我可以迭代從iBatis返回的列表,但我更願意能夠在iBatis地圖中執行此操作。謝謝

回答

0

如果更改sql不是一個選項,然後嘗試更改映射對象的Setter方法。

public void setName(String name) { 
    this.name = "Joe"; 
} 
0

在我們的項目中,我們使用下面的解決方案來獲得過程數據庫值,例如,布爾值。

ABC-sqlmaps.xml

<resultMap id="resultMapABC" class="com.abc.dto.ABC"> 
     ... 
     <result property="isA" column="is_a" typeHandler="YesNoTypeHandler"/> 
     ... 
</resultMap> 

ibatis.xml

<sqlMapConfig> 
    ... 
    <typeAlias alias="YesNoTypeHandler" type="com.abc.dao.sqlmap.YesNoTypeHandler"/> 
    <typeHandler javaType="boolean" jdbcType="BOOLCHAR" callback="YesNoTypeHandler"/> 
    ... 
</sqlMapConfig> 

YesNoTypeHandler.java

package com.abc.dao.sqlmap; 

import com.ibatis.sqlmap.client.extensions.ParameterSetter; 
import com.ibatis.sqlmap.client.extensions.ResultGetter; 
import com.ibatis.sqlmap.client.extensions.TypeHandlerCallback; 

import java.sql.SQLException; 

public class YesNoTypeHandler implements TypeHandlerCallback { 
    /** 
    * Sets a boolean parameter as a corresponding string value ("Y" or "N"). 
    * 
    * @param setter The <code>ParameterSetter</code> for an object being setted 
    * @param parameter An object to set 
    * @throws SQLException is expected exception. 
    */ 
    public void setParameter(ParameterSetter setter, Object parameter) 
      throws SQLException { 
     Boolean value = (Boolean) parameter; 

     if (value == null) { 
      value = Boolean.FALSE; 
     } 

     setter.setString(value ? "Y" : "N"); 
    } 

    /** 
    * Performs the string "Y"/"N" to the boolean type and gets the result as a boolean object. 
    * 
    * @param getter The <code>ResultGetter</code> 
    * @return object. 
    * @throws SQLException is expected exception. 
    */ 
    public Object getResult(ResultGetter getter) throws SQLException { 
     String value = getter.getString(); 

     return "Y".equalsIgnoreCase(value); 
    } 

    /** 
    * Returns the value of a string as an <code>Object</code>. 
    * 
    * @param s string. 
    * @return YesNoTypeHandler. 
    */ 
    public Object valueOf(String s) { 
     return s; 
    } 
} 

可能使用相同的配置,你就可以定義一個常量。

2

YesNoTypeHandler.java與MyBatis的3兼容:

ABC-sqlmaps.xml

<resultMap id="resultMapABC" class="com.abc.dto.ABC"> 
     ... 
     <result property="isA" column="is_a" typeHandler="YesNoTypeHandler"/> 
     ... 
</resultMap> 

ibatis.xml

<sqlMapConfig> 
    ... 
    <typeAlias alias="YesNoTypeHandler" type="com.abc.dao.sqlmap.YesNoTypeHandler"/> 
    <typeHandler javaType="boolean" jdbcType="BOOLCHAR" callback="YesNoTypeHandler"/> 
    ... 
</sqlMapConfig> 




import java.sql.CallableStatement; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 

import org.apache.ibatis.type.JdbcType; 
import org.apache.ibatis.type.TypeHandler; 

public class YesNoTypeHandler implements TypeHandler { 

    @Override 
    public void setParameter(PreparedStatement paramPreparedStatement, int paramInt, Object paramObject, JdbcType paramJdbcType) throws SQLException { 
     if (paramObject == null) { 
      paramPreparedStatement.setString(paramInt, "N"); 
     } 
     else { 
      Boolean value = (Boolean) paramObject; 

      paramPreparedStatement.setString(paramInt, value ? "Y" : "N"); 
     } 
    } 


    @Override 
    public Object getResult(ResultSet getter, String columnLabel) throws SQLException { 
     String value = getter.getString(columnLabel); 
     if (getter.wasNull()) { return false; } 
     return "Y".equalsIgnoreCase(value); 

    } 

    @Override 
    public Object getResult(CallableStatement cs, int columnNb) throws SQLException { 
     String value = cs.getString(columnNb); 
     if (cs.wasNull()) { return false; } 
     Boolean BoolValue = "Y".equalsIgnoreCase(value); 
     return BoolValue; 
    } 
}