三、搞定Service接口和实现类

1.建包com.myz.service.interfaces,用于存放接口,包com.myz.service.imps,用于存放实现类

2.包com.myz.service.interfaces下新建接口EmployeeServiceInterface

package com.myz.service.interfaces;

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

import com.myz.domain.Employee;

public interface EmployeeServiceInterface {
    public void addEmployee(Employee e);//添加雇员
    public List<Employee> showEmployee();//显示所有雇员
    public void updEmployee(Employee e);//更新雇员
    public void delEmployee(Serializable id);//根据id删除雇员
}

3.包com.myz.service.imps下新建EmployeeService类,并编写

package com.myz.service.imps;

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

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.myz.domain.Employee;
import com.myz.service.interfaces.EmployeeServiceInterface;

public class EmployeeService implements EmployeeServiceInterface {

    //增加雇员
    public void addEmployee(Employee e) {
        // TODO Auto-generated method stub
        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
        SessionFactory sf=(SessionFactory) ac.getBean("sessionFactory");
        Session openSession = sf.openSession();
        Transaction ts=openSession.beginTransaction();
        openSession.save(e);
        ts.commit();
    }
    
    //根据id删除雇员
    public void delEmployee(Serializable id) {
        // TODO Auto-generated method stub
        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
        SessionFactory sf=(SessionFactory) ac.getBean("sessionFactory");
        Session openSession = sf.openSession();
        Transaction ts=openSession.beginTransaction();
        Employee e=(Employee) openSession.load(Employee.class, id);
        openSession.delete(e);
        ts.commit();
    }

    //显示所有雇员
    public List<Employee> showEmployee() {
        // TODO Auto-generated method stub
        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
        SessionFactory sf=(SessionFactory) ac.getBean("sessionFactory");
        Session openSession = sf.openSession();
        List<Employee> list = openSession.createQuery("from Employee").list();
        return list;
    }

    //更新雇员信息
    public void updEmployee(Employee e) {
        // TODO Auto-generated method stub
        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
        SessionFactory sf=(SessionFactory) ac.getBean("sessionFactory");
        Session openSession = sf.openSession();
        Transaction ts=openSession.beginTransaction();
        Employee ee=(Employee) openSession.load(Employee.class, e.getId());
        ee.setEmail(e.getEmail());
        ee.setGrade(e.getGrade());
        ee.setHiredate(e.getHiredate());
        ee.setName(e.getName());
        ee.setPassword(e.getPassword());
        ee.setSalary(e.getSalary());
        ts.commit();
    }

}

4.测试,对EmployeeService中的函数逐个测试,成功!

package com.myz.test;

import java.util.Date;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.myz.domain.Employee;
import com.myz.service.imps.EmployeeService;

public class Test {
    public static void main(String[] args) {
        EmployeeService es=new EmployeeService();
        
        //增加一个雇员
//        Employee e1=new Employee(3, "marry", "2466@qq.com", new Date(), 4500f, "123456", 1);
//        es.addEmployee(e1);
        
        //根据id删除一个雇员
//        es.delEmployee(3);
        
        //显示所有雇员
//        List<Employee> employees = es.showEmployee();
//        for(Employee e:employees){
//            System.out.println(e.getId()+" "+e.getName());
//        }
        
        //更新3号雇员的信息
//        Employee e3=new Employee(3, "rose", "24666@163.com", new Date(), 6000f, "123456", 2);
//        es.updEmployee(e3);
    }
}

 

posted @ 2018-02-09 09:55  菜鸡蔡文姬  阅读(1015)  评论(0编辑  收藏  举报