Re: Set parameters by name on CallableStatement

97 views
Skip to first unread message

Ikchan Sim

unread,
Sep 27, 2012, 9:30:50 PM9/27/12
to mybati...@googlegroups.com
DefaultTypeHandler  ---> BaseTypeHandler ---> CustomTypeHandler
 
You Accessed by setNonNullParameter( ) method()
follow source code:
 
 
--------------------------------------------------------------------------------------------------------------------------------------------------------------------

package chapter3.org.mybatis.type;

 

import java.io.StringReader;

import java.sql.CallableStatement;

import java.sql.Clob;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import org.apache.ibatis.type.BaseTypeHandler;

import org.apache.ibatis.type.JdbcType;

 

public class CustomTypeHandler extends BaseTypeHandler<String> {

    public void setNonNullParameter(CallableStatement callableStatement, String columnName,

            String parameter, JdbcType jdbcType) throws SQLException {

        // do something..........
        callableStatement.setString(columnName, parameter);
    }

 

    @Override

    public void setNonNullParameter(PreparedStatement preparedStatement, int i, String parameter,

            JdbcType jdbcType) throws SQLException {

        StringReader reader = new StringReader(parameter);

        preparedStatement.setCharacterStream(i, reader, parameter.length());

    }

 

    @Override

    public String getNullableResult(ResultSet resultSet, String columnName) throws SQLException {

        String value = "";

        Clob clob = resultSet.getClob(columnName);

        if (clob != null) {

            int size = (int) clob.length();

            value = clob.getSubString(1, size);

        }

        return value;

    }

 

    @Override

    public String getNullableResult(ResultSet resultSet, int columnIndex) throws SQLException {

        String value = "";

        Clob clob = resultSet.getClob(columnIndex);

        if (clob != null) {

            int size = (int) clob.length();

            value = clob.getSubString(1, size);

        }

        return value;

    }

 

    @Override

    public String getNullableResult(CallableStatement callableStatement, int columnIndex)

            throws SQLException {

        String value = "";

        Clob clob = callableStatement.getClob(columnIndex);

        if (clob != null) {

            int size = (int) clob.length();

            value = clob.getSubString(1, size);

        }

        return value;

    }

}

 
config-mybatis.xml (mybatis config file)
---------------------------------------------------------------------------------------------------------------------------------------------
    <typeAliases>
        <typeAlias alias="writer" type="chapter3.org.mybatis.domain.Writer" />
        <typeAlias alias="customTypeHandler" type="chapter3.org.mybatis.type.CustomTypeHandler" />
    </typeAliases>
---------------------------------------------------------------------------------------------------------------------------------------------
 
WriterMapper.xml (mapper file)
---------------------------------------------------------------------------------------------------------------------------------------------
    <parameterMap id="writerParameterMap" type="writer" >
        <parameter property="no" />
        <parameter property="id"  />
        <parameter property="name" />
        <parameter property="note" jdbcType="CLOB" javaType="string" typeHandler="customTypeHandler" />
        <parameter property="status" />
    </parameterMap>
 
    <select id="selectWriter" parameterMap="writerParameterMap" resultType="writer">
        SELECT * FROM WRITER
        <where>
            <if test="no != null and no > 0">NO = #{no}</if>
        </where>
 </select>
</mapper>
---------------------------------------------------------------------------------------------------------------------------------------------
 
 
Good Luck...

Ikchan Sim

unread,
Sep 27, 2012, 9:59:21 PM9/27/12
to mybati...@googlegroups.com
TypeHandler.png

Alex Beaupré

unread,
Sep 28, 2012, 9:15:17 AM9/28/12
to mybati...@googlegroups.com
Thanks Ikchan for the quick reply, I will try that.

However does this means that if I want all my parameters to be set by name I will have to specify a typeHandler for all parameters ?

Thanks,
Alex

Alex Beaupré

unread,
Sep 28, 2012, 10:30:55 AM9/28/12
to mybati...@googlegroups.com

Also, at this point I need the name of the property in order to do the correct mapping.
I think mapping parameters by name is simply not supported in mybatis.

It would be great if there would be an attribute in the parameter of a parameterMap, say "name", that would be considered when using CallableStatements.


Reply all
Reply to author
Forward
0 new messages