我在用Mybatis和Vertica DB。我有一个像下面这样的对象,我不能修改它
Class Employee{
Integer empId;
Integer deptId;
Integer salary;
//get and set methods for above
}TypeHandler的代码如下所示
public class IntegerToStringTypeHandler extends BaseTypeHandler<Integer> {
@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));
}
}然后桌子如下所示
Desc Employee
Columns. Type
empId Integer
deptId Varchar(10)
Salary Integer我有如下所示的查询
<select id=“getCount” parameter=“employee”>
Select count(1) from Employee
Where empid = #{employee.empid}
AND. deptId = #{employee. deptId,typeHandler=com.convert.type.IntegerToStringTypeHandler}
AND salary= #{employee.salary}
</select>但是它不起作用--除了下面的例外情况,它会失败
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]我花了很多时间。有人能告诉我我错过了什么吗?
主要的问题在这条线上。
#{employee. deptId,typeHandler=com.convert.type.IntegerToStringTypeHandler}谢谢
发布于 2018-06-28 09:50:11
这里的代码不能是实际执行的代码,我想您已经在文章中重写了代码,因为:-引用字符“ /”不受XML解析器的支持。
.之后有一个意外的点AND,可能会导致SQL语法异常。因此,请复制/粘贴实际执行的代码,这样可以给出更准确的答案。你的TypeHandler运行良好。
https://stackoverflow.com/questions/50997240
复制相似问题