联合查询jjjj

(利用spring、springMVC、hibernate实现对员工表的联合查询)

  • 联合查询思路

通过从客户端传回所需查询数据库字段条件,然后通过服务端根据客户端传过来的条件对数据库进行查询

客户端联合查询传值方式代码:


html代码

输入区

后台联合查询逻辑代码:

package controller;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import bean.EmpBean;
import dao.EmpDao;

@Controller
public class EmpController {
    private final static Log log = LogFactory.getLog(EmpController.class);
    
    @Resource
    private EmpDao empDao;
    
    @RequestMapping(value="/findAllEmp.do")
    public ModelAndView findAllEmp(ModelAndView mv,String empno, String ename, String job, String deptno) {
        StringBuffer hql = new StringBuffer("from EmpBean t where 1=1 ");
        if(StringUtils.isNotBlank(job)) {
            hql.append(" and job like upper('%"+ job +"%')");       //模糊查询这个字段中有客户端传过来的字符数据
            mv.addObject("job",job);                    //将传入服务端的数据传回客户端,进行条件回显
        }
        if(StringUtils.isNotBlank(empno)) {
            hql.append(" and empno like upper('%"+ empno +"%')");
            mv.addObject("empno",empno);
        }
        if(StringUtils.isNotBlank(ename)) {
            hql.append(" and ename like upper('%"+ ename +"%')");
            mv.addObject("ename",ename);
        }
        if(StringUtils.isNotBlank(deptno)) {
            hql.append(" and deptno like upper('%"+ deptno +"%')");
            mv.addObject("deptno",deptno);
        }
        hql.append(" order by empno");      //排序
        List listEmp = empDao.findListByHql(hql.toString());
        mv.addObject("listEmp",listEmp);
        mv.setViewName("emp_manage");
        return mv;
    }
    
}

Dao层查询方法代码:

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import bean.EmpBean;

@Repository
@Transactional
public class EmpDao {
    
    @Resource 
    private SessionFactory sessionFactory;
    
    //获取会话
    public Session getSession() {
        return sessionFactory.getCurrentSession();
    }
    
    //查询所有
    public List findListByHql(String hql){
        List list = new ArrayList();
        try {
            Session session = getSession();
            Query query = session.createQuery(hql);
            list = query.list();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return list;
    }
}

(bean层省略)springMVC配置



    
    
    
    
    
    
    
        
                 
                            
            
        

spring配置:




    
    
    
        
            ${jdbc.driverClassName}
        
        
            ${jdbc.url}
        
        
            ${jdbc.username}
        
        
            ${jdbc.password}
        
        
            50
        
        
            5
        
        
            50
        
        
            -1
        
        
            false
        
         
         
         
         
         
          
         
    

    
    
        
            
        
    

    
        
            
                
                org.hibernate.dialect.Oracle10gDialect
                true 
                true
                
                500  
                false
                org.hibernate.cache.ehcache.EhCacheRegionFactory
                
                org.springframework.orm.hibernate4.SpringSessionContext
            
        
        
            
        
        
            
                classpath:/hbm/
            
        
        
    
    
    
        
    
     
 
 

jdbc.properties文件配置

jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc\:oracle\:thin\:@127.0.0.1\:1521\:oracle10
jdbc.username=emp
jdbc.password=workit

web.xml配置:



  
    entrance.jsp
  
   
   
    characterEncodingFilter
    org.springframework.web.filter.CharacterEncodingFilter
    
      encoding
      UTF-8
    
    
      forceencoding
      true
    
  
  
   
    characterEncodingFilter
    /*
  

  
  
  
    springMVC
    org.springframework.web.servlet.DispatcherServlet
    
        contextConfigLocation
        /WEB-INF/spring-conf/*
    
    1
  
  
  
    springMVC
    *.do
  


大概文件架构(省略了大部分项目搭建思想)


你可能感兴趣的:(联合查询jjjj)