在 iBatis resultMap 中返回常量

发布于 2024-11-25 21:02:19 字数 277 浏览 2 评论 0原文

我设置了一个 resultMap,其中包含许多结果元素。我希望能够设置一个常量作为结果之一。因此,

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

我希望能够确保该名称将作为字符串“Joe”返回。在理想的情况下,我会更改查询以返回此常量,但不幸的是,这对我来说不是一个选择。我扫描了 iBatis dtd,但无法找到合适的属性。我知道我可以迭代从 iBatis 返回的列表,但我更希望能够在 iBatis 映射中执行此操作。谢谢

I have a resultMap set up with a number of result elements 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

乱了心跳 2024-12-02 21:02:19

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

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;
    }
}
峩卟喜欢 2024-12-02 21:02:19

如果无法更改 sql,则尝试更改映射对象的 Setter 方法。

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

If changing sql is not an option, then try to change Setter method for mapped object.

public void setName(String name) {
    this.name = "Joe";
}
无法言说的痛 2024-12-02 21:02:19

在我们的项目中,我们使用以下解决方案来处理过程 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;
    }
}

也许使用相同的配置您就能定义一个常量。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文