最简单的hibernate整合spring(1)
http://feichen00.blog.sohu.com/91380336.html
利用spring提供的HibernateDaoSupport类来简化Hibernate的事务操作:
实体类:
Hibernate实体类映射文件Person.hbm.xml:
测试类:(记得根据实际数据来编写测试数据)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 {@BeforeClasspublic static void setUpBeforeClass() throws Exception {}@Testpublic 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);}@Testpublic void get() { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); PersonDaoImpl personDaoImpl = (PersonDaoImpl)ctx.getBean("PersonDaoImpl"); Person person = personDaoImpl.get(1); System.out.println(person.getName());}@Testpublic 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);}@Testpublic 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可以从附件获得。