查询没有数据?
您好!
问题是这样的,web运行查询数据时,返回为null,但是没有报错。
但是我使用test测试时,是有数据的如图
它们加载的配置文件和使用的数据库都是一样的,请问有什么原因会出现在这种情况呢?
正在回答
请同学再这里随意打印一句话:
看看是否进入的catch中呢?
另外,看一下这两个地方是否有报错信息呢?
祝学习愉快。
package dao; import java.util.List; import org.apache.ibatis.session.SqlSession; import com.entiry.Users; import utils.SqlSessionFactoryUtils; public class UsersDao { private SqlSession session=SqlSessionFactoryUtils.getFactory().openSession(); private List<Users> list; public List<Users> findAll(){ try { list = session.selectList("findAll"); } catch (Exception e) { // TODO: handle exception }finally{ session.close(); } return list; } }
package listener; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; import utils.SqlSessionFactoryUtils; @WebListener public class SqlSessionFacteryListener implements ServletContextListener{ public void contextInitialized(ServletContextEvent sce) { System.out.println("SqlSessionFactory加载中"); SqlSessionFactoryUtils.initSqlSessionFactory(); } public void contextDestroyed(ServletContextEvent sce) { System.out.println("Session销毁中"); SqlSessionFactoryUtils.close(); } }
package utils; import java.io.IOException; import java.io.InputStream; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class SqlSessionFactoryUtils { private static String resource = "mybatis-c.xml"; private static SqlSessionFactory factory; private static ThreadLocal<SqlSession> threadlocal = new ThreadLocal<SqlSession>(); public static void initSqlSessionFactory(){ try { InputStream is = Resources.getResourceAsStream(resource); factory = new SqlSessionFactoryBuilder().build(is); } catch (IOException e) { e.printStackTrace(); } } public static SqlSessionFactory getFactory(){ return factory; } public static void close(){ SqlSession session = threadlocal.get(); if(session != null){ session.close(); threadlocal.set(null); } } }
package servlet; import java.io.IOException; import java.util.List; 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 com.entiry.Users; import dao.UsersDao; @WebServlet("/indexc") public class UsersSelectServlet extends HttpServlet{ private UsersDao dao = 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 = dao.findAll(); if(list==null){ System.out.println(list); } req.setAttribute("usersFindALL", list); req.getRequestDispatcher("index.jsp").forward(req, resp); } }
@易达333 您好!请帮忙查看问题出在什么地方,非常感谢。
@易达333 您好!感谢您的帮忙,如下是你需要的:
package dao;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import com.entiry.Users;
import utils.SqlSessionFactoryUtils;
public class UsersDao {
private SqlSession session=SqlSessionFactoryUtils.getFactory().openSession();
private List<Users> list;
public List<Users> findAll(){
try {
list = session.selectList("findAll");
} catch (Exception e) {
}finally{
session.close();
}
return list;
}
}
- 参与学习 人
- 提交作业 357 份
- 解答问题 8016 个
本阶段将带你学习MySQL数据库,JDBC接口,MyBatis框架等,带你掌握的数据的存放和管理。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星