Spring.xml
ITestDao.java
TestDaoImpl.java
DomainObject.java
Client.java
===============Spring.xml===============
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <bean id="placeHolder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" p:locations="classpath:jdbc.properties" /> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="annotatedClasses"> <list> <value> com.spring.ch11.dao.hibernate.daosupport.DomainObject </value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQL5InnoDBDialect </prop> </props> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <tx:annotation-driven transaction-manager="transactionManager" /> <bean id="com.spring.ch11.dao.hibernate.daosupport.TestDaoImpl" class="com.spring.ch11.dao.hibernate.daosupport.TestDaoImpl" p:sessionFactory-ref="sessionFactory"> </bean> </beans>
===============ITestDao.java===============
package com.spring.ch11.dao.hibernate.daosupport;
import java.util.List;
import org.hibernate.HibernateException;
import org.springframework.transaction.annotation.Transactional;
public interface ITestDao {
public List<DomainObject> getAll();
public DomainObject getByIdWithQuery(int id);
public DomainObject getByIdWithSession(int id);
@Transactional(rollbackFor = HibernateException.class, noRollbackFor = Throwable.class)
public void save(DomainObject domainObject);
}
===============TestDaoImpl.java===============
package com.spring.ch11.dao.hibernate.daosupport; import java.util.List; import org.hibernate.HibernateException; import org.hibernate.Session; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; public class TestDaoImpl extends HibernateDaoSupport implements ITestDao { public List<DomainObject> getAll() { return getSession().createQuery("from DomainObject").list(); } @Override public DomainObject getByIdWithQuery(int id) { return (DomainObject) getHibernateTemplate().find( "from DomainObject d where d.id = " + id).get(0); } @Override public DomainObject getByIdWithSession(int id) { Session session = getSession(); try { return (DomainObject) session.createQuery( "from DomainObject d where d.id = " + id).uniqueResult(); } catch (HibernateException e) { throw convertHibernateAccessException(e); } } @Override public void save(DomainObject domainObject) { getSession().save(domainObject); } }
===============DomainObject.java===============
package com.spring.ch11.dao.hibernate.daosupport; import java.io.Serializable; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = "demo") public class DomainObject implements Serializable { private int id; private float unitPrice; private String name; @Id public int getId() { return id; } public void setId(int id) { this.id = id; } public float getUnitPrice() { return unitPrice; } public void setUnitPrice(float unitPrice) { this.unitPrice = unitPrice; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
===============Client.java===============
package com.spring.ch11.dao.hibernate.daosupport; import java.util.Random; import org.apache.commons.lang.builder.ToStringBuilder; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Client { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( "spring.xml", Client.class); ITestDao testBean = (ITestDao) context.getBean(TestDaoImpl.class .getName()); DomainObject domainObject = new DomainObject(); domainObject.setId(new Random().nextInt(10000)); domainObject.setName("name" + domainObject.getId()); domainObject.setUnitPrice(10.23f); System.out.println(ToStringBuilder.reflectionToString(domainObject)); testBean.save(domainObject); System.out.println(testBean.getAll().size()); } }
另外mysql的数据库一定要是Innodb方式的才能支持事务。