Spring 对 Hibernate提供声明式的事务管理。
1.User.java
package cn.com.boyang;
public class User {
private Integer id;
private String name;
private Integer age;
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 Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
2.IUserDAO.java
package cn.com.boyang;
public interface IUserDAO {
public void insert(User user);
public User find(Integer id);
}
3.UserDAO.java
package cn.com.boyang;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;
public class UserDAO implements IUserDAO {
private HibernateTemplate hibernateTemplate;
public void setSessionFactory(
SessionFactory sessionFactory) {
hibernateTemplate = new HibernateTemplate(sessionFactory);
}
public void insert(User user) {
hibernateTemplate.save(user);
}
public User find(Integer id) {
User user =
(User) hibernateTemplate.get(User.class, id);
return user;
}
}
4.SpringHibernateDemo.java
package cn.com.boyang;
import org.springframework.context.ApplicationContext;
import org.springframework.context. support.FileSystemXmlApplicationContext;
public class SpringHibernateDemo {
public static void main(String[] args) {
ApplicationContext context =
new FileSystemXmlApplicationContext(
"src/beans-config.xml");
// 建立DAO物件
IUserDAO userDAO =
(IUserDAO) context.getBean("userDAOProxy");
User user = new User();
user.setName("cater");
user.setAge(new Integer(30));
userDAO.insert(user);
user = userDAO.find(new Integer(1));
System.out.println("name: " + user.getName());
}
}
5.User.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="cn.com.boyang.User"
table="user">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name" column="name"/>
<property name="age" column="age"/>
</class>
</hibernate-mapping>
6.beans-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/dbsourcedemo</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>12345678</value>
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
destroy-method="close">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="mappingResources">
<list>
<value>cn/com/boyang/User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
</bean>
<bean id="userDAO" class="cn.com.boyang.UserDAO">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="userDAOProxy"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="proxyInterfaces">
<list>
<value>cn.com.boyang.IUserDAO</value>
</list>
</property>
<property name="target">
<ref bean="userDAO"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
</beans>
7.加载包文件。
spring-beans.jar spring-context.jar spring-core.jar spring-dao.jar spring-jdbc.jar spring-hibernate3.jar Hibernate 3.1 Core Libraries aopalliance-1.0.jar mysql-connector-java-5.0.8-bin.jar spring-aop.jar
8.数据库表结构定义。
create database dbsourcedemo;
use dbsourcedemo;
create table user(
id int(11) not null auto_increment primary key,
name varchar(100) not null default '',
age int
);