xml中对应的sql如下:
parameterType="com.newcosoft.lsmp.op.dao.entity.LsmpTournament">
update lsmp_tournament
set
match_num=#{matchNum,jdbcType=VARCHAR},
tournament_num=#{tournamentNum,jdbcType=VARCHAR},
tournament_code=#{tournamentCode,jdbcType=VARCHAR},
tournament_name=#{tournamentName,jdbcType=VARCHAR},
tournament_type=#{tournamentType,jdbcType=NUMERIC},
game_num=#{gameNum,jdbcType=VARCHAR},
pool_code=#{poolCode,jdbcType=VARCHAR},
pool_id=#{poolId,jdbcType=VARCHAR},
pool_name=#{poolName,jdbcType=VARCHAR},
pool_status=#{poolStatus,jdbcType=NUMERIC},
league_code=#{leagueCode,jdbcType=VARCHAR},
league_id=#{leagueId,jdbcType=VARCHAR},
start_sale_time=#{startSaleTime,jdbcType=DATE},
end_sale_time=#{endSaleTime},
odds_type=#{oddsType,jdbcType=NUMERIC},
single=#{single,jdbcType=NUMERIC},
allup=#{allup,jdbcType=NUMERIC},
group_number=#{groupNumber,jdbcType=VARCHAR},
group_name=#{groupName,jdbcType=VARCHAR},
group_display=#{groupDisplay,jdbcType=NUMERIC},
refund_status=#{refundStatus,jdbcType=NUMERIC},
selection=#{selection,jdbcType=VARCHAR},
ext01=#{ext01,jdbcType=VARCHAR},
ext02=#{ext02,jdbcType=VARCHAR}
where
id=#{id}
sql中end_sale_time=#{endSaleTime},没有指定jdbcType,所以在插入数据时报如下错误:2014-5-8 13:46:14 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet [dispatcher] in context with path [/tlsmp-gm-web] threw exception [Request processing failed;
nested exception is org.springframework.jdbc.UncategorizedSQLException: Error setting null for parameter #14 with JdbcType OTHER .
Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. Cause: java.sql.SQLException: 无效的列类型: 1111
; uncategorized SQLException for SQL []; SQL state [99999]; error code [17004]; 无效的列类型: 1111;
nested exception is java.sql.SQLException: 无效的列类型: 1111] with root cause
java.sql.SQLException: 无效的列类型: 1111
解决方法:为每个参数设置对应的jdbcType 类型。如上面的sql中所配置的。
网上第二种解决方法:
在MyBatis配置文件中,设置当JDBC类型为空值时,要指定的值,默认为OTHER,我们指定为NULL就好了(注意是大写的NULL)。
mybatis的配置文件:
SIMPLE :简单
REUSE:执行器可能重复使用prepared statements 语句
BATCH:执行器可以重复执行语句和批量更新
–>
适配oracle数据库的时候,mybatis报了Error setting null parameter,bug发现是参数出现了null值,对于Mybatis,如果进行操作的时候,没有指定jdbcType类型的参数,就可能导致问题。
而postgreSQL,MySQL,SQLSERVER都支持JdbcType.NULL类型,Oracle是不支持,适配的时候也因为这个问题导致mybatis报错。
去源码中查看 org.apache.ibatis.type.org.apache.ibatis.type中有如下代码:
public void setParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException {
if (parameter == null) {
if (jdbcType == null) {
throw new TypeException("JDBC requires that the JdbcType must be specified for all nullable parameters.");
}
try {
ps.setNull(i, jdbcType.TYPE_CODE);
} catch (SQLException e) {
throw new TypeException("Error setting null for parameter #" + i + " with JdbcType " + jdbcType + " . " +
"Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. " +
"Cause: " + e, e);
}
} else {
setNonNullParameter(ps, i, parameter, jdbcType);
}
} 可以看出,是因为你传入的参数的字段为null对象无法获取对应的jdbcType类型,而报的错误。