異常信息:
org.apache.ibatis.exceptions.PersistenceException:?
###?Error?updating?database.??Cause:?org.apache.ibatis.binding.BindingException:?Parameter?'lastName'?not?found.?Available?parameters?are?[1,?0,?param1,?param2]
###?Cause:?org.apache.ibatis.binding.BindingException:?Parameter?'lastName'?not?found.?Available?parameters?are?[1,?0,?param1,?param2]
at?org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at?org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:200)
at?org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:62)
at?org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
at?com.sun.proxy.$Proxy3.updateEmpsTestSet(Unknown?Source)
at?com.lesson.mybatis.test.MybatisTest.test05(MybatisTest.java:140)
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:47)
at?org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at?org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at?org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at?org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at?org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at?org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at?org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at?org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at?org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at?org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at?org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at?org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at?org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at?org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at?org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at?org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at?org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at?org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused?by:?org.apache.ibatis.binding.BindingException:?Parameter?'lastName'?not?found.?Available?parameters?are?[1,?0,?param1,?param2]
at?org.apache.ibatis.binding.MapperMethod$ParamMap.get(MapperMethod.java:202)
at?org.apache.ibatis.scripting.xmltags.DynamicContext$ContextAccessor.getProperty(DynamicContext.java:115)
at?org.apache.ibatis.ognl.OgnlRuntime.getProperty(OgnlRuntime.java:2671)
at?org.apache.ibatis.ognl.ASTProperty.getValueBody(ASTProperty.java:114)
at?org.apache.ibatis.ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
at?org.apache.ibatis.ognl.SimpleNode.getValue(SimpleNode.java:258)
at?org.apache.ibatis.ognl.ASTNotEq.getValueBody(ASTNotEq.java:50)
at?org.apache.ibatis.ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
at?org.apache.ibatis.ognl.SimpleNode.getValue(SimpleNode.java:258)
at?org.apache.ibatis.ognl.ASTAnd.getValueBody(ASTAnd.java:61)
at?org.apache.ibatis.ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
at?org.apache.ibatis.ognl.SimpleNode.getValue(SimpleNode.java:258)
at?org.apache.ibatis.ognl.Ognl.getValue(Ognl.java:470)
at?org.apache.ibatis.ognl.Ognl.getValue(Ognl.java:434)
at?org.apache.ibatis.scripting.xmltags.OgnlCache.getValue(OgnlCache.java:44)
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.apply(MixedSqlNode.java:33)
at?org.apache.ibatis.scripting.xmltags.TrimSqlNode.apply(TrimSqlNode.java:55)
at?org.apache.ibatis.scripting.xmltags.MixedSqlNode.apply(MixedSqlNode.java:33)
at?org.apache.ibatis.scripting.xmltags.DynamicSqlSource.getBoundSql(DynamicSqlSource.java:41)
at?org.apache.ibatis.mapping.MappedStatement.getBoundSql(MappedStatement.java:292)
at?org.apache.ibatis.executor.statement.BaseStatementHandler.<init>(BaseStatementHandler.java:64)
at?org.apache.ibatis.executor.statement.PreparedStatementHandler.<init>(PreparedStatementHandler.java:40)
at?org.apache.ibatis.executor.statement.RoutingStatementHandler.<init>(RoutingStatementHandler.java:46)
at?org.apache.ibatis.session.Configuration.newStatementHandler(Configuration.java:558)
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:198)
...?27?more
配置文件:
<update?id="updateEmpsTestSet">
????UPDATE?tb_employees
????<set>
????????<if?test="lastName?!=?null?and?lastName?!=?""">
????lastName?=?#{param1.lastName},
</if>
<if?test="gender?!=?null?and?gender?!=?""">
????gender?=?#{param1.gender},
</if>
<if?test="email?!=?null?and?email?!=?""">
????email?=?#{param1.email}
</if>
????</set>
????<!--?<trim?prefix="set"?suffixOverrides=",">
????????<if?test="lastName?!=?null?and?lastName?!=?""">
????lastName?=?#{param1.lastName},
</if>
<if?test="gender?!=?null?and?gender?!=?""">
????gender?=?#{param1.gender},
</if>
<if?test="email?!=?null?and?email?!=?""">
????email?=?#{param1.email}
</if>
????</trim>?-->
WHERE?id?=?#{param2}
</update>
接口方法:
public?int?updateEmpsTestSet(Employee?employee,Long?id);
測試方法:
@Test
public?void?test05()?{
????//一個SqlSession代表和數據庫的一次會話,用完之后必須關閉
????SqlSession?session?=?null;
????try?{
session?=?SessionUtil.getSession();
EmployeeMapper?mapper?=?session.getMapper(EmployeeMapper.class);
Employee?employee?=?new?Employee();
employee.setLastName("Pandas");
employee.setGender('男');
employee.setEmail("[email protected]");
int?index?=?mapper.updateEmpsTestSet(employee,new?Long(7));
System.out.println("==============================>"+index);
session.commit();
????}?catch?(IOException?e)?{
//?TODO?Auto-generated?catch?block
e.printStackTrace();
????}?finally{
if?(session?!=?null)?{
????SessionUtil.closeSession(session);
????????}
????}
}
2018-01-01
test里面的條件寫錯了,lastName 都要換成 parm1.lastName.如:
?<if?test="param1.lastName?!=?null?and?lastName?!=?""">
? ? ?