使用SSH完成條件及分頁查詢的主要程式碼

Gadxiong發表於2018-07-23

Dao層:

@Repository("empDao")

public EmpDaoImpl implements EmpDao{

@Resource(name="sessionFactory")
private SessionFactory sessionFactory;
@Override
public List<Emp> selectBynameordept(String name, int dept,int first,int fast) {
    StringBuffer sb = new StringBuffer();

    sb.append("FROM Emp e where 1=1");
    if(dept!=0){
        sb.append(" and e.deptno="+dept+" ");
    }
    if(!"".equals(name)){
        sb.append(" and e.empname LIKE '%"+name+"%' ");
    }
    Query query = sessionFactory.getCurrentSession().createQuery(sb.toString());

    List<Emp> list =query.setFirstResult(first)//從查出來的資料中第幾個資料開始顯示
            .setMaxResults(fast)//一次顯示多少條資料
            .list();//轉換成List型別

    return list;
}

}

實現類:

package com.demo.controller;

import com.demo.dao.EmpDao;
import com.demo.pojo.Dept;
import com.demo.pojo.Emp;
import com.demo.service.EmpService;
import com.demo.util.Page;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller("addAction")
@Scope("prototype")
public class AddAction extends ActionSupport implements SessionAware,RequestAware,ApplicationAware {
    @Resource
    private EmpService empService;
    private Map<String,Object> requset;
    private Map<String,Object> session;
    private Map<String,Object> application;
    private Emp emp ;

    private Page page ;



    public Page getPage() {
        return page;
    }

    public void setPage(Page page) {
        this.page = page;
    }

    public Emp getEmp() {
        return emp;
    }

    public void setEmp(Emp emp) {
        this.emp = emp;
    }

    @Override
    public void setApplication(Map<String, Object> map) {
        this.application=map;
    }

    @Override
    public void setRequest(Map<String, Object> map) {
        this.requset=map;
    }

    @Override
    public void setSession(Map<String, Object> map) {
        this.session=map;
    }

    /**
     * 條件查詢及分頁
     * @return
     */
    public String add(){

        //查詢所有部門
        List<Dept> depts = empService.selectDept();


        //判斷emp是否等於傳過來得值
        if("1".equals(emp.getEmpname())){
            System.out.println(emp.getEmpname());
            emp.setEmpname("");
            emp.setDeptno(0);
        }

        page.setIs_rows(3);//每頁顯示多少條資料
        page.setIs_page(page.getIs_page());//當前頁碼
        int i = empService.selectAll(emp.getEmpname(), emp.getDeptno());//查詢此條件下有多少條資料
        System.out.println("一共有"+i+"條資料");
        page.setIs_allrow(i);//把總條數賦值給page
        //查詢全部
        System.out.println("姓名:"+emp.getEmpname()+",編號:"+emp.getDeptno());

        List<Emp> list = empService.selectBynameordept(emp.getEmpname(), emp.getDeptno(), (page.getIs_page() - 1) * page.getIs_rows(), page.getIs_rows());

        requset.put("pages",page);
        requset.put("name",emp.getEmpname());
        requset.put("depts",depts);
        requset.put("Lists",list);
        return "index";
    }


}

 

相關文章