I have a resultMap set up with a number of result element开发者_如何学Pythons in it. I would like to be able to set a constant as one of the results. So instead of
<result property="name" column="Name"/>
I'd like to be able to make sure that name would come back as the string 'Joe'. In an ideal world I'd have the query changed to return this constant but unfortunately that's not an option for me. I've scanned over the iBatis dtd and was unable to find a suitable attribute. I know I could just iterate over the list returned from iBatis but i'd prefer to be able to do it in the iBatis map. Thanks
YesNoTypeHandler.java compatible with 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;
}
}
If changing sql is not an option, then try to change Setter method for mapped object.
public void setName(String name) {
this.name = "Joe";
}
In our project we use the solution below for process DB values, e.g. Boolean values.
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;
}
}
May be using the same configuration you will be able to define a constants.
精彩评论