spring-MVC__spring__mybatis整合值之spring的配置文件 (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-4.3.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-4.0.xsd">
        
       <!-- 配置数据源jdbc-dbcp -->
      <!--  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="url" value="jdbc:mysql://localhost:3306/hibernate"></property>
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="username" value="root"></property>
        <property name="password" value="rt"></property>
       </bean> -->
       
       <!-- 读取jdbc的配置文件 -->
       <context:property-placeholder location="classpath:jdbc.properties"/>
       <!-- 开启包扫描 -->
       <context:component-scan base-package="com.qf.*"></context:component-scan>
       <!-- 配置数据源jdbc-c3p0 -->
       <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
       </bean>
       
       
       <!-- spring整合mybatis -->
       <bean id="sqlSessionFactory"  class="org.mybatis.spring.SqlSessionFactoryBean">
    <!-- 注入数据源 -->
        <property name="dataSource" ref="dataSource"></property>
    <!-- mybatis配置文件 -->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
        <!-- 设置别名 -->
        <property name="typeAliasesPackage" value="com.qf.entity"></property>
        <!-- mapper文件 -->
        <!-- <property name="mapperLocations" value="com/qf/mapper/*.xml" />  -->
       </bean>
     
       
       
       <!--  spring事物全自动化的 -->
       <!-- 事务管理器class换成hibernate的 -->
       <bean id="traManager"  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
       </bean>
       <!-- 事物以注解的方式实现 @Transational
       <tx:annotation-driven transaction-manager="traManager"/> -->
       <!-- 配置事务策略 -->
       <tx:advice id="txAdvice" transaction-manager="traManager">
        <tx:attributes>
        <!-- name:方法名称,支持通配符的
        isolation:隔离级别
        propagation:事物的转播属性
        read-only:是否只读
        -->
        <tx:method name="tra*" isolation="DEFAULT" propagation="REQUIRED" read-only="false" />
        <!-- 查询方法 -->
        <tx:method name="get*" read-only="true" />
        <tx:method name="find*" read-only="true" />
        <tx:method name="query*" read-only="true" />
        <!-- 更新操作 -->
        <tx:method name="insert*" read-only="false" />
        <tx:method name="remo*" read-only="false" />
        <tx:method name="update*" read-only="false" />
        <tx:method name="del*" read-only="false" />
        <tx:method name="add*" read-only="false" />
        </tx:attributes>
       </tx:advice>
       <!-- AOP的配置 -->
       <aop:config>
        <!-- <aop:pointcut expression="execution(* com.qf.service.*.*(..))" id="p1"/>  pointcut-ref="p1" -->
        <!-- 事物一般加在service(业务层)层 -->
        <aop:advisor  advice-ref="txAdvice" pointcut="execution(* com.qf.service.*.*(..))" />
       </aop:config>
       
       <!-- 自动创建的dao层的实现类 -->
       <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 指定mapper接口文件所在的基包 -->
        <property name="basePackage" value="com.qf.dao"></property>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
       </bean>
       
       
     
       
       
</beans>

猜你喜欢

转载自blog.csdn.net/futao127/article/details/80382625
今日推荐