spring hibernate struts 整合开发(2)
一. spring hibernate struts 整合开发(1) - 搭建环境
二. spring hibernate struts 整合开发(2) - Spring集成的Hibernate编码和测试
三. spring hibernate struts 整合开发(3) - Struts集成Spring
四. spring hibernate struts 整合开发(4) - Struts与Spring集成2
五. spring hibernate struts 整合开发(5) - Hibernate二级缓存
六. spring hibernate struts 整合开发(6) - 额外功能
Spring集成的Hibernate编码与测试
1. 新建一个业务bean:com.john.service.impl.PersonServiceBean
@Transactionalpublic class PersonServiceBean implements PersonService {@Resource private SessionFactory sessionFactory;public void save(Person person) {sessionFactory.getCurrentSession().persist(person);}public void update(Person person) {sessionFactory.getCurrentSession().merge(person);}@Transactional(propagation=Propagation.NOT_SUPPORTED, readOnly=true)public Person getPerson(Integer id) {return (Person)sessionFactory.getCurrentSession().get(Person.class, id);}public void delete(Integer id) {sessionFactory.getCurrentSession().delete(sessionFactory.getCurrentSession().load(Person.class, id));}@Transactional(propagation=Propagation.NOT_SUPPORTED, readOnly=true)public List<Person> getPersons() {return sessionFactory.getCurrentSession().createQuery("from Person").list();}public class PersonServiceTest {private static PersonService personService; @BeforeClasspublic static void setUpBeforeClass() throws Exception {ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");personService = (PersonService)ctx.getBean("personService");}@Testpublic void testSave() throws UnsupportedEncodingException {Person person = new Person();person.setName("John");personService.save(person);}@Testpublic void testUpdate() {Person person = personService.getPerson(1);person.setName("Lisa");personService.update(person);}@Testpublic void testGetPerson() {Person person = personService.getPerson(1);System.out.println(person.getName());}@Testpublic void testDelete() {personService.delete(1);}@Testpublic void testGetPersons() {List<Person> persons = personService.getPersons();for (Person p : persons)System.out.println(p.getName());}