HibernateDAO实现类 查询List方法

HibernateDAO实现类 查询List方法
 public List<CertInfoListView> getInfoList(CertInfoListView view) throws DAOException {
  List<CertInfoListView>  liinfo = new ArrayList<CertInfoListView>();
  String sql="select b.usercode,b.agentcode,i.sn,i.cert,i.certstate,i.endtime, i.ipasskeyno,b.id,b.addtime  from certbind b inner join certinfo i on b.ipasskeyno=i.ipasskeyno";
  Session session = HibernateTemplate.getInstance().getSession();
  StringBuffer sb = new StringBuffer(sql);
  sb.append(" where 1=1 ");
  if(!CheckEmpty.isEmpty(view.getIpasskeyno())){
   sb.append(" and i.ipasskeyno ='").append(view.getIpasskeyno()).append("'");
  }
  if(!CheckEmpty.isEmpty(view.getAgentcode())){
   sb.append(" and b.agentcode ='").append(view.getAgentcode()).append("'");
  }
  if(!CheckEmpty.isEmpty(view.getSn())){
   sb.append(" and i.sn ='").append(view.getSn()).append("'");
  }
  if(!CheckEmpty.isEmpty(view.getUsercode())){
   sb.append(" and b.usercode ='").append(view.getUsercode()).append("'");
  }
  if(view.getAddtime()!=null){
   sb.append(" and b.addtime=").append(view.getAddtime());
  }
  sb.append(" order by ipasskeyno ,addtime desc");
  Query q = session.createSQLQuery(sb.toString());
  int pageno = view.getPageno();
  int size = view.getPagesize();
  if(pageno!=0&&size!=0){
   q.setFirstResult((pageno-1)*size);
   q.setMaxResults(size);
  }
  List list = q.list();
  Iterator it = list.iterator();
  while(it.hasNext()){
   Object[] obj = (Object[])it.next();
   CertInfoListView c = new CertInfoListView();
   for(int i=0;i<obj.length;i++){
    if(!CheckEmpty.isEmpty((String)obj[0])){
     c.setUsercode((String)obj[0]);
    }
    if(!CheckEmpty.isEmpty((String)obj[1])){
     c.setAgentcode((String)obj[1]);
    }
    if(!CheckEmpty.isEmpty((String)obj[2])){
     c.setSn((String)obj[2]);
    }
    if(obj[3]!=null){
     SerializableClob sc = (SerializableClob)obj[3];
     String cc = null;
     if(sc!=null){
      try {
       cc = sc.getSubString(1, (int)sc.length());
      } catch (SQLException e) {
      }
     }
     if(cc!=null)c.setCert(cc);
    }
    if(obj[4]!=null){
     BigDecimal b = (BigDecimal)obj[4];
     c.setCertstate(b.intValue());
    }
    if((obj[5])!=null){
     c.setEndtime(((Date)obj[5]));
    }
    if(!CheckEmpty.isEmpty((String)obj[6])){
     c.setIpasskeyno((String)obj[6]);
    }
    c.setCertbindid(Integer.parseInt((String)obj[7]));
    c.setAddtime((Date)obj[8]);
   }
   liinfo.add(c);
  }
  if(session!=null&&session.isConnected())session.close();
  return liinfo;
 }


实现分页的DAO
/*
  * (non-Javadoc)
  *
  * @see com.cns.certservice.dao.CertBindTabDao#getCertBindList(com.cns.certservice.vo.CertBindView)
  */
 public List<CertBindView> getCertBindList(CertBindView view)
   throws DAOException {
  List<CertBindView> li = null;
  List<Certbind> lic = null;
  Session session = null;
  try {
   session = HibernateTemplate.getInstance().getSession();
   Criteria cri = session.createCriteria(Certbind.class);
   Certbind c = getCertBind(view);
   if (c.getAddtime() != null) {
    cri.add(Expression.eq("addtime", c.getAddtime()));
   }
   if (!CheckEmpty.isEmpty(c.getAgentcode())) {
    cri.add(Expression.eq("agentcode", c.getAgentcode()));
   }
   if(c.getId()!=0)cri.add(Expression.eq("id", c.getId()));
   if (!CheckEmpty.isEmpty(c.getIpasskeyno())) {
    cri.add(Expression.eq("ipasskeyno", c.getIpasskeyno()));
   }
   if (!CheckEmpty.isEmpty(c.getUsercode())) {
    cri.add(Expression.eq("usercode", c.getUsercode()));
   }
   if (!CheckEmpty.isEmpty(c.getExtend1())) {
    cri.add(Expression.eq("extend1", c.getExtend1()));
   }
   if (!CheckEmpty.isEmpty(c.getExtend2())) {
    cri.add(Expression.eq("extend2", c.getExtend2()));
   }
   if (c.getAgenttype() != 0) {
    cri.add(Expression.eq("agenttype", c.getAgenttype()));
   }
   int pageno = view.getPageno();
   int size = view.getPagesize();
   if(pageno!=0&&size!=0){
    cri.setFirstResult((pageno-1)*size);
    cri.setMaxResults(size);
   }
   lic = cri.list();
   if (lic != null) {
    li = new ArrayList<CertBindView>();
    for (Certbind cer : lic) {
     CertBindView v = getCertBindView(cer);
     li.add(v);
    }
   }
   
  } catch (HibernateException e) {
   log.error(e,e);
   throw new DAOException(e);
  }finally{
   if (session != null && session.isConnected())
    session.close();
  }
  return li;
 }
 public int getInfoListCount(CertBindView view) throws DAOException {
  int count = 0;
  Session session = null;
  try {
   session = HibernateTemplate.getInstance().getSession();
   Criteria cri = session.createCriteria(Certbind.class);
   Certbind c = getCertBind(view);
   if (c.getAddtime() != null) {
    cri.add(Expression.eq("addtime", c.getAddtime()));
   }
   if (!CheckEmpty.isEmpty(c.getAgentcode())) {
    cri.add(Expression.eq("agentcode", c.getAgentcode()));
   }
   if(c.getId()!=0)cri.add(Expression.eq("id", c.getId()));
   if (!CheckEmpty.isEmpty(c.getIpasskeyno())) {
    cri.add(Expression.eq("ipasskeyno", c.getIpasskeyno()));
   }
   if (!CheckEmpty.isEmpty(c.getUsercode())) {
    cri.add(Expression.eq("usercode", c.getUsercode()));
   }
   if (!CheckEmpty.isEmpty(c.getExtend1())) {
    cri.add(Expression.eq("extend1", c.getExtend1()));
   }
   if (!CheckEmpty.isEmpty(c.getExtend2())) {
    cri.add(Expression.eq("extend2", c.getExtend2()));
   }
   if (c.getAgenttype() != 0) {
    cri.add(Expression.eq("agenttype", c.getAgenttype()));
   }
   count = (Integer)cri.setProjection(Projections.rowCount()).uniqueResult();
   cri.setProjection(null);
  } catch (HibernateException e) {
   log.error(e,e);
   throw new DAOException(e);
  }finally{
   if (session != null && session.isConnected())
    session.close();
  }
  return count;
 }

