正确导入hibernate包后在eclipse中能正常运行查询到数据库的数据但是在servlet中报错?

图片说明

HbnUtil 通用类调用session对象

package zuoye2.common;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HbnUtil {
  private static SessionFactory sessionFactory;

  public static Session getSession(){
      if((sessionFactory==null)||(sessionFactory.isClosed())){
          sessionFactory = new Configuration().configure().buildSessionFactory();
      }
      return sessionFactory.openSession();
  }
}

GoodsDAO.java

package zuoye2.dao.impl;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import zuoye2.common.HbnUtil;
import zuoye2.dao.dai.GoodsDAI;
import zuoye2.model.Goods;

public class GoodsDAO implements GoodsDAI {

    @Override
    public boolean create(Goods goods) {
        Session session = HbnUtil.getSession();
        session.beginTransaction();
        session.save(goods);
        session.getTransaction().commit();
        session.close();
        return true;
    }

    @Override
    public boolean delete(String code) {
        Session session = HbnUtil.getSession();
        Goods goods = (Goods) session.load(Goods.class, code);
        session.beginTransaction();
        session.delete(goods);
        session.getTransaction().commit();
        session.close();
        return true;
    }

    @Override
    public boolean update(Goods goods, String code) {
        Session session = HbnUtil.getSession();
        session.beginTransaction();
        session.update(goods);
        session.getTransaction().commit();
        session.close();
        return true;
    }

    @Override
    public List<Goods> findAll() {
        String hql = "from Goods";//查找持久化类
        Session session = HbnUtil.getSession();
        session.beginTransaction();
        List<Goods> list = session.createQuery(hql).list();
        session.getTransaction().commit();
        session.close();
        return list;
    }
    @Override
    public List<Goods> findByCondition(Map<String, String> condition) {
        Session session = HbnUtil.getSession();
        session.beginTransaction();
        Criteria criteria = session.createCriteria(Goods.class);
        Iterator<Entry<String,String>> iter = condition.entrySet().iterator();
        while(iter.hasNext()){
            Map.Entry<String, String> enty = iter.next();
            String key = enty.getKey();
            String value = enty.getValue();
            if("code".equals(key)){ 
                criteria.add(Restrictions.like("code", value,MatchMode.ANYWHERE));
            }
            if("name".equals(key)){
                criteria.add(Restrictions.like("name", value,MatchMode.ANYWHERE));
            }
            if("pricegt".equals(key)){
                criteria.add(Restrictions.gt("pricegt", Float.valueOf(value)));
            }
            if("pricelt".equals(key)){
                criteria.add(Restrictions.lt("pricelt", Float.valueOf(value)));
            }
            if("category".equals(key)){
                criteria.add(Restrictions.like("category",value,MatchMode.ANYWHERE));
            }
        }
        List<Goods> list =criteria.list();
        session.getTransaction().commit();
        session.close();
        return list;
    }
    }

GoodsServlet.java

        List<Goods> list = GoodsEBOFactory.getGoodsEBO().findAll();
        if(list==null){
            list = new ArrayList<Goods>();
        }
        PageBean pb = new PageBean();
        pb.setPageSize(5);
        pb.setCurrPage(0);
        pb.setTotalPage(list.size()/pb.getPageSize()+1);
        pb.setPageList(list.subList(0, 1*pb.getPageSize()-1));

        req.getSession().setAttribute("pb", pb);
        req.getSession().setAttribute("list", list);
        RequestDispatcher dispatcher = req.getRequestDispatcher("Goodslist.jsp");
        try {
            dispatcher.forward(req, resp);
        } catch (ServletException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

LoginFilter.java

package zuoye2.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginFilter implements Filter {
    @Override
    public void destroy() {

    }
    String passUrl="";

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        String[] strArray = passUrl.split(";");
        for (String str : strArray) {
            if (str.equals(""))
                continue;
            if (httpRequest.getRequestURL().indexOf(str) >= 0) {
                chain.doFilter(request, response);
                return;
            }
        }

        HttpSession session = httpRequest.getSession();
        if (session.getAttribute("currentZH") != null) {
            chain.doFilter(request, response);
        } else {
            httpResponse.sendRedirect(httpRequest.getContextPath() + "/login.jsp");
        }
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {
        passUrl = arg0.getInitParameter("passUrl");
    }

    }


Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