Spring4+Hibernate4整合

来源:互联网 发布:网络知识大赛 编辑:程序博客网 时间:2024/06/10 00:01

    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.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsdhttp://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 {@Overridepublic void save(Person person) {// TODO Auto-generated method stubthis.getHibernateTemplate().save(person);}/* * finder methods for HQL strings * (non-Javadoc) * @see com.hlx.dao.PersonDao#list() */@Overridepublic List<Person> list() {// TODO Auto-generated method stubreturn (List<Person>) this.getHibernateTemplate().find("from Person");}@Overridepublic void delete(Person person) {// TODO Auto-generated method stubthis.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)测试

@Testpublic void test() {//HibernateTransactionManagerApplicationContext 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());}}

 

0 0
原创粉丝点击