我在resultMap中设置了许多结果元素。我希望能够将常量设置为结果之一。而不是
<result property="name" column="Name"/>
我希望能够确保名称会以字符串'Joe'的形式返回。在理想的世界中,我将查询更改为返回此常量,但不幸的是,这不是我的选择。我已经扫描了iBatis dtd,但无法找到合适的属性。我知道我可以迭代从iBatis返回的列表,但我更愿意能够在iBatis地图中执行此操作。感谢
答案 0 :(得分: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;
}
}
答案 1 :(得分:0)
如果不能更改sql,则尝试更改映射对象的Setter方法。
public void setName(String name) {
this.name = "Joe";
}
答案 2 :(得分: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;
}
}
可以使用相同的配置来定义常量。