Spring4+Hibernate4整合

    SpringHibernate提供了非常好的支持,包括Session管理、声明式事务管理、代码模板。用Spring包装Hibernate,使用起来非常方便,代码量大大减少,以往每个DAO方法中的重复代码不需要写了,而且因为使用了Spring,直接面向接口编程,整个架构有了层次上的提升。

 具体实现的步骤:

    1) add Spring Capabilities  

    2) add Hibernate Capabilities 将hibernate.cfg.xml文件不生成,而是配置指定在applicationContext.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	">
        <!--数据源 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver">
		</property>
		<property name="url" value="jdbc:oracle:thin:@localhost:1521:HLX">
		</property>
		<property name="username" value="scott"></property>
		<property name="password" value="hsx"></property>
	</bean>
<!-- 会话工厂-->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.Oracle9Dialect
				</prop>
				<prop key="hibernate.show_sql">
					true
				</prop>
				<prop key="hibernate.format_sql">
					true
				</prop>
				<prop key="hibernate.hbm2dll.auto">
					update
				</prop>
			</props>
		</property>
		<property name="mappingResources">
			<list>
				<value>com/hlx/entity/Person.hbm.xml</value>
			</list>
		</property>
	</bean>

    3)add Librarities (SH.jar)

   4)在Dao类继承HibernateDaoSupport 使用getHibernateTemplate()方法获取HibernateTemplate实例完成持久化操作

public class PersonDaoImpl extends HibernateDaoSupport implements PersonDao {

	@Override
	public void save(Person person) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().save(person);

	}

	/*
	 * finder methods for HQL strings
	 * (non-Javadoc)
	 * @see com.hlx.dao.PersonDao#list()
	 */
	@Override
	public List<Person> list() {
		// TODO Auto-generated method stub
		return (List<Person>) this.getHibernateTemplate().find("from Person");
	}

	@Override
	public void delete(Person person) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().delete(person);

	}

}

   5)配置文件applicationContext.xml

<!-- programming do thing -->
	<!-- dao -->
	<bean id="personDaoImpl" class="com.hlx.dao.impl.PersonDaoImpl">
	  <property name="sessionFactory" ref="sessionFactory"/>
	 </bean>
	 
	 <!-- service -->
	<bean id="personService" class="com.hlx.service.impl.PersonServiceImpl">
	  <property name="personDao" ref="personDaoImpl"/>
	 </bean>
	 
	 <!-- spring do thing -->
	  <!-- tx bean -->
	 <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
	  <property name="sessionFactory" ref="sessionFactory"/>
	 </bean>
	 
	 <!-- declare tx advice -->
	  <tx:advice id="txAdvice">
	    <tx:attributes>
	     <tx:method name="*save*" propagation="REQUIRED" read-only="false"/>
	     <tx:method name="*list*" propagation="SUPPORTS" read-only="true"/>
	    </tx:attributes>
	  </tx:advice>
	  
	  <!-- aop  --> 
	  <aop:config>
	    <aop:pointcut expression="execution(* com.hlx.service.*.*(..))" id="mycut"/>
	    <aop:advisor advice-ref="txAdvice" pointcut-ref="mycut"/>
	   </aop:config>
	 

   6)测试

	@Test
	public void test() {

		//HibernateTransactionManager
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

		PersonService manager = (PersonService) context
				.getBean("personService");

//		manager.save(new Person(new BigDecimal(30), "周芳"));
		manager.save_delete(new Person(new BigDecimal(30), ""));
		List<Person> list = manager.list();
		for (Person person : list) {
			System.out.println(person.getPid() + "\t" + person.getPname());
		}

	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值