1.配置數據源
<!-- 配置數據源 使用C3p0數據源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///shop"></property>
<property name="user" value="root"></property>
<property name="password" value=""></property>
</bean>
2.讓Spring的IOC容器管理Hibernate的SessionFactory
<!-- 配置SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<property name="packagesToScan">
<list>
<value>com.entity</value>
</list>
</property>
</bean>
其中
<property name="packagesToScan">
<list>
<value>com.entity</value>
</list>
</property>
這裏是爲了能使用Hibernate對Bean進行註解開發
3.事務管理
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
4.聲明面向切面編程
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* com.service.impl.*.*(..))" id="txPointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
其中
<aop:pointcut expression="execution(* com.service.impl.*.*(..))" id="txPointcut"/>
這裏的第一個*號指任意返回值,第二個指impl包下的任意類,第三個指任意方法,最後是任意參數
最後是web.xml中Spring的DispatcherServlet的配置
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
以上是本人在學習過程中爲了避免忘記而記錄,若有錯,歡迎指正!!!