雇员管理系统-SSH版(八)

优化业务类
一 BasicServiceInter
package com.hsp.basic;
import java.util.List;
public interface BasicServiceInter {
        //声明一些常用的方法
        //1.通过id获取对象
        public Object findById(Class clazz,java.io.Serializable id);
        //2.查询方法 hql
        public List executeQuery(String hql,Object []parameters);
        //3.查询方法 带分页
        public List executeQueryByPage(String hql,Object []parameters,int pageNow,int PagesSize);
        //4.添加一个对象
        public void add(Object obj);
        //5.统一的执行hql->删除,修改 hql="update domain对象 where ?"
        public List executeUpdate(String hql,Object []parameters);
        //6.返回一个对象的操作
        public Object uniqueQuery(String hql,Object []parameters);
        //7.得到hql,返回pageCount
        public int queryPageCount(String hql,Object [] parameters,int pageSize);
        //8.根据id号删除对象
        public void delById(Class clazz,java.io.Serializable id);
        //9.通过Object修改
        public void update(Object object); 
}

二 BasicService
package com.hsp.basic;
import java.io.Serializable;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;
@Transactional
public abstract class BasicService implements BasicServiceInter {
    @Resource
    private SessionFactory sessionFactory;
    
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
    public void add(Object obj) {
        
        this.sessionFactory.getCurrentSession().save(obj);
    }
    //统一查询方法(hql)
    public List executeQuery(String hql, Object[] parameters) {
        // TODO Auto-generated method stub
        
        Query query=this.sessionFactory.getCurrentSession().createQuery(hql);
        //注入?值
        if(parameters!=null && parameters.length>0){
            for(int i=0;i<parameters.length;i++){
                query.setParameter(i, parameters[i]);
                
            }
        }
        
        return query.list();
    }
    //分页
    public List executeQueryByPage(String hql, Object[] parameters,
            int pageNow, int PagesSize) {
        // TODO Auto-generated method stub
        Query query=this.sessionFactory.getCurrentSession().createQuery(hql);
        
        if(parameters!=null && parameters.length>0){
            for(int i=0;i<parameters.length;i++){
                query.setParameter(i, parameters[i]);
                
            }
        }
    
        //体现分页
        return query.setFirstResult((pageNow-1)*PagesSize).setMaxResults(PagesSize).list();
    }
    //统一的修改和删除
    public List executeUpdate(String hql, Object[] parameters) {
        // TODO Auto-generated method stub
        return null;
    }
    public Object findById(Class clazz, Serializable id) {
        // TODO Auto-generated method stub
        return this.sessionFactory.getCurrentSession().get(clazz, id);
    }
    public Object uniqueQuery(String hql, Object[] parameters) {
        // TODO Auto-generated method stub
        Query query=this.sessionFactory.getCurrentSession().createQuery(hql);
        //给?赋值
        if(parameters!=null && parameters.length>0){
            for(int i=0;i<parameters.length;i++){
                query.setParameter(i, parameters[i]);
            }
        }
        
        return query.uniqueResult();
    }
    
    public int queryPageCount(String hql, Object[] parameters, int pageSize) {
        // TODO Auto-generated method stub
        //获取rowCount
    /*    List list=this.executeQuery(hql, parameters);
        Iterator iteator=list.iterator();
        if(iteator.hasNext()){
            
        }*/
        Object obj=this.uniqueQuery(hql, parameters);
        //System.out.println("obj value= "+ obj);//obj如果等于rowConunt
        int rowCount=Integer.parseInt(obj.toString());
        
        
        return (rowCount-1)/pageSize+1;
    }
    
    public void delById(Class clazz,Serializable id) {
        // TODO Auto-generated method stub
        Session session=this.sessionFactory.getCurrentSession();
        session.delete(this.findById(clazz, id));
    }
    
    public void update(Object object) {
        // TODO Auto-generated method stub
        this.sessionFactory.getCurrentSession().update(object);
    }
}

三 DepartmentServiceInter
package com.hsp.service.interfaces;
import com.hsp.basic.BasicServiceInter;
import com.hsp.domain.Department;
public interface DepartmentServiceInter extends BasicServiceInter{
    //1.如果有比较特殊的方法,则可以在这里声明.
    
}

四 EmployeeServiceInter
package com.hsp.service.interfaces;
import java.util.List;
import com.hsp.basic.BasicServiceInter;
import com.hsp.domain.Employee;
public interface EmployeeServiceInter extends BasicServiceInter {
    public List showEmployList(int pageSize,int pageNow);
    //如果该雇员存在,则返回该雇员的完整信息,否则返回null
    public Employee checkEmploye(Employee e);
    
    public int getPageCount(int pageSize);
    
    
}

五 DepartmentService
package com.hsp.service.imp;
import java.io.Serializable;
import javax.annotation.Resource;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;
import com.hsp.basic.BasicService;
import com.hsp.domain.Department;
import com.hsp.service.interfaces.DepartmentServiceInter;
public class DepartmentService extends BasicService implements DepartmentServiceInter {

}

六 EmployeeService
package com.hsp.service.imp;
import java.io.Serializable;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.transaction.annotation.Transactional;
import com.hsp.basic.BasicService;
import com.hsp.domain.Department;
import com.hsp.domain.Employee;
import com.hsp.service.interfaces.EmployeeServiceInter;
//这里配置@Transactional用处是让spring的事务管理器接管该 Service的事务.
public class EmployeeService extends BasicService implements EmployeeServiceInter {
    //显示所有雇员
    public List showEmployList(int pageSize,int pageNow){
        String hql="from Employee order by id";
        return this.executeQueryByPage(hql, null, pageNow, pageSize);
    }
    
    public int getPageCount(int pageSize){
        String hql="select count(*) from Employee";
        return this.queryPageCount(hql, null, pageSize);
    }
    //验证用户
    public Employee checkEmploye(Employee e) {
        // TODO Auto-generated method stub
        System.out.println("checkEmploye");
        String hql="from Employee where id=? and pwd=?";
        Object []parameters ={e.getId(),e.getPwd()};
        
        List list=this.executeQuery(hql, parameters);
        if(list.size()==0){
            return null;
        }else{
            return (Employee) list.get(0);
        }
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值