请老师帮忙看一下报下面的错误
界面显示如下,没有显示出用户状态,数据库中已设置
报错信息:
org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: org.apache.ibatis.executor.ExecutorException: Executor was closed.
### Cause: org.apache.ibatis.executor.ExecutorException: Executor was closed.
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:150)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:136)
at com.imooc.dao.UsersDAO.findAll(UsersDAO.java:21)
at com.imooc.servlet.UsersFindServlet.doPost(UsersFindServlet.java:23)
at com.imooc.servlet.UsersFindServlet.doGet(UsersFindServlet.java:18)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:800)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:806)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
Caused by: org.apache.ibatis.executor.ExecutorException: Executor was closed.
at org.apache.ibatis.executor.BaseExecutor.createCacheKey(BaseExecutor.java:197)
at org.apache.ibatis.executor.CachingExecutor.createCacheKey(CachingExecutor.java:146)
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:82)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:148)
... 29 more
[com.imooc.bean.Users@307a2eaa, com.imooc.bean.Users@3d90ff9f, com.imooc.bean.Users@5564bc21, com.imooc.bean.Users@423b177f]
org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: org.apache.ibatis.executor.ExecutorException: Executor was closed.
### Cause: org.apache.ibatis.executor.ExecutorException: Executor was closed.
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:150)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:136)
at com.imooc.dao.UsersDAO.findAll(UsersDAO.java:21)
at com.imooc.servlet.UsersFindServlet.doPost(UsersFindServlet.java:23)
at com.imooc.servlet.UsersFindServlet.doGet(UsersFindServlet.java:18)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:800)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:806)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
Caused by: org.apache.ibatis.executor.ExecutorException: Executor was closed.
at org.apache.ibatis.executor.BaseExecutor.createCacheKey(BaseExecutor.java:197)
at org.apache.ibatis.executor.CachingExecutor.createCacheKey(CachingExecutor.java:146)
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:82)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:148)
... 29 more
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>用户管理</title> <link rel="stylesheet" href="lib/bootstrap-3.3.7-dist/css/bootstrap.min.css"> <script src="lib/2.2.4/jquery-2.2.4.min.js"></script> <script src="lib/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <div class="row"> <div class="page-header"> <h1>慕课网后台管理系统<small>用户数据管理中心</small></h1> </div> </div> <div class="row"> <div class="jumbotron"> <h1>MyBatis基础入门课程</h1> <p>项目案例</p> <p><a class="btn btn-primary btn-lg" href="#" role="button">慕课网</a></p> </div> </div> <div class="row"> <table class="table table-hover table-striped"> <tr> <th>用户编号</th> <th>用户名</th> <th>用户昵称</th> <th>邮箱</th> <th>联系方式</th> <th>账号创建时间</th> <th>用户状态</th> <th>操作</th> </tr> <c:forEach var="user" items="${usersList}"> <tr> <td>${user.id}</td> <td>${user.username}</td> <td>${user.nickname}</td> <td>${user.email}</td> <td>${user.phone}</td> <td>${user.createTime}</td> <c:if test="${user.userStatus}==0"> <td>正常</td> </c:if> <c:if test="${user.userStatus}==1"> <td>锁定</td> </c:if> <c:if test="${user.userStatus}==2"> <td>删除</td> </c:if> <td> <a href="">查看</a> <a href="">修改</a> <a href="">删除</a> </td> </tr> </c:forEach> </table> </div> </div> </body> </html>
package com.imooc.utils; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; import java.io.InputStream; /** * SqlSessionFactory工具类 */ public class SqlSessionFactoryUtils { private static String RESOURCE = "mybatis.xml"; private static SqlSessionFactory sqlSessionFactory; private static ThreadLocal<SqlSession> threadLocal = new ThreadLocal<SqlSession>(); /** * 创建一个初始化SqlSessionFactory的方法 */ public static void initSqlSessionFactory(){ try { InputStream is = Resources.getResourceAsStream(RESOURCE); sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); } catch (IOException e) { e.printStackTrace(); } } /** * 获取工厂对象的方法 */ public static SqlSessionFactory getSqlSessionFactory(){ return sqlSessionFactory; } /** * 关闭Sqlsession的方法 */ public static void close(){ SqlSession sqlSession = threadLocal.get(); if(sqlSession != null){ sqlSession.close(); threadLocal.set(null); } } }
package com.imooc.listener; import com.imooc.utils.SqlSessionFactoryUtils; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; /** * 监听器类 */ @WebListener public class InitSqlSessionListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { //初始化我们的SqlSessionFactory对象 System.out.println("初始化中。。。"); SqlSessionFactoryUtils.initSqlSessionFactory(); } @Override public void contextDestroyed(ServletContextEvent sce) { //关闭Sqlsession对象 System.out.println("关闭中。。。"); SqlSessionFactoryUtils.close(); } }
package com.imooc.dao; import com.imooc.bean.Users; import com.imooc.utils.SqlSessionFactoryUtils; import org.apache.ibatis.session.SqlSession; import java.util.List; /** * 用户的DAO */ public class UsersDAO { //打开和数据库之间的会话 private SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSessionFactory().openSession(); private List<Users> list; //查询的方法 public List<Users> findAll(){ try { list = sqlSession.selectList("findUsers"); }catch (Exception e){ e.printStackTrace(); }finally { sqlSession.close(); } System.out.println(list); return list; } }
package com.imooc.servlet; import com.imooc.bean.Users; import com.imooc.dao.UsersDAO; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.List; @WebServlet("/index") public class UsersFindServlet extends HttpServlet { UsersDAO usersDAO = new UsersDAO(); @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { List<Users> list = usersDAO.findAll(); req.setAttribute("usersList",list); req.getRequestDispatcher("index.jsp").forward(req,resp); } }
package com.imooc.bean; import java.util.Date; public class Users { private Integer id; private String username; private String userpass; private String nickname; private String age; private String gender; private String phone; private String email; private Date createTime; private Date updateTime; private Date lastLogin; private Integer userStatus; private String remark; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getUserpass() { return userpass; } public void setUserpass(String userpass) { this.userpass = userpass; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getNickname() { return nickname; } public void setNickname(String nickname) { this.nickname = nickname; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public Date getUpdateTime() { return updateTime; } public void setUpdateTime(Date updateTime) { this.updateTime = updateTime; } public Date getLastLogin() { return lastLogin; } public void setLastLogin(Date lastLogin) { this.lastLogin = lastLogin; } public Integer getUserStatus() { return userStatus; } public void setRemark(String remark) { this.remark = remark; } public String getRemark() { return remark; } public void setUserStatus(Integer userStatus) { this.userStatus = userStatus; } }
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!--<properties resource="db.properties"></properties>--> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mydb+?useSSL=false&serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf-8"/> <property name="username" value="root"/> <property name="password" value="115224"/> </dataSource> </environment> </environments> <mappers> <mapper resource="mapper/usersMapper.xml"/> </mappers> </configuration>
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.imooc.bean.Users"> <select id="findUsers" resultType="com.imooc.bean.Users"> select * from Users; </select> </mapper>
正在回答
你好!从报错提示看,是sqlSession被关闭了,可以尝试在list = sqlSession.selectList("findUsers");语句后加上sqlSession.commit()。如果还是不行,将sqlSession.close()去掉,看看是否是这条语句引起的。如果还有问题,请继续提问。
祝学习愉快!
- 参与学习 人
- 提交作业 277 份
- 解答问题 4297 个
Java数据库开发的必备技能,从流行的MySQL数据库开始,到Java原生的数据库管理接口JDBC的使用,再到常用的数据持久化框架MyBatis,让你向Java工程师的目标又迈进了一步!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星