二、测试hibernate是否配置成功
- 首先我们测试hibernate的话,那必须连接数据库,这里用的数据库为sqlserver2005
- 编写雇员domain的bean类,根据数据库的字段,建立bean字段。
- 建立逻辑,那逻辑的话,怎样建立呢?spring是面向借口的编程,那当然会有接口啦,ok,接口的话有逻辑接口,就是如,雇员添加、删除、查看、更新操作等。
- 有接口那就有实现,好,那我们就建立一个实现的类,去实现刚刚我们编写的接口,编写相应的方法的实现。
- 那现在我们要对应上数据库的字段了啊,刚刚已经建好的bean类,根据bean类建立一个x.hbm.xml的文件进行和数据库的字段一一对应。
- 测试类Test,测试hibernate是否成功,
下面是具体的代码和实现。。。。
- 数据库表employee
- 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; } }
- 逻辑 接口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); }
- 逻辑实现 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 } }
- 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>
- 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(); } }