title: Mybatis TypeHandler to convert Integer to String
body: <p>I’m using Mybatis ORM with Vertica DB. I have an object like below, which I can’t modify</p>
  
  <pre><code>Class Employee{
  
    Integer empId;
  
    Integer deptId;
  
   Integer salary;
  
    //get and set methods for above
  
  }
  </code></pre>
  
  <p>And code for TypeHandler is like below</p>
  
  <pre><code>public class IntegerToStringTypeHandler  extends BaseTypeHandler&lt;Integer&gt; {
  
      @Override
      public void setNonNullParameter(PreparedStatement preparedStatement, int i, Integer integer, JdbcType jdbcType) throws SQLException {
          System.out.println("i  "+i +" integer "+ integer + "   "+integer );
          preparedStatement.setString(i,integer.toString());
      }
  
      @Override
      public Integer getNullableResult(ResultSet resultSet, String columnName) throws SQLException {
  
          return Integer.valueOf(resultSet.getString(columnName));
      }
  
      @Override
      public Integer getNullableResult(ResultSet resultSet, int i) throws SQLException {
          return Integer.valueOf(resultSet.getString(i));
  
      }
  
      @Override
      public Integer getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
          return Integer.valueOf(callableStatement.getString(i));
      }
  
  
  }
  </code></pre>
  
  <p>And then table is like below</p>
  
  <pre><code>Desc Employee
  
  Columns.        Type
  empId           Integer
  
  deptId          Varchar(10)
  
  Salary          Integer
  </code></pre>
  
  <p>I have mybatis query like below</p>
  
  <pre><code>&lt;select id=“getCount” parameter=“employee”&gt;
  
  Select count(1) from Employee
  Where  empid = #{employee.empid}
  AND.  deptId = #{employee. deptId,typeHandler=com.convert.type.IntegerToStringTypeHandler}
  AND  salary= #{employee.salary}  
  &lt;/select&gt;
  </code></pre>
  
  <p>But it doesn’t work it fails with the following exception</p>
  
  <pre><code>Caused by: org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='__frch_employee_0.deptid’, mode=IN, javaType=class java.lang.Integer, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #3 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. Cause: java.sql.SQLException: [Vertica][JDBC](10940) Invalid parameter index: 3.
          at org.apache.ibatis.scripting.defaults.DefaultParameterHandler.setParameters(DefaultParameterHandler.java:89) ~[mybatis-3.4.5.jar:3.4.5]
  
  
  Caused by: org.apache.ibatis.type.TypeException: Error setting non null for parameter #3 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. Cause: java.sql.SQLException: [Vertica][JDBC](10940) Invalid parameter index: 3.
          at org.apache.ibatis.type.BaseTypeHandler.setParameter(BaseTypeHandler.java:55) ~[mybatis-3.4.5.jar:3.4.5]
  
  Caused by: java.sql.SQLException: [Vertica][JDBC](10940) Invalid parameter index: 3.
          at com.vertica.exceptions.ExceptionConverter.toSQLException(Unknown Source) ~[vertica-jdbc-8.1.1-7.jar:?]
          at com.vertica.jdbc.common.SPreparedStatement.checkValidParameterIndex(Unknown Source) ~[vertica-jdbc-8.1.1-7.jar:?]
          at com.vertica.jdbc.common.SPreparedStatement.setString(Unknown Source) ~[vertica-jdbc-8.1.1-7.jar:?]
          at com.convert.type.IntegerToStringTypeHandler.setNonNullParameter(IntegerToStringTypeHandler.java:16) ~[classes/:?]
          at com.convert.type.IntegerToStringTypeHandler.setNonNullParameter(IntegerToStringTypeHandler.java:11) ~[classes/:?]
          at org.apache.ibatis.type.BaseTypeHandler.setParameter(BaseTypeHandler.java:53) ~[mybatis-3.4.5.jar:3.4.5]
          at org.apache.ibatis.scripting.defaults.DefaultParameterHandler.setParameters(DefaultParameterHandler.java:87) ~[mybatis-3.4.5.jar:3.4.5]
          at org.apache.ibatis.executor.statement.PreparedStatementHandler.parameterize(PreparedStatementHandler.java:93) ~[mybatis-3.4.5.jar:3.4.5]
          at org.apache.ibatis.executor.statement.RoutingStatementHandler.parameterize(RoutingStatementHandler.java:64) ~[mybatis-3.4.5.jar:3.4.5]
          at org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.java:86) ~[mybatis-3.4.5.jar:3.4.5]
  </code></pre>
  
  <p>I have spent a lot of time. Could someone please let know what I am missing?</p>
  
  <p>the main problem is in this line</p>
  
  <pre><code>#{employee. deptId,typeHandler=com.convert.type.IntegerToStringTypeHandler}
  </code></pre>
  
  <p>Thanks</p>