查询正常,页面显示正常,但是后台报错误
首先是错误信息:
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:15)
at com.imooc.servlet.UserFindServlet.doPost(UserFindServlet.java:26)
at com.imooc.servlet.UserFindServlet.doGet(UserFindServlet.java:20)
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:496)
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:803)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
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
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:15)
at com.imooc.servlet.UserFindServlet.doPost(UserFindServlet.java:26)
at com.imooc.servlet.UserFindServlet.doGet(UserFindServlet.java:20)
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:496)
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:803)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
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
--------------------------------------------------------------------------
下面是我的代码:
UsersDao:
package com.imooc.Dao;
import com.imooc.entity.Users;
import com.imooc.utils.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import java.util.List;
public class UsersDao {
private SqlSession sqlsession = SqlSessionFactoryUtils.getSqlSessionFactory().openSession();
List<Users > list = null;
public List<Users> findAll(){
try {
list = sqlsession.selectList("findAll");
}catch (Exception e){
e.printStackTrace();
}finally {
sqlsession.close();
}
return list;
}
}
------------------------------------------------------------------------
initSqlSessionListener:
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 {
public void contextInitialized(ServletContextEvent servletContextEvent) {
System.out.println("容器初始化");
SqlSessionFactoryUtils.initSquSessionFactory();
}
public void contextDestroyed(ServletContextEvent servletContextEvent) {
System.out.println("容器销毁");
SqlSessionFactoryUtils.close();
}
}
---------------------------------------------
UserFindServlet:
package com.imooc.servlet;
import com.imooc.Dao.UsersDao;
import com.imooc.entity.Users;
import com.imooc.utils.SqlSessionFactoryUtils;
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 UserFindServlet extends HttpServlet {
private 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 {
// super.doPost(req, resp);
List<Users> list = usersDao.findAll();
req.setAttribute("usersList", list);
req.getRequestDispatcher("index.jsp").forward(req,resp);
}
}
-------------------------------------------------------------------------------------
initSquSessionFactory:
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;
public class SqlSessionFactoryUtils {
private static String resource = "mybatis-config.xml";
private static SqlSessionFactory sqlSessionFactory;
private static ThreadLocal<SqlSession> threadLocal = new ThreadLocal<SqlSession>();
public static void initSquSessionFactory(){
try {
InputStream rs = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(rs);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSessionFactory getSqlSessionFactory(){
return sqlSessionFactory;
}
public static void close(){
SqlSession session = threadLocal.get();
if(session!=null){
session.close();
threadLocal.set(null);
}
}
}
-------------------------------------------------------------------
index.jsp:
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2018/6/14
Time: 16:12
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<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-1.12.4.min.js"></script>
<script src="lib/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
</head>
<body>
<div class="containner">
<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>
<%--<th>最后修改时间</th>--%>
<%--<th>最后登录时间</th>--%>
<th>用户状态</th>
<th>操作</th>
</tr>
<c:forEach var="user" items="${usersList}" >
<tr>
<td>aa${user.id}</td>
<td>${user.username}</td>
<td>${user.nickname}</td>
<%--<td>18</td>--%>
<%--<td>男</td>--%>
<td>${user.email}</td>
<td>${user.phone}</td>
<td>${user.createTime}</td>
<%--<td>2017-05-15</td>--%>
<td>zz${user.userStatus}</td>
<td>
<a href="">查看</a>
<a href="">修改</a>
<a href="">删除</a>
</td>
</tr>
</c:forEach>
</table>
</div>
</div>
</body>
</html>
------------------------------------------------------------------------------------
home.jsp:
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2018/6/17
Time: 14:53
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
response.sendRedirect("/index");
%>
</body>
</html>
--------------------------------------------------------------------------------------
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>mybatispro</display-name>
<welcome-file-list>
<welcome-file>home.jsp</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
-------------------------------------------------------------------
usersMapper:
<?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属性是用于配置命名空间,主要进行session级别的缓存管理。命名空间默认为当前操作的实体类的全路径-->
<mapper namespace="com.imooc.entity.Users">
<select id="findAll" resultType="com.imooc.entity.Users">
select * from users ;
</select>
</mapper>
-----------------------------------------------------------------
db.propertities:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatisdemo?useUnicode=true&characterEncoding=utf-8&useSSL=true
username=root
password=123456
------------------------------------------------------------------------------------
这个是我的显示界面》显示了三个不同的信息(数据库里的登录账号不同,其他数据一致。。。)
ps:顺便请教下老师,我的网页显示貌似不正常,无法显示完整,在Javaweb课程中还是正常的。。。。这节课就变样了。。。
正在回答 回答被采纳积分+1
1、显示不正常:Ctrl+鼠标滚轮,看一下页面显示的百分比
2、关于Executor was closed.的这个问题:
建议同学把UsersDao类中的代码调整为这种样式后再试下~
public class UsersDAO { private SqlSession sqlSession; private List<Users> list; private SqlSession getSession() { sqlSession = SqlSessionFactoryUtils.getSqlSessionFactory().openSession(); return sqlSession; } /** * 查询全部用户 * @return */ public List<Users> findAll() { try { list = getSession().selectList("findUsers"); } catch (Exception e) { e.printStackTrace(); } finally { sqlSession.close(); } return list; } }
祝学习愉快!
- 参与学习 人
- 提交作业 277 份
- 解答问题 4297 个
Java数据库开发的必备技能,从流行的MySQL数据库开始,到Java原生的数据库管理接口JDBC的使用,再到常用的数据持久化框架MyBatis,让你向Java工程师的目标又迈进了一步!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星