ssh之雇员管理系统(2)-hibernate测试

本文通过创建Employee类并实现与SQLServer2005数据库的交互,展示如何使用hibernate进行数据持久化操作,包括添加、删除、查看和更新雇员信息。

二、测试hibernate是否配置成功

  1. 首先我们测试hibernate的话,那必须连接数据库,这里用的数据库为sqlserver2005
  2. 编写雇员domain的bean类,根据数据库的字段,建立bean字段。
  3. 建立逻辑,那逻辑的话,怎样建立呢?spring是面向借口的编程,那当然会有接口啦,ok,接口的话有逻辑接口,就是如,雇员添加、删除、查看、更新操作等。
  4. 有接口那就有实现,好,那我们就建立一个实现的类,去实现刚刚我们编写的接口,编写相应的方法的实现。
  5. 那现在我们要对应上数据库的字段了啊,刚刚已经建好的bean类,根据bean类建立一个x.hbm.xml的文件进行和数据库的字段一一对应。
  6. 测试类Test,测试hibernate是否成功,

下面是具体的代码和实现。。。。

  1. 数据库表employee                                
  2. bean类 Employee.java
    View Code
    package com.wang.domain;
     
     import java.util.Date;
     
     public class Employee {
     
         private Integer id;
         private String name;
         private String email;
         private String pwd;
         private java.util.Date hiredate;
         private Float salary;
         
         public Employee(){}
     
         public Employee(String name, String email, Date hiredate, Float salary) {
             this.name = name;
             this.email = email;
             this.hiredate = hiredate;
             this.salary = salary;
         }
     
         public String getPwd() {
            return pwd;
        }
    
        public void setPwd(String pwd) {
            this.pwd = pwd;
        }
    
        public Integer getId() {
             return id;
         }
         public void setId(Integer id) {
             this.id = id;
         }
         public String getName() {
             return name;
         }
         public void setName(String name) {
             this.name = name;
         }
         public String getEmail() {
             return email;
         }
         public void setEmail(String email) {
             this.email = email;
         }
     
         public java.util.Date getHiredate() {
             return hiredate;
         }
     
         public void setHiredate(java.util.Date hiredate) {
             this.hiredate = hiredate;
         }
     
         public Float getSalary() {
             return salary;
         }
         public void setSalary(Float salary) {
             this.salary = salary;
         }
     }

     

  3. 逻辑 接口EmployeeServiceInter.java
    View Code
    package com.wang.service.interfaces;
     
     import java.util.List;
     
     import com.wang.domain.Employee;
     
     public interface EmployeeServiceInter {
     
         //增加雇员方法
         public void addEmployee(Employee e);
         //显示雇员方法 用list集合
         public List<Employee> showEmployee();
         //根据id号删除雇员 这里用所有类型的serializable关键字
         public void delEmployee(java.io.Serializable id);
         //更新雇员的方法
         public void updateEmployee(Employee e);
     }

     

  4. 逻辑实现 EmployeeService.java
    View Code
    package com.wang.service.imp;
    
    import java.io.Serializable;
    import java.util.List;
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    
    import com.wang.domain.Employee;
    import com.wang.service.interfaces.EmployeeServiceInter;
    
    public class EmployeeService implements EmployeeServiceInter {
    
        public void addEmployee(Employee e) {
        }
    
        public void delEmployee(Serializable id) {
            // TODO Auto-generated method stub
    
        }
    
        public List<Employee> showEmployee() {
            // TODO Auto-generated method stub
            return null;
        }
    
        public void updateEmployee(Employee e) {
            // TODO Auto-generated method stub
    
        }
    
    }

     

  5. x.hbm.xml的文件Employee.hbm.xml
    View Code
    <?xml version="1.0" encoding="UTF-8"?>
     <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
     <hibernate-mapping package="com.wang.domain">
         <class name="Employee" table="employee">
         <!-- 主键策略 -->
         <id name="id" type="java.lang.Integer">
             <generator class="native" />
         </id>
         <property name="email" type="java.lang.String">
             <column name="email" length="64"></column>
         </property>
         <property name="hiredate" type="java.util.Date">
             <column name="hiredate"></column>
         </property>
         <property name="name" type="java.lang.String">
             <column name="name" length="64"></column>
         </property>
         <property name="salary" type="java.lang.Float">
             <column name="salary"></column>
         </property>
         </class>
     </hibernate-mapping>

     

  6. Test.java
    View Code
    package com.wang.ssh;
    
    import org.apache.bsf.util.event.adapters.java_awt_event_ActionAdapter;
    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.wang.domain.Employee;
    import com.wang.service.imp.EmployeeService;
    import com.wang.service.interfaces.EmployeeServiceInter;
    
    public class Test {
        public static void main(String[] args) {
            /**
             * 测试Spring配置是否成功
             */
    //        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
    //        TestServcie testServcie = (TestServcie)ac.getBean("testServcie");
    //        System.out.println(testServcie.getName()+"***");
            
            /**
             * 测试hibernater是否配置成功
             */
            ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
            SessionFactory sessionFactory= (SessionFactory) ac.getBean("sessionFactory");
            Session session =  sessionFactory.openSession();
            Employee employee = new Employee("Tom", "123@qq.com", new java.util.Date(), 123.45f);
            Transaction transaction = session.beginTransaction();
            session.save(employee);
            transaction.commit();
        }
    
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值