Spring_Hibernate整合准备
1,Spring整合Hibernate 整合什么?
1)由IOC容器来生成Hibernate的SessionFactory
2)让Hibernate使用上Spring的声明式事务
2,整合步骤
1)先加入Hibernate
Jar包
/Spring_Hibernate/lib/antlr-2.7.7.jar /Spring_Hibernate/lib/c3p0-0.9.1.2.jar ---c3p0数据源 /Spring_Hibernate/lib/dom4j-1.6.1.jar /Spring_Hibernate/lib/geronimo-jta_1.1_spec-1.1.1.jar /Spring_Hibernate/lib/hibernate-commons-annotations-5.0.1.Final.jar /Spring_Hibernate/lib/hibernate-core-5.0.12.Final.jar /Spring_Hibernate/lib/hibernate-jpa-2.1-api-1.0.0.Final.jar /Spring_Hibernate/lib/jandex-2.0.0.Final.jar /Spring_Hibernate/lib/javassist-3.18.1-GA.jar /Spring_Hibernate/lib/jboss-logging-3.3.0.Final.jar /Spring_Hibernate/lib/mysql-connector-java-8.0.11.jar --mysql驱动
添加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> <!-- 配置Hibernate的基本属性 --> <!-- 1.数据源需配置到IOC容器中,所以在此处不再需要配置数据源 --> <!-- 2.导入关联的映射文件 ,也在IOC容器配置session-factory里配置--> <!-- 3.配置hibernate的基本属性,:方言,格式化sql,生成表的策略,以及二级缓存的 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> <property name="hibernate.hbm2ddl.auto">update</property><!--①--> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> </session-factory> </hibernate-configuration>
2)加入Spring
3)整合