spring、hibernate整合核心配置檔案applicationContext.xml
阿新 • • 發佈:2019-02-14
<?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">
<!-- 自動掃描註解的bean 多個包以 逗號分隔-->
<context:component-scan base-package="com.sun" />
<aop:aspectj-autoproxy proxy-target-class="true"/>
<!-- 引入jdbc配置檔案 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
<property name="locations">
<list>
<value>classpath:properties/*.properties</value>
<!--要是有多個配置檔案,只需在這裡繼續新增即可 -->
</list>
</property>
</bean>
<!-- 配置資料來源 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- 不使用properties來配置 -->
<!-- <property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/learning" />
<property name="username" value="root" />
<property name="password" value=" [email protected]" /> -->
<!-- 使用properties來配置 -->
<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>
<!-- mapper介面代理實現,自動掃描了所有的XxxxMapper.xml對應的mapper介面檔案,這樣就不用一個一個手動配置Mpper的映射了,只要Mapper介面類和Mapper對映檔案對應起來就可以了。 -->
<!--<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >-->
<!--<!–指定掃描整個包,多個包用英文","分隔符–>-->
<!--<property name="basePackage" value="com.sun.dao" />-->
<!--</bean>-->
<!-- 配置Mybatis的檔案 ,mapperLocations配置**Mapper.xml檔案位置,configLocation配置mybatis-config檔案位置-->
<!-- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:orm.mybatis/*.xml"/>
<!–<property name="configLocation" value="classpath:config/mybatis-config.xml" />–>
<!– <property name="typeAliasesPackage" value="com.tiantian.ckeditor.model"
/> –>
</bean>-->
<!--配置hibernate檔案,hibernateProperties是-->
<bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!--注入連線池-->
<property name="dataSource" ref="dataSource"/>
<!--hibernate常用配置-->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!--hibernate 對映檔案配置-->
<!--<property name="mappingLocations">
<list>
<value>classpath:orm/hibernate/hibernate.hbm.xml</value>
</list>
</property>-->
<!--hibernate 對映包配置-->
<!--<property name="mappingDirectoryLocations">
<list>
<value>classpath:orm/hibernate/</value>
</list> </property>-->
<!--hibernate 自動掃描註解包配置-->
<!--<property name="packagesToScan">
<list>
<value>classpath:com.sun.dto.po</value>
</list>
</property>-->
</bean>
<!-- (事務管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="txManage" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!--開啟事務註解掃描-->
<tx:annotation-driven transaction-manager="txManage"/>
</beans>
<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">
<!-- 自動掃描註解的bean 多個包以 逗號分隔-->
<context:component-scan base-package="com.sun" />
<aop:aspectj-autoproxy proxy-target-class="true"/>
<!-- 引入jdbc配置檔案 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
<property name="locations">
<list>
<value>classpath:properties/*.properties</value>
<!--要是有多個配置檔案,只需在這裡繼續新增即可 -->
</list>
</property>
</bean>
<!-- 配置資料來源 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- 不使用properties來配置 -->
<!-- <property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/learning" />
<property name="username" value="root" />
<property name="password" value="
<!-- 使用properties來配置 -->
<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>
<!-- mapper介面代理實現,自動掃描了所有的XxxxMapper.xml對應的mapper介面檔案,這樣就不用一個一個手動配置Mpper的映射了,只要Mapper介面類和Mapper對映檔案對應起來就可以了。 -->
<!--<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >-->
<!--<!–指定掃描整個包,多個包用英文","分隔符–>-->
<!--<property name="basePackage" value="com.sun.dao" />-->
<!--</bean>-->
<!-- 配置Mybatis的檔案 ,mapperLocations配置**Mapper.xml檔案位置,configLocation配置mybatis-config檔案位置-->
<!-- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:orm.mybatis/*.xml"/>
<!–<property name="configLocation" value="classpath:config/mybatis-config.xml" />–>
<!– <property name="typeAliasesPackage" value="com.tiantian.ckeditor.model"
/> –>
</bean>-->
<!--配置hibernate檔案,hibernateProperties是-->
<bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!--注入連線池-->
<property name="dataSource" ref="dataSource"/>
<!--hibernate常用配置-->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!--hibernate 對映檔案配置-->
<!--<property name="mappingLocations">
<list>
<value>classpath:orm/hibernate/hibernate.hbm.xml</value>
</list>
</property>-->
<!--hibernate 對映包配置-->
<!--<property name="mappingDirectoryLocations">
<list>
<value>classpath:orm/hibernate/</value>
</list> </property>-->
<!--hibernate 自動掃描註解包配置-->
<!--<property name="packagesToScan">
<list>
<value>classpath:com.sun.dto.po</value>
</list>
</property>-->
</bean>
<!-- (事務管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="txManage" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!--開啟事務註解掃描-->
<tx:annotation-driven transaction-manager="txManage"/>
</beans>