spring4 hibernate4(5) 整合
2016-04-25 16:38
639 查看
纸上得来终觉浅
1.前面学了Spring具有两个重要特性,IOC和AOP,利用这些特性可以优化代码框架; 既然如此,那么在使用Hibernate时,就可以利用Spring框架对hibernate的框架进行优化。这就是Spring hibernate整合。
2.Spring可以优化Hibernate的哪些部分?
1)在未使用hibernate之前,session的获取一般通过单例模式从sessionFactory中获取,利用Spring,可以直接将sessionFactory交给IOC容器来管理;
2)Spring AOP的一个重要应用是声明式事务,这也可以在hibernate上使用,方便了事务管理。
3.下面是配置步骤:
1)加入hibernate 4和spring 4的jar包,(需要使用的(required文件夹下)都加上,否则后面整合后运行会有错误)
2)src目录下添加hibernate配置文件hibernate.cfg.xml:
sessionFactory和事务都是都这个文件来配置的。这个是整合的两个核心; 另外针对每个类的hbm.xml也是由这个文件来管理的。
4)其中db.properties文件在src下建立:
1.前面学了Spring具有两个重要特性,IOC和AOP,利用这些特性可以优化代码框架; 既然如此,那么在使用Hibernate时,就可以利用Spring框架对hibernate的框架进行优化。这就是Spring hibernate整合。
2.Spring可以优化Hibernate的哪些部分?
1)在未使用hibernate之前,session的获取一般通过单例模式从sessionFactory中获取,利用Spring,可以直接将sessionFactory交给IOC容器来管理;
2)Spring AOP的一个重要应用是声明式事务,这也可以在hibernate上使用,方便了事务管理。
3.下面是配置步骤:
1)加入hibernate 4和spring 4的jar包,(需要使用的(required文件夹下)都加上,否则后面整合后运行会有错误)
2)src目录下添加hibernate配置文件hibernate.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?> <!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> <!-- /*1.数据源的配置放在Spring中*/ --> <!-- /*2.bibernate本身属性的配置留下*/ --> <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <property name="hbm2ddl.auto">update</property> <property name="hibernate.temp.use_jdbc_metadata_defaults">false</property> <!-- <property name="current_session_context_class">thread</property> --> <!-- /*3.关于hbm.xml(映射文件由Spring来管理)*/ --> </session-factory> </hibernate-configuration>3)src下添加Spring配置文件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:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"> <!-- 扫描的包 --> <context:component-scan base-package="roadArchitectWeb.Test"></context:component-scan> <!--导入数据源配置 --> <context:property-placeholder location="classpath:db.properties"/> <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.initPoolSize}"></property> <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property> </bean> <!-- 配置Hibernate的SessionFactory实例,通过Spring提供的LocalSessionFactoryBean来配置 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <!-- 配置数据源属性 --> <property name="dataSource" ref="dataSource"></property> <!-- 配置hibernate配置文件的位置以及名称 --> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> <property name="mappingLocations" value="classpath:roadArchitectWeb/Test/*.hbm.xml"></property> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 启用注解来管理事务 --> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
sessionFactory和事务都是都这个文件来配置的。这个是整合的两个核心; 另外针对每个类的hbm.xml也是由这个文件来管理的。
4)其中db.properties文件在src下建立:
jdbc.user=root jdbc.password=123456 jdbc.driverClass=com.mysql.jdbc.Driver jdbc.jdbcUrl=jdbc:mysql:///roadArchitectWeb jdbc.initPoolSize=5 jdbc.maxPoolSize=10
相关文章推荐
- Spring整合Hibernate(1)
- spring整合hibernate
- Spring整合hibernate(2)之基于HibernateTemplate的整合
- Spring整合hibernate(5)之Service层的分析
- 分析:no session found for current session原因
- Spring整合Hibernate
- SpringMVC+hibernate框架搭建
- spring整合hibernate
- spring+hibernate xml和注解方式配置事务
- SSH框架整合
- Spring整合Hibernate遇到的问题
- Spring4.3+Hibernate5.2.10整合
- Spring整合hibernate No Session found for current thread
- java三大框架之spring整合hibernate4
- No bean named 'MysessionFactory' is defined
- spring和hibernate整合时异常(详解): getFlushMode is not valid without active transaction
- SPring Hibernate 整合学习开发出现的错误
- Error creating bean with name 'sessionFactory' defined in class path resource
- 《JAVA与模式》之建造模式
- 通过反射将javabean封装为json对象