有关ibatis求助
请哪位高手看一下,错在哪里了
public void updateChannel(Channel c) {
try {
session.update(Channel.class.getName()+".updateChannel", c);
session.commit();
} catch (Exception e) {
e.printStackTrace();
session.rollback();
} finally{
session.close();
}
}配置文件
<update id="updateChannel" parameterType="Channel" >
update t_channel set name=#{name},description=#{description} where id=#{id}
</update>
junit测试
@Test
public void testUpdateChannel() {
Channel c = channeldao.getChannelById(11);
c.setName("updatechannel");
channeldao.updateChannel(c);
}
出现的错误--------------------------------------------------
org.apache.ibatis.exceptions.PersistenceException:
### Error updating database. Cause: org.apache.ibatis.executor.ExecutorException: Executor was closed.
### The error may exist in com/test/cms/backmanager/DAO/implement/Channel.xml
### The error may involve com.test.cms.model.Channel.updateChannel
### The error occurred while executing an update
### Cause: org.apache.ibatis.executor.ExecutorException: Executor was closed.
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:8)
at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:123)
at com.test.cms.backmanager.DAO.implement.ChannelDaoForMybatisImpl.updateChannel(ChannelDaoForMybatisImpl.java:89)
at com.test.cms.backmanager.DAO.ChannelDAOTest.testUpdateChannel(ChannelDAOTest.java:55)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
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.executor.ExecutorException: Executor was closed.
at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:86)
at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:43)
at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:121)
... 26 more
[解决办法]
SqlSession session =Mapper.openSession(TransactionIsolationLevel.READ_COMMITTED);
或者参考FortuneJ中MyBatis的用法
参考地址www.cnblogs.com/mail-ricklee
[解决办法]
junit是绿条,怎么会有错
[解决办法]
SqlSession已经有的了,其他增删查没有问题
[解决办法]
就这个update出错
[解决办法]
session.update(Channel.class.getName()+".updateChannel", c);
你的错误在这里 你的session是全局的
@Test
public void testUpdateChannel() {
Channel c = channeldao.getChannelById(11);
c.setName("updatechannel");
channeldao.updateChannel(c); }
做完查询操作 session就关闭了 所以 再更新的时候 session 就没有了
遇到同样的错误了,哎。。。
[解决办法]
把session改成局部的!!!