java查询数据库数据报下面异常:
java.sql.SQLException: Parameter index out of range (2 > number of parameters, which is 1).

14-Feb-2022 15:38:05.578 严重 [http-nio-8080-exec-4] org.apache.catalina.core.StandardWrapperValve.invoke 在路径为/rbacDemo_war_exploded的上下文中,Servlet[springmvc]的Servlet.service()引发了具有根本原因的异常Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='username', mode=IN, javaType=class java.lang.String, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #2 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. Cause: java.sql.SQLException: Parameter index out of range (2 > number of parameters, which is 1).
	java.sql.SQLException: Parameter index out of range (2 > number of parameters, which is 1).
		at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:965)
		at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:898)
		at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:887)
		at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:861)
		at com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3382)
		at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3367)
		at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:4083)
		at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
		at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
		at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
		at java.lang.reflect.Method.invoke(Method.java:498)
		at org.apache.ibatis.logging.jdbc.PreparedStatementLogger.invoke(PreparedStatementLogger.java:67)
		at com.sun.proxy.$Proxy32.setString(Unknown Source)

解决:查看mybatis的.xml文件中注释的地方,看一下注释的方式:
如果是类似下面注释:
源代码:

--         SELECT * from users u...
--         where u.username=#{username}

则应该改为:


<!--       SELECT * from users u...
        and u.username=#{username}-->

更多推荐