Spring结合Hibernate实现数据库事务性操作范例

一、Vo(User.java)
package cn.edu.shnu.Vo;

public class User {

	private Integer id;
	private String name;
	private Integer age;	

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = 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;
	}
}

二、Vo与数据库表的映射文件(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.edu.shnu.Vo.User" table="user" catalog="test">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="native"></generator>
        </id>
        <property name="name" type="java.lang.String">
            <column name="name" />
        </property>
        <propery name="age" column="age" />          
    </class>
</hibernate-mapping>

三、IUserDAO接口(IUserDAO.java)
package cn.edu.shnu.impl;

public class IUserDAO {
    public void insert(User user);
    public User find(Integer id);
}

四、(UserDAO.java) 实现上面的接口
package cn.edu.shnu.Dao

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 = ()hibernateTemplate.get(User.class, id);

        return user;
    }
}

五、(beans-config.xml)配置文件
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsl="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

    <bean id="dataSource" class="org.springframework.jdbc.dataSource.DriverManagerDataSource">
       <property name="driverClassName" value="com.mysql.jdbc.Driver" />
       <property name="url" value="jdbc:mysql://localhost:3306/yourDBName" />
       <property name="username" value="root" />
       <property name="password" value="123456" />
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" destroy-method="close">
       <property name="dataSource" ref="dataSource" />
       <property name="mappingResources">
           <list>
               <value>cn/edu/shnu/Vo/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.edu.shnu.Dao.UserDAO">
	<property name="sessionFactory" ref="sessionFactory"/>
   </bean>
	
   <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
	<property name="sessionFactory" ref="sessionFactory" />
   </bean>
	
   <bean id="userDAOProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
	<property name="transactionManager" ref="transactionManager"/>
	<property name="proxyInterfaces">
		<list><value>cn.edu.shnu.impl.IUserDAO</value></list>
	</property>
	<property name="target" ref="userDAO"/>
	<property name="transactionAttributes">
		<props>
			<prop key="insert">PROPAGATION_REQUIRED</prop>		
		</props>
	</property>
  </bean>
</beans>

六、测试(SpringHibernateTest.java)

package cn.edu.shnu.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringHibernateTest {
	public static void main(String[] args){
			ApplicationContext context = new ClassPathXmlApplicationContext("beans-config.xml");
			
		//建立DAO对象
		IUserDAO userDAO = (IUserDAO)context.getBean("userDAOProxy");
		
		User user = new User();
		user.setName("Nemo");
		user.setAge(new Integer(26));
			
		userDAO.insert(user);
			
		user = userDAO.find(new Integer(1));
			
		System.out.println("name: " + user.getName());
       }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值