Mabatis中出现的一个bug。。。准确说是IDE给我挖的坑。。。

    xiaoxiao2022-07-07  209

    在下使用mybatis的动态sql中的set标签时xml中是这么写的,

    <update id="updateUser" parameterType="User"> update tb_user <set> <if test="userName !=null and userName != ''">user_name = #{userName},</if> <if test="name !=null and name != ''">name = #{name},</if> <if test="password !=null and password != ''">password = #{password},</if> <if test="age>0">age=#{age},</if> </set> where id = #{id}; -- set user_name = #{userName}, -- password = #{password}, -- name = #{name}, -- age = #{age}, -- sex = #{sex}, -- birthday = #{birthday}, -- updated = now() where id = #{id}; </update>

    然后报了这么个错误

    ### Cause: java.sql.SQLException: Parameter index out of range (6 > number of parameters, which is 5). at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:26) at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:154) at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:54) at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:52) at com.sun.proxy.$Proxy5.updateUser(Unknown Source) at cn.iphoneU.mybatis.daotest.UserDaoTest.testUpdate(UserDaoTest.java:155) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:305) at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:365) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63) at org.junit.runners.ParentRunner$4.run(ParentRunner.java:330) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:78) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:328) at org.junit.runners.ParentRunner.access$100(ParentRunner.java:65) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:292) at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:305) at org.junit.runners.ParentRunner.run(ParentRunner.java:412) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) Caused by: java.sql.SQLException: Parameter index out of range (6 > number of parameters, which is 5). at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927) at com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3729) at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3713) at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:4553) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.apache.ibatis.logging.jdbc.PreparedStatementLogger.invoke(PreparedStatementLogger.java:70) at com.sun.proxy.$Proxy7.setString(Unknown Source) at org.apache.ibatis.type.StringTypeHandler.setNonNullParameter(StringTypeHandler.java:31) at org.apache.ibatis.type.StringTypeHandler.setNonNullParameter(StringTypeHandler.java:26) at org.apache.ibatis.type.BaseTypeHandler.setParameter(BaseTypeHandler.java:50) at org.apache.ibatis.scripting.defaults.DefaultParameterHandler.setParameters(DefaultParameterHandler.java:81) at org.apache.ibatis.executor.statement.PreparedStatementHandler.parameterize(PreparedStatementHandler.java:80) at org.apache.ibatis.executor.statement.RoutingStatementHandler.parameterize(RoutingStatementHandler.java:61) at org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.java:74) at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:47) at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:105) at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:71) at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:152) ... 30 more

    百度来百度去,有说是sql语句的问题的,我反反复复检查了,没有错,并且控制台也打印了日志

    2019-05-22 23:50:46,191 ==> Preparing: update tb_user SET user_name = ?, name = ?, password = ?, age=? where id = ?; -- set user_name = ?, -- password = ?, -- name = ?, -- age = ?, -- sex = ?, -- birthday = ?, -- updated = now() where id = ?;

    一开始以为这个日志是没问题的,以为后-- set user_name = ?, -- password = ?, -- name = ?, -- age = ?, -- sex = ?, -- birthday = ?, -- updated = now() where id = ?是在进行参数注入。。后来定睛一看,哎呦我去,因为我使用快捷键生成的注释,IDEA自动给我弄成了sql的注释。。。。而xml中的注释应该是<!--代码代码-->这种,所以等于我之前的注释没生效,而日志中打印得也没问题,所以我把注释重新改成如图所示这样,就解决了

    <!-- set user_name = #{userName},--> <!-- password = #{password},--> <!-- name = #{name},--> <!-- age = #{age},--> <!-- sex = #{sex},--> <!-- birthday = #{birthday},--> <!-- updated = now() where id = #{id};-->

     

    最新回复(0)