0

Trying to use the typehandler for an insert statement in mybatis, but it is not working. I am using mybatis-spring 1.2.1, mybatis 3.2.3. But I am getting an error message saying that parameter 2 is not set. Here is the code,

mybatis config file:

    <configuration>
        <typeAliases>
            .......
            .........
            <typeAlias type="org.test.util.TSTypeHandler" alias="TSTypeHandler"/>
        </typeAliases>
        <typeHandlers>
           .......
    <typeHandler handler="TSTypeHandler" javaType="java.lang.String" jdbcType="TIMESTAMP"/>
    </typeHandlers>
    <mappers>
        ......
    </mappers>
</configuration>

Mapper xml:

<insert id="saveMyOutput">
        INSERT INTO TEST.MY_OUTPUT (
        YEAR, 
        RUN_TMS,
        PRODUCT
       ) 
        VALUES 
        <foreach item="element" index="index" collection="mOutput"
            open="(" separator="),(" close=")">
            #{element.year}, 
            #{element.runTS, typeHandler=TSTypeHandler},
            #{element.product}
        </foreach>
    </insert>
SiddP
  • 1,603
  • 2
  • 14
  • 36
aryanRaj_kary
  • 503
  • 2
  • 7
  • 28

1 Answers1

0

Type handlers declared in mybatis config file are intended to be applied globally, then beware of side effect, especially when common types such are java.lang.String are involved.

In this case, mybatis would apply the type handler to every string to convert it to SQL timestamp. And I suppose you want most of string params be passed as is.

First, add logs in method TSTypeHandler.setNonNullParameter to check whether it is actually called or not.

Then just remove type handler from global config and reference it only in statement. Try without alias first: use full qualified name.

blackwizard
  • 2,034
  • 1
  • 9
  • 21