2011-07-21 27 views
0

resultMapに多数のresult要素が設定されています。私は結果の1つとして定数を設定できるようにしたいと思います。だから代わりにiBatis resultMapで定数を返す

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

私は文字列 'ジョー'として戻って来ることを確認したいと思います。理想的な世界では、この定数を返すようにクエリが変更されていましたが、残念ながらそれは私の選択肢ではありません。私はiBatis dtdをスキャンして、適切な属性を見つけることができませんでした。私はiBatisから返されたリストを反復することができますが、私はiBatisマップでそれを行うことができると思っています。ありがとう

答えて

0

sqlの変更がオプションでない場合、マップされたオブジェクトのSetterメソッドを変更しようとします。

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

私たちのプロジェクトでは、以下のソリューションをプロセスDB値として使用します。ブール値。

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; 
    } 
} 

は、次のことができるようになり、同じ構成を使用している可能性があり定数を定義する。 MyBatisの3との互換性

2

YesNoTypeHandler.java:

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; 
    } 
} 
関連する問題