利用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>
转自:https://www.cnblogs.com/yjq520/p/6701185.html