zoukankan      html  css  js  c++  java
  • 三、搞定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);
        }
    }
  • 相关阅读:
    ELK Kafka json to elk
    ElasticSearch 日期赋值
    ELK Nxlog->Kafka->ElasticSearch
    Windows 安装Kafka
    图论路径简单算法
    鉴以往而知未来
    细微之处显文化
    Alt+数字键所能打出的符号表
    《愚公移山》与愚公精神
    Wikipedia's World
  • 原文地址:https://www.cnblogs.com/myz666/p/8434267.html
Copyright © 2011-2022 走看看