spring hibernate mysql整合 配置事务(HibernateDaoSupport)

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方式的才能支持事务。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值