/*
  * (non-Javadoc)
  *
  * @see com.cns.certservice.dao.CertBindTabDao#updateCertBind(com.cns.certservice.vo.CertBindView)
  */
 public boolean updateCertBind(CertBindView view) throws DAOException {
  boolean res = false;
  if (view.getId()==0) {
   throw new DAOException("getId from CertBindView is null!");
  }
  Session session= null;
  try {
   session = HibernateTemplate.getInstance().getSession();
   Certbind c = (Certbind) session.get(Certbind.class, view.getId());
   if (!CheckEmpty.isEmpty(view.getAgentcode()))
    c.setAgentcode(view.getAgentcode());
   if (!CheckEmpty.isEmpty(view.getExtend1()))
    c.setExtend1(view.getExtend1());
   if (!CheckEmpty.isEmpty(view.getExtend2()))
    c.setExtend2(view.getExtend2());
   if (!CheckEmpty.isEmpty(view.getIpasskeyno()))
    c.setIpasskeyno(view.getIpasskeyno());
   if (!CheckEmpty.isEmpty(view.getUsercode()))
    c.setUsercode(view.getUsercode());
   if (view.getAgenttype() != 0)
    c.setAgenttype(view.getAgenttype());
   res = HibernateTemplate.getInstance().updateObject(c);
  } catch (HibernateException e) {
   log.error(e,e);
   throw new DAOException(e);
  }finally{
   if (session != null && session.isConnected())
    session.close();
  }
  return res;
 }


 private Certbind getCertBind(CertBindView view) {
  Certbind c = new Certbind();
  if (view.getAddtime() != null)
   c.setAddtime(view.getAddtime());
  if (!CheckEmpty.isEmpty(view.getAgentcode()))
   c.setAgentcode(view.getAgentcode());
  if (!CheckEmpty.isEmpty(view.getExtend1()))
   c.setExtend1(view.getExtend1());
  if (!CheckEmpty.isEmpty(view.getExtend2()))
   c.setExtend2(view.getExtend2());
  if (!CheckEmpty.isEmpty(view.getIpasskeyno()))
   c.setIpasskeyno(view.getIpasskeyno());
  if (!CheckEmpty.isEmpty(view.getUsercode()))
   c.setUsercode(view.getUsercode());
  if(view.getId()!=0)c.setId(view.getId());
  if (view.getAgenttype() != 0)
   c.setAgenttype(view.getAgenttype());
  return c;
 }

 private CertBindView getCertBindView(Certbind view) {
  CertBindView c = new CertBindView();
  if (view.getAddtime() != null)
   c.setAddtime(view.getAddtime());
  if (!CheckEmpty.isEmpty(view.getAgentcode()))
   c.setAgentcode(view.getAgentcode());
  if (!CheckEmpty.isEmpty(view.getExtend1()))
   c.setExtend1(view.getExtend1());
  if (!CheckEmpty.isEmpty(view.getExtend2()))
   c.setExtend2(view.getExtend2());
  if (!CheckEmpty.isEmpty(view.getIpasskeyno()))
   c.setIpasskeyno(view.getIpasskeyno());
  if (!CheckEmpty.isEmpty(view.getUsercode()))
   c.setUsercode(view.getUsercode());
  if(view.getId()!=0)c.setId(view.getId());
  if (view.getAgenttype() != 0)
   c.setAgenttype(view.getAgenttype());
  return c;
 }

你可能感兴趣的:(HibernateDAO实现类 查询List方法)