十五、抽象出基础接口

1.将EmployeeServie和departmentService中共有的方法(如增加,执行sql语句的查询,通过id查询出对象等)抽取出来

1.1新建包com.myz.basic,在包下新建基础接口BasicServiceInterface

package com.myz.basic;

import java.io.Serializable;
import java.util.List;

public interface BasicServiceInterface {

    public Object getObjectByid(Class clazz,Serializable id);//通过id获取对象
    
    public List excuteQuery(String hql,Object parameters[]);//执行hql语句查询对象,返回list
    
    public List excuteQueryByPage(String hql,Object parameters[],int pageNow,int pageSize);//带分页的hql查询方法
    
    public void add(Object obj);//添加一个对象
    
    public Object uniqueQuery(String hql,Object parameters[]);//执行hql语句查询对象,返回唯一的一条记录
    
}

1.2在包com.myz.basic包下新建类BasicService,实现BasicServiceInterface接口

package com.myz.basic;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;

@Transactional
public class BasicService implements BasicServiceInterface {

    @Resource
    private SessionFactory sessionFactory;
    
    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public void add(Object obj) {
        // TODO Auto-generated method stub
        sessionFactory.getCurrentSession().save(obj);
    }

    public List excuteQuery(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 excuteQueryByPage(String hql, Object[] parameters, int pageNow,
            int pageSize) {
        // 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)*pageSize).setMaxResults(pageSize).list();
    }

    public Object getObjectByid(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();
    }

}

1.3在applicationContext.xml中配置BasicService

<bean name="basicService" class="com.myz.basic.BasicService"></bean>

 

2.修改DepartmentService

  2.1修改DepartmentServiceInterface

public interface DepartmentServiceInterface extends BasicServiceInterface{
    //Department有独立的方法再声明到这里
}

  2.2修改DepartmentService,需要的函数都在父类里了,自己不用写任何函数

public class DepartmentService extends BasicService implements DepartmentServiceInterface {


}

 

3.修改EmployeeService

  3.1修改EmployeeServiceInterface,仅有一个登录验证 的独立方法需要保留

public interface EmployeeServiceInterface extends BasicServiceInterface{
    public Employee loginCheck(Employee e);//登录验证
}

  3.2修改EmployeeService,重写其loginCheck方法

public class EmployeeService extends BasicService implements EmployeeServiceInterface {
    
    
    //登录验证,如果验证成功则返回该employee对象,否则返回空
    public Employee loginCheck(Employee e) {
        // TODO Auto-generated method stub
        String hql="from Employee where id=? and password=?";
        Object[] parameters={e.getId(),e.getPassword()};
        
        List list = excuteQuery(hql, parameters);
        if(list.size()>0) return (Employee) list.get(0);
        else return null;
    }
    
    

}

 

4.修改EmployeeAction中的addEmp方法

//添加雇员
    public ActionForward addEmp(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        // TODO Auto-generated method stub
        EmployeeForm employeeForm=(EmployeeForm) form;
        
        Employee e=new Employee();
        e.setName(employeeForm.getName());
        e.setEmail(employeeForm.getEmail());
        e.setGrade(employeeForm.getGrade());
        e.setId(employeeForm.getId());
        e.setHiredate(new Date());
        e.setPassword(employeeForm.getPassword());
        e.setSalary(employeeForm.getSalary());
        e.setDepartment((Department)departmentService.getObjectByid(Department.class, employeeForm.getId()));
        
        try {
            employeeService.add(e);
        } catch (Exception e2) {
            // TODO: handle exception
            return mapping.findForward("err");
        }
        return mapping.findForward("ok");
    }

 

5.测试,依然能够执行登录、增加员工的操作,成功!

posted @ 2018-02-12 13:20  菜鸡蔡文姬  阅读(148)  评论(0编辑  收藏  举报