利用Spring整合Hibernate时的XML文件配置 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 容器注册
AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor、RequiredAnnotationBeanPostProcessor 这 4 个BeanPostProcessor
识别注解 -->
<context:annotation-config /> <!-- 自动扫描base-pack子包下面Java文件,如果扫描到有@Component @Controller @Service等这些注解的类,则把这些类注册为bean -->
<context:component-scan base-package="cqvie.yjq" /> <!-- ************************************************************************************************ -->
<!-- 定义.properties文件 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations" value="classpath:jdbc.properties"/> <!-- 如果有多个.properties文件,则可以透过 locations属性来设定:
<property name="locations">
<list>
<value>classpath:mailsender1.properties</value>
<value>classpath:mailsender2.properties</value>
</list>
</property>
-->
</bean> <!-- ************************************************************************************************ -->
<!-- 配置数据源 -->
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean> <!-- ************************************************************************************************ -->
<!-- 自动创建 SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 等价于下面的方法
<property name="annotatedClasses">
<list>
<value>cqvie.yjq.model.User</value>
<value>cqvie.yjq.model.Log</value>
</list>
</property>
-->
<!-- 扫描包下面的java文件 -->
<property name="packagesToScan">
<list>
<value>cqvie.yjq.model</value>
</list>
</property> <!-- 配置 hibernate.cfg.xml 中的信息 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean> <!-- ************************************************************************************************ -->
<!-- 事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean> <!-- 注解方式
<tx:annotation-driven transaction-manager="txManager"/>
-->
<!-- XML方式 -->
<aop:config>
<aop:pointcut expression="execution(public * cqvie.yjq.service..*.*(..))" id="bussinessService"/>
<aop:advisor pointcut-ref="bussinessService" advice-ref="txAdvice"/>
</aop:config> <tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="is*" read-only="true"/>
<tx:method name="add*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice> <!-- ************************************************************************************************ -->
<!-- Spring 调用 Hibernate 的持久化操作 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> </beans>

web.xml文件配置如下

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>SSH_170324</display-name> <welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list> <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
<!-- 默认路径:/WEB-INF/applicationContext.xml -->
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- <param-value>/WEB-INF/applicationContext*.xml</param-value> -->
<param-value>classpath:beans.xml</param-value>
</context-param> <!-- Spring 解决中文乱码问题 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!-- 增加session的使用范围 -->
<filter>
<filter-name>openSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
<!-- 默认寻找beans.xml中的sessionFactory -->
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>openSessionInView</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!-- 配置struts2 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> </web-app>

最新文章

  1. Azure上的那些IP
  2. Android 短信的备份
  3. DS实验题 order
  4. java反射快速入门(二)
  5. iOS UIKit:TableView之单元格配置(2)
  6. DB2单个DB重启
  7. srand((double)microtime()*1000000)
  8. Centos系统mysql 忘记root用户的密码:
  9. Android jni 编程4(对基本类型二维整型数组的操作)
  10. Mybatis 插入数据并返回刚刚插入的数据id
  11. javascript实时保存时出现改动多条记录的bug
  12. FusionCharts封装-Category
  13. python当中的 可迭代对象 迭代器
  14. iOS开发之zip文件解压
  15. MongoDB 在系统数据库local上无法创建用户的解决方法
  16. C#开源框架(转载)
  17. 怎么成为asp.net大神!!!!!!!!!!!!!!!!!!!怎么成为asp.net大神!!!!!!!!!!!!!!!!!!!
  18. 20145328 《Java程序设计》第5周学习总结
  19. 读取pandas修改单列数据类型
  20. Spring boot-(3) Spring Boot特性1

热门文章

  1. matlab toolboxes 大全
  2. [ucgui] 仪表盘函数
  3. C#开发微信小程序
  4. ASP.NET Core 2 学习笔记(八)URL重写
  5. 哎呀,我艹,使用tfs时候,离职人员锁定了代码.
  6. 创建自己的Code Snippet(代码模板)
  7. Linear and Quadratic Programming Solver ( Arithmetic and Algebra) CGAL 4.13 -User Manual
  8. log4net 未生成log 原因分析
  9. 873. Length of Longest Fibonacci Subsequence
  10. Bind读取配置到C#实例