Spring4+Hibernate4整合
来源:互联网 发布:网络知识大赛 编辑:程序博客网 时间:2024/06/10 00:01
Spring对Hibernate提供了非常好的支持,包括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
- Struts2+Hibernate4+Spring4整合
- spring4如何整合hibernate4
- Spring4+Hibernate4整合
- spring4整合hibernate4
- spring4 hibernate4(5) 整合
- spring4, hibernate4 整合问题
- Spring4 整合 Hibernate4
- strut2+hibernate4+spring4整合
- Spring4+Hibernate4整合问题解决
- spring4+hibernate4+struts2整合
- Spring4 整合Hibernate4,Struts2
- Spring4整合Hibernate4.3
- Spring4+Hibernate4整合配置
- springMVC+hibernate4.3+spring4整合
- Spring4.x+Hibernate4.x整合
- spring4.1.6整合hibernate4.0
- 关于Spring4与Hibernate4整合
- Spring4整合Hibernate4详细示例
- Android 编码规范
- iOS中的lauchOptions
- python数据类型转换 python字符串转浮点数 python字符串转float
- Android多线程机制详细解析
- Sping+ActiveMQ整合
- Spring4+Hibernate4整合
- 关于使用stm8单片机的“外部计数”TIMx_ETR测脉冲的软件配置问题!
- 官方实现下拉刷新
- 数据窗口对象里面列的格式复制
- lintcode: Ugly Number II
- android中Webview与javascript的交互(互相调用)
- ORACLE 存储过程动态游标例子
- python实现的html转xlsx,xlsx转html
- 摩客应邀到长虹集团做产品设计培训