weixin_41984131 2019-06-20 09:04
浏览 171

正确导入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");
    }

    }


  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 训练的多模态特征融合模型准确度很低怎么办
    • ¥15 kylin启动报错log4j类冲突
    • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
    • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
    • ¥15 onvif+openssl,vs2022编译openssl64
    • ¥15 iOS 自定义输入法-第三方输入法
    • ¥15 很想要一个很好的答案或提示
    • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
    • ¥15 怀疑手机被监控,请问怎么解决和防止
    • ¥15 Qt下使用tcp获取数据的详细操作