applicationContext-mybatis.xml 配置文件

applicationContext-mybatis.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:aop="http://www.springframework.org/schema/aop"  
            xmlns:p="http://www.springframework.org/schema/p"  
            xmlns:tx="http://www.springframework.org/schema/tx"  
            xmlns:context="http://www.springframework.org/schema/context"  
            xsi:schemaLocation="   
                http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                http://www.springframework.org/schema/aop
                http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                http://www.springframework.org/schema/tx
                http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
                http://www.springframework.org/schema/context 
                http://www.springframework.org/schema/context/spring-context.xsd">  

            <!-- spring 管理bean  创建对象的 -->
        <!--3 读取外部配置文件 -->
      <!--  <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
                <property name="location" value="classpath:database.properties"/>
        </bean>-->
            <context:property-placeholder location="classpath:database.properties"></context:property-placeholder>
        <!--2 配置数据源
                dbcp - - >dbcp2
                c3p0
                druid 德鲁依  alibaba 最牛的数据源 ==dbcp
        -->
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
            <property name="username" value="${jdbc.user}"></property>
            <property name="url" value="${jdbc.url}"/>
            <property name="password" value="${jdbc.password}"/>
            <property name="driverClassName" value="${jdbc.driver}"/>
            <!-- 省略心跳包 -->
        </bean>
        <!-- 1 连接数据库,需要数据库连接对象 工厂-->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="configLocation" value="classpath:mybatis-config.xml"/>
<!--           <property name="mapperLocations" value="classpath:cn/kgc/mapper/*.xml"></property>-->
            <property name="dataSource" ref="dataSource"></property>
        </bean>

        <!-- 4 配置mybaits mapper  XxxxxTemplate  XxxxSupport
                开发方式1  传统开发 ibatis过来,自己写实现类
                        2  mapper代理开发 ,没有实现类
                                1 一个一个配置 MapperFactoryBean
                                        有id
                                2 一次性装配 MapperScannerConfigure
                                        没有显式ID,默认将首字母小写当ID
        -->
        <bean  class="org.mybatis.spring.mapper.MapperScannerConfigurer">
                <property name="basePackage" value="cn.kgc.dao"/>
        </bean>
        <!--<bean id="devUserMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">-->
                <!--<property name="mapperInterface" value="cn.kgc.mapper.DevUserMapper"/>-->
                <!--<property name="sqlSessionFactory" ref="sqlSessionFactory"> </property>-->
        <!--</bean>-->
        <!-- 5 告诉spring使用注解-->
        <context:component-scan base-package="cn.kgc.service"/>
        <!--<bean id="manServiceImpl" class="cn.kgc.service.impl.ManSerciveImpl"/>-->
        <!-- 6 声明式的事务处理 -->
    <!-- 6.3 配置事务管理类-->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"/>
        </bean>
    <!-- 6.2 配置通知-->
        <tx:advice id="tx" transaction-manager="transactionManager">
                <tx:attributes>
                    <tx:method name="get*"  propagation="NEVER"/>
                    <tx:method name="set*" propagation="NEVER"/>
                    <tx:method name="select*" read-only="true"/>
                    <tx:method name="find*" read-only="true"/>
                    <tx:method name="show*" read-only="true"/>
                    <tx:method name="*"/>
                </tx:attributes>
        </tx:advice>
    <!-- 6.1 aop配置事务-->
        <aop:config>
                <aop:pointcut id="pc" expression="execution(public * cn.kgc.service..*(..))"></aop:pointcut>
                <aop:advisor advice-ref="tx" pointcut-ref="pc"></aop:advisor>
        </aop:config>
</beans>

猜你喜欢

转载自blog.csdn.net/s20180412/article/details/82834391
今日推荐