利用spring提供的HibernateDaoSupport类来简化Hibernate的事务操作:
实体类:
package com.entity; public class Person { private Integer id; private String name; 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; } }
DAO接口类:
package com.dao; import com.entity.Person; public interface PersonDao { Person get(int id); void save(Person person); void update(Person person); void delete(Person person); }
DAO接口实现类:
package com.impl; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import com.dao.PersonDao; import com.entity.Person; public class PersonDaoImpl extends HibernateDaoSupport implements PersonDao { @Override public Person get(int id) { return (Person)getHibernateTemplate().get(Person.class, new Integer(id)); } @Override public void save(Person person) { getHibernateTemplate().save(person); } @Override public void update(Person person) { Person p = (Person)getHibernateTemplate().load(Person.class, new Integer(person.getId())); getHibernateTemplate().update(person); } @Override public void delete(Person person) { Person p = (Person)getHibernateTemplate().load(Person.class, new Integer(person.getId())); getHibernateTemplate().delete(p); } }
spring配置文件applicationContext.xml:
<?xml version="1.0" encoding="gb2312"?> <!-- Spring配置文件的DTD定义 --> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <!-- Spring配置文件的根元素是beans --> <beans> <!--定义数据源,该bean的ID为dataSource --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <!-- 指定数据库驱动 --> <property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> </property> <!-- 指定连接数据库的URL --> <property name="url"> <value>jdbc:mysql://localhost:3306/lee</value> </property> <!-- root为数据库的用户名 --> <property name="username"> <value>root</value> </property> <!-- pass为数据库密码 --> <property name="password"> <value>root</value> </property> </bean> <!--定义Hibernate的SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- 依赖注入数据源,注入正是上文定义的dataSource --> <property name="dataSource"> <ref local="dataSource" /> </property> <!-- mappingResouces属性用来列出全部映射文件 --> <property name="mappingResources"> <list> <!--以下用来列出所有的PO映射文件 --> <value>Person.hbm.xml</value> </list> </property> <!--定义Hibernate的SessionFactory的属性 --> <property name="hibernateProperties"> <props> <!-- 指定Hibernate的连接方言 --> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <!-- 不同数据库连接,启动时选择create,update,create-drop --> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> </bean> <!-- 配置Person持久化类的DAO bean --> <bean id="PersonDaoImpl" class="com.impl.PersonDaoImpl"> <!-- 采用依赖注入来传入SessionFactory的引用 --> <property name="sessionFactory"> <ref local="sessionFactory" /> </property> </bean> </beans>
Hibernate实体类映射文件Person.hbm.xml:
<?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> <class name="com.entity.Person" table="person"> <!-- 配置主键信息 --> <id name="id" column="ID" type="integer"> <generator class="identity"></generator> </id> <!-- 配置属性信息,默认type为string类型,所以不需要配置 --> <property name="name" column="NAME" not-null="true" type="string"></property> </class> </hibernate-mapping>
测试类:(记得根据实际数据来编写测试数据)
package com.test; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.entity.Person; import com.impl.PersonDaoImpl; public class TestHibernate { @BeforeClass public static void setUpBeforeClass() throws Exception { } @Test public void save() { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); PersonDaoImpl personDaoImpl = (PersonDaoImpl)ctx.getBean("PersonDaoImpl"); System.out.println(personDaoImpl); Person person = new Person(); person.setName("Tom"); personDaoImpl.save(person); } @Test public void get() { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); PersonDaoImpl personDaoImpl = (PersonDaoImpl)ctx.getBean("PersonDaoImpl"); Person person = personDaoImpl.get(1); System.out.println(person.getName()); } @Test public void update() { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); PersonDaoImpl personDaoImpl = (PersonDaoImpl)ctx.getBean("PersonDaoImpl"); System.out.println(personDaoImpl); Person person = new Person(); person.setId(1); person.setName("猫"); personDaoImpl.update(person); } @Test public void delete() { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); PersonDaoImpl personDaoImpl = (PersonDaoImpl)ctx.getBean("PersonDaoImpl"); System.out.println(personDaoImpl); Person person = new Person(); person.setId(2); personDaoImpl.delete(person); } }
其他的结构图,lib可以从附件获得。