Spring4+Hibernate4整合
2016-04-18 16:34
225 查看
Spring对Hibernate提供了非常好的支持,包括Session管理、声明式事务管理、代码模板。用Spring包装Hibernate,使用起来非常方便,代码量大大减少,以往每个DAO方法中的重复代码不需要写了,而且因为使用了Spring,直接面向接口编程,整个架构有了层次上的提升。
具体实现的步骤:
1) add Spring Capabilities
2) add Hibernate Capabilities 将hibernate.cfg.xml文件不生成,而是配置指定在applicationContext.xml文件中。
3)add Librarities (SH.jar)
4)在Dao类继承HibernateDaoSupport 使用getHibernateTemplate()方法获取HibernateTemplate实例完成持久化操作
5)配置文件applicationContext.xml
6)测试
具体实现的步骤:
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()); } }
相关文章推荐
- Java中类的排序
- [转]Java中导入、导出Excel
- 堆栈静态理解
- Exception in thread "main" java.lang.RuntimeException: java.lang.NoSuchFieldException: versionID
- Spring Task的坑
- JAVA 笔记 (总)
- java版精简搜狗皮肤
- J2SE(七)Java之类的实例化
- javascrip基本知识总结
- Java设计模式系列之状态模式
- Java—JDK的下载与安装
- 三种Java创建线程的方式(Callable,FutureTask)
- java 根据二叉树前序 ,中序求后续
- java 里面的string 和byte[] 怎么互转?
- JDK1.7新特性(二)Fork/Join框架demo
- Spring MVC And Java Web Learning Diary
- Java进阶学习第六天——DOM4J入门
- 【转】Eclipse 中设置JVM 内存 -- 不错
- (14)Spring Boot定时任务的使用【从零开始学Spring Boot】
- Java获取目录下的所有文件