spring整合hibernate的详细步骤
2016-03-17 15:31
627 查看
Spring整合hibernate需要整合些什么?
由IOC容器来生成hibernate的sessionFactory.
让hibernate使用spring的声明式事务
整合步骤:
加入hibernate:
1.导jar包
2.配置hibernate的配置文件,hibernate.cfg.xml.(其实可以省略这个配置文件,但还是加上比较舒服):
3.编写持久化类以及对应的hbm.xml.
加入Spring
1.导入jar包.
2.配置spring配置文件(重点,详见注释):
beans.xml:
到此,配置工作就完成了,接下来可以测试一下代码,看看自动生成的数据库表.
如果你选择不使用hibernate.cfg.xml,那么可以在上面的beans.xml中,删掉<property name="configLocation" value="hibernate.cfg.xml"></property>,同时添加以下代码:
由IOC容器来生成hibernate的sessionFactory.
让hibernate使用spring的声明式事务
整合步骤:
加入hibernate:
1.导jar包
2.配置hibernate的配置文件,hibernate.cfg.xml.(其实可以省略这个配置文件,但还是加上比较舒服):
<?xml version="1.0"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 配置hibernate的基本属性 --> <!-- 配置数据源 ,因为需要把数据源配置到IOC容器中,所以这里不需要再配置--> <!-- 配置数据库方言 --> <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- 显示并格式化sql语句 --> <property name="show_sql">true</property> <property name="format_sql">true</property> <!-- 自动生成数据库表结构 --> <property name="hbm2ddl.auto">update</property> <!-- 使用getCurrentSession方式打开会话 --> <property name="hibernate.current_session_context_class">thread</property> <!-- 关联的*.hbm.xml也在容器配置sessionFactory实例时在进行配置,这里也不再配置 --> </session-factory> </hibernate-configuration>
3.编写持久化类以及对应的hbm.xml.
加入Spring
1.导入jar包.
2.配置spring配置文件(重点,详见注释):
beans.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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <!-- 导入资源文件 --> <context:property-placeholder location="db.properties" ignore-resource-not-found="false" /> <!-- 配置c3p0连接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <property name="initialPoolSize" value="${jdbc.initialPoolSize}"></property> <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property> </bean> <!--配置hibernate的SessionFactory实例,通过Spring提供的LocalSessionFactorybean进行配置 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <!-- 配置数据源属性 --> <property name="dataSource" ref="dataSource"></property> <!-- 配置hibernate.cfg.xml的位置及名称--> <property name="configLocation" value="hibernate.cfg.xml"></property> <!-- 配置hibernate映射文件的位置及名称,可以使用通配符--> <property name="mappingLocations" value="com/wang/entity/*.hbm.xml"></property> </bean> <!-- 配置Spring的声明式事务 --> <!-- 1.配置事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 2.配置事务属性,需要事务管理器 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*"/> <tx:method name="get*" read-only="true"/> </tx:attributes> </tx:advice> <!-- 3.配置事务切点,并把切点和事务属性关联起来 --> <aop:config> <aop:pointcut expression="execution(* com.wang.service.*.*(..))" id="pointcut"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/> </aop:config> </beans>
到此,配置工作就完成了,接下来可以测试一下代码,看看自动生成的数据库表.
如果你选择不使用hibernate.cfg.xml,那么可以在上面的beans.xml中,删掉<property name="configLocation" value="hibernate.cfg.xml"></property>,同时添加以下代码:
<property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property>
相关文章推荐
- Eclipse/MyEclipse中使用复制粘贴功能卡的解决办法
- JAVA_SE基础——58.如何用jar命令对java工程进行打包
- java中的内部类总结
- 如何在Eclipse下查看JDK源代码 (转)
- 回溯算法解数独问题(java版)
- 图解JDK7的Comparison method violates its general contract异常
- java中的socket编程有关printStream的println方法和write方法
- java并发:获取线程执行结果(Callable、Future、FutureTask)
- java 线程如何被终止
- 关于Spring,default-autowire-candidates属性的作用验证
- java代码走查审查规范
- jetbrick,新一代 Java 模板引擎,具有高性能和高扩展性
- 成为Java GC专家(3)
- 当spring 容器初始化完成后执行某个方法
- Java中的集合
- Spring 源码分析(三) —— AOP(六)源码分析与总结
- 成为JavaGC专家(2)
- Java:单例模式的7种写法
- log4j简单使用
- eclipse中Debug简单记忆