MyBatis 遇到 Error updating database.问题

这是我第一次想要在 CSDN 社区发表博客,原因呢是因为我也用了 CSDN 两年了,非常感谢它帮助我解决了很多问题,但是呢,每个人和每个人的问题都会有一些出入,所以呢,我想把我遇到的问题并且得以解决的分享给大家!

问题

        我是在听了老师讲的 MyBatis 后想要使用动态 SQL 语句执行插入操作,但是我自认为没有问题的 sql 语句却出现了一个非常呆呆的错误,就是我的下面这条语句的最后一个 test="userRole != null and userRole ! = ''"  中的感叹号写成了中文的,这个困扰了我好久都没有找出来,后来经过一点点调试发现的。

<update id="modify" parameterType="User">
    update smbms_user
    <trim prefix="set" suffixOverrides=",">
        <if test="userCode != null and userCode != ''">
            userCode = #{userCode},
        </if>
        <if test="userName != null and userName != ''">
            userName = #{userName},
        </if>
        <if test="userPassword != null and userPassword != ''">
            userPassword = #{userPassword},
        </if>
        <if test="gender != null and gender != ''">
            gender = #{gender},
        </if>
        <if test="birthday != null and birthday != ''">
            birthday = #{birthday},
        </if>
        <if test="phone != null and phone != ''">
            phone = #{phone},
        </if>
        <if test="address != null and address != ''">
            address = #{address},
        </if>
        <if test="userRole != null and userRole ! = ''">
            userRole = #{userRole},
        </if>

    </trim>
    where id=#{id}
</update>

解决办法

        只需要将test="userRole != null and userRole ! = ''"  中的感叹号改成英文的即可。

下面看一下我的错误提示吧

org.apache.ibatis.exceptions.PersistenceException: 
### Error updating database.  Cause: org.apache.ibatis.builder.BuilderException: Error evaluating expression 'userRole != null and userRole ! = '''. Cause: org.apache.ibatis.ognl.ExpressionSyntaxException: Malformed OGNL expression: userRole != null and userRole ! = '' [org.apache.ibatis.ognl.ParseException: Encountered " "!" "! "" at line 1, column 31.
Was expecting one of:
    <EOF> 
    "," ...
    "=" ...
    "?" ...
    "||" ...
    "or" ...
    "&&" ...
    "and" ...
    "|" ...
    "bor" ...
    "^" ...
    "xor" ...
    "&" ...
    "band" ...
    "==" ...
    "eq" ...
    "!=" ...
    "neq" ...
    "<" ...
    "lt" ...
    ">" ...
    "gt" ...
    "<=" ...
    "lte" ...
    ">=" ...
    "gte" ...
    "in" ...
    "not" ...
    "<<" ...
    "shl" ...
    ">>" ...
    "shr" ...
    ">>>" ...
    "ushr" ...
    "+" ...
    "-" ...
    "*" ...
    "/" ...
    "%" ...
    "instanceof" ...
    "." ...
    "(" ...
    "[" ...
    <DYNAMIC_SUBSCRIPT> ...
    "(" ...
    ]
### The error may exist in mapper/UserMapper.xml
### The error may involve com.smbms.mapper.UserMapper.modify
### The error occurred while executing an update
### Cause: org.apache.ibatis.builder.BuilderException: Error evaluating expression 'userRole != null and userRole ! = '''. Cause: org.apache.ibatis.ognl.ExpressionSyntaxException: Malformed OGNL expression: userRole != null and userRole ! = '' [org.apache.ibatis.ognl.ParseException: Encountered " "!" "! "" at line 1, column 31.
Was expecting one of:
    <EOF> 
    "," ...
    "=" ...
    "?" ...
    "||" ...
    "or" ...
    "&&" ...
    "and" ...
    "|" ...
    "bor" ...
    "^" ...
    "xor" ...
    "&" ...
    "band" ...
    "==" ...
    "eq" ...
    "!=" ...
    "neq" ...
    "<" ...
    "lt" ...
    ">" ...
    "gt" ...
    "<=" ...
    "lte" ...
    ">=" ...
    "gte" ...
    "in" ...
    "not" ...
    "<<" ...
    "shl" ...
    ">>" ...
    "shr" ...
    ">>>" ...
    "ushr" ...
    "+" ...
    "-" ...
    "*" ...
    "/" ...
    "%" ...
    "instanceof" ...
    "." ...
    "(" ...
    "[" ...
    <DYNAMIC_SUBSCRIPT> ...
    "(" ...
    ]

    at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:196)
    at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:67)
    at org.apache.ibatis.binding.MapperProxy$PlainMethodInvoker.invoke(MapperProxy.java:145)
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:86)
    at com.sun.proxy.$Proxy6.modify(Unknown Source)
    at TestUser.TestQuery(TestUser.java:70)
    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.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
    at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
Caused by: org.apache.ibatis.builder.BuilderException: Error evaluating expression 'userRole != null and userRole ! = '''. Cause: org.apache.ibatis.ognl.ExpressionSyntaxException: Malformed OGNL expression: userRole != null and userRole ! = '' [org.apache.ibatis.ognl.ParseException: Encountered " "!" "! "" at line 1, column 31.
Was expecting one of:
    <EOF> 
    "," ...
    "=" ...
    "?" ...
    "||" ...
    "or" ...
    "&&" ...
    "and" ...
    "|" ...
    "bor" ...
    "^" ...
    "xor" ...
    "&" ...
    "band" ...
    "==" ...
    "eq" ...
    "!=" ...
    "neq" ...
    "<" ...
    "lt" ...
    ">" ...
    "gt" ...
    "<=" ...
    "lte" ...
    ">=" ...
    "gte" ...
    "in" ...
    "not" ...
    "<<" ...
    "shl" ...
    ">>" ...
    "shr" ...
    ">>>" ...
    "ushr" ...
    "+" ...
    "-" ...
    "*" ...
    "/" ...
    "%" ...
    "instanceof" ...
    "." ...
    "(" ...
    "[" ...
    <DYNAMIC_SUBSCRIPT> ...
    "(" ...
    ]
    at org.apache.ibatis.scripting.xmltags.OgnlCache.getValue(OgnlCache.java:48)
    at org.apache.ibatis.scripting.xmltags.ExpressionEvaluator.evaluateBoolean(ExpressionEvaluator.java:32)
    at org.apache.ibatis.scripting.xmltags.IfSqlNode.apply(IfSqlNode.java:34)
    at org.apache.ibatis.scripting.xmltags.MixedSqlNode.lambda$apply$0(MixedSqlNode.java:32)
    at java.util.ArrayList.forEach(ArrayList.java:1257)
    at org.apache.ibatis.scripting.xmltags.MixedSqlNode.apply(MixedSqlNode.java:32)
    at org.apache.ibatis.scripting.xmltags.TrimSqlNode.apply(TrimSqlNode.java:55)
    at org.apache.ibatis.scripting.xmltags.MixedSqlNode.lambda$apply$0(MixedSqlNode.java:32)
    at java.util.ArrayList.forEach(ArrayList.java:1257)
    at org.apache.ibatis.scripting.xmltags.MixedSqlNode.apply(MixedSqlNode.java:32)
    at org.apache.ibatis.scripting.xmltags.DynamicSqlSource.getBoundSql(DynamicSqlSource.java:39)
    at org.apache.ibatis.mapping.MappedStatement.getBoundSql(MappedStatement.java:305)
    at org.apache.ibatis.executor.statement.BaseStatementHandler.<init>(BaseStatementHandler.java:64)
    at org.apache.ibatis.executor.statement.PreparedStatementHandler.<init>(PreparedStatementHandler.java:41)
    at org.apache.ibatis.executor.statement.RoutingStatementHandler.<init>(RoutingStatementHandler.java:46)
    at org.apache.ibatis.session.Configuration.newStatementHandler(Configuration.java:681)
    at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:48)
    at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:117)
    at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:76)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:194)
    ... 29 more
Caused by: org.apache.ibatis.ognl.ExpressionSyntaxException: Malformed OGNL expression: userRole != null and userRole ! = '' [org.apache.ibatis.ognl.ParseException: Encountered " "!" "! "" at line 1, column 31.
Was expecting one of:
    <EOF> 
    "," ...
    "=" ...
    "?" ...
    "||" ...
    "or" ...
    "&&" ...
    "and" ...
    "|" ...
    "bor" ...
    "^" ...
    "xor" ...
    "&" ...
    "band" ...
    "==" ...
    "eq" ...
    "!=" ...
    "neq" ...
    "<" ...
    "lt" ...
    ">" ...
    "gt" ...
    "<=" ...
    "lte" ...
    ">=" ...
    "gte" ...
    "in" ...
    "not" ...
    "<<" ...
    "shl" ...
    ">>" ...
    "shr" ...
    ">>>" ...
    "ushr" ...
    "+" ...
    "-" ...
    "*" ...
    "/" ...
    "%" ...
    "instanceof" ...
    "." ...
    "(" ...
    "[" ...
    <DYNAMIC_SUBSCRIPT> ...
    "(" ...
    ]
    at org.apache.ibatis.ognl.Ognl.parseExpression(Ognl.java:181)
    at org.apache.ibatis.scripting.xmltags.OgnlCache.parseExpression(OgnlCache.java:55)
    at org.apache.ibatis.scripting.xmltags.OgnlCache.getValue(OgnlCache.java:46)
    ... 48 more
Caused by: org.apache.ibatis.ognl.ParseException: Encountered " "!" "! "" at line 1, column 31.
Was expecting one of:
    <EOF> 
    "," ...
    "=" ...
    "?" ...
    "||" ...
    "or" ...
    "&&" ...
    "and" ...
    "|" ...
    "bor" ...
    "^" ...
    "xor" ...
    "&" ...
    "band" ...
    "==" ...
    "eq" ...
    "!=" ...
    "neq" ...
    "<" ...
    "lt" ...
    ">" ...
    "gt" ...
    "<=" ...
    "lte" ...
    ">=" ...
    "gte" ...
    "in" ...
    "not" ...
    "<<" ...
    "shl" ...
    ">>" ...
    "shr" ...
    ">>>" ...
    "ushr" ...
    "+" ...
    "-" ...
    "*" ...
    "/" ...
    "%" ...
    "instanceof" ...
    "." ...
    "(" ...
    "[" ...
    <DYNAMIC_SUBSCRIPT> ...
    "(" ...
    
    at org.apache.ibatis.ognl.OgnlParser.generateParseException(OgnlParser.java:3232)
    at org.apache.ibatis.ognl.OgnlParser.jj_consume_token(OgnlParser.java:3098)
    at org.apache.ibatis.ognl.OgnlParser.topLevelExpression(OgnlParser.java:19)
    at org.apache.ibatis.ognl.Ognl.parseExpression(Ognl.java:179)
    ... 50 more

猜你喜欢

转载自blog.csdn.net/lemonzjk/article/details/124514044
今日推荐