• 三、搞定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);
        }
    }
  • 相关阅读:
    那些年搞不懂的多线程、同步异步及阻塞和非阻塞(一)---多线程简介
    java中IO流详细解释
    Java IO流学习总结
    MySQL数据库中索引的数据结构是什么?(B树和B+树的区别)
    使用Redis存储Nginx+Tomcat负载均衡集群的Session
    Redis 3.2.4集群搭建
    JDK1.8源码分析之HashMap
    java HashMap和LinkedHashMap区别
    Java中原子类的实现
    多线程-传统定时任务
  • 原文地址:https://www.cnblogs.com/myz666/p/8434267.html
Copyright © 2020-2023  润新知