使用的jar包信息:
1、Spring4.3.8
2、Hibernate5.2.10
这是基于xml的整合,下一篇我们来进行注解式整合。
整合步骤:
1、引用jar包
2、编写配置文件:
数据库配置:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring_jdbc?characterEncoding=utf-8
jdbc.username=lx
jdbc.password=lx
jdbc.maxsize=100
jdbc.minsize=5
jdbc.idletime=60
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:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--Spring +Hibernate整合 -->
<!--加载数据库的连接配置文件 -->
<context:property-placeholder location="classpath:dbconfig.properties" />
<!--配置数据库来连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<!--驱动类全称 -->
<property name="driverClass" value="${jdbc.driverClassName}" />
<!--数据库的url地址 -->
<property name="jdbcUrl" value="${jdbc.url}" />
<!--用户名 -->
<property name="user" value="${jdbc.username}" />
<!--密码 -->
<property name="password" value="${jdbc.password}" />
<!--配置最大的连接数 -->
<property name="maxPoolSize" value="${jdbc.maxsize}"></property>
<!--配置最小的连接数 -->
<property name="minPoolSize" value="${jdbc.minsize}"></property>
<!--配置连接最大空闲时间 -->
<property name="maxIdleTime" value="${jdbc.idletime}"></property>
</bean>
<!--配置Hibernate的SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!--1、配置数据库连接池 -->
<property name="dataSource" ref="dataSource"></property>
<!--2、Hibernate的配置信息 -->
<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>
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop>
</props>
</property>
<property name="mappingLocations" value="classpath:cn/code404/domain/*.hbm.xml"></property>
</bean>
<!--事物管理对象 -->
<bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!--配置事物的通知 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<!-- 事物的属性,其实就是为切面中的哪些方法进行事物的配置 -->
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!--aop配置信息 -->
<aop:config>
<aop:pointcut id="pcut" expression="execution(* cn.code404.servie.*Service.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pcut"/>
</aop:config>
<!--配置dao对象 -->
<!--使用SessionFactory进行数据的操作 -->
<bean id="sdao" class="cn.code404.dao.StudentDao" scope="prototype">
<property name="factory" ref="sessionFactory"></property>
</bean>
<!--使用Spring的Hibernate模板进行数据的操作 -->
<bean id="sdao1" class="cn.code404.dao.StudentDao2" scope="prototype">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!--配置service对象 -->
<bean id="sservice" class="cn.code404.service.StudentService" scope="prototype">
<property name="dao" ref="sdao1"></property>
</bean>
</beans>
3、web.xml的配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>SH_Integrate</display-name>
<!--配置Spring框架的信息 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--设置Spring加载Session时的刷新模式 -->
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>cn.code404.web.filter.OpenSessionInViewFilterOver</filter-class>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
这里有几个注意的地方,参考Spring整合Hibernate遇到的问题
4、创建ORM映射类
5、创建dao和service
6、测试即可