applicationContext配置

本文档详细展示了如何使用Spring框架整合MyBatis,包括配置数据源、定义SqlSessionFactory、扫描Mapper接口以及配置事务管理等内容。通过具体的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:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.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
        ">

    <!-- jdbc.properties->dataSource->SqlSessionFactory->Mapper(Dao)->service->Controller -->

    <!-- 开启注解扫描配置,用于扫描service -->
    <context:component-scan base-package="cn.itsource.crm.service" />

    <!-- web项目必须添加classpath:前缀 -->
    <context:property-placeholder location="classpath:jdbc.properties" />
    <!-- 配置连接池&数据源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <!--maxActive: 最大连接数量 -->
        <property name="maxActive" value="150" />
        <!--minIdle: 最小空闲连接 -->
        <property name="minIdle" value="5" />
        <!--maxIdle: 最大空闲连接 -->
        <property name="maxIdle" value="20" />
        <!--initialSize: 初始化连接 -->
        <property name="initialSize" value="30" />
        <!-- 用来配置数据库断开后自动连接的连接被泄露时是否打印 -->
        <property name="logAbandoned" value="true" />
        <!--removeAbandoned: 是否自动回收超时连接 -->
        <property name="removeAbandoned" value="true" />
        <!--removeAbandonedTimeout: 超时时间(以秒数为单位) -->
        <property name="removeAbandonedTimeout" value="10" />
        <!--maxWait: 超时等待时间以毫秒为单位 1000等于60秒 -->
        <property name="maxWait" value="1000" />
        <!-- 在空闲连接回收器线程运行期间休眠的时间值,以毫秒为单位. -->
        <property name="timeBetweenEvictionRunsMillis" value="10000" />
        <!-- 在每次空闲连接回收器线程(如果有)运行时检查的连接数量 -->
        <property name="numTestsPerEvictionRun" value="10" />
        <!-- 1000 * 60 * 30 连接在池中保持空闲而不被空闲连接回收器线程 -->
        <property name="minEvictableIdleTimeMillis" value="10000" />
        <property name="validationQuery" value="SELECT NOW() FROM DUAL" />
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="mapperLocations"
            value="classpath*:cn/itsource/crm/mapper/*Mapper.xml" />
        <property name="typeAliasesPackage" value="cn.itsource.crm.domain,cn.itsource.crm.query"></property>
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 扫描mapper接口的包路径 -->
        <property name="basePackage" value="cn.itsource.crm.mapper" />
    </bean>

    <!-- 事务管理器 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- 开启注解事务,解析@Transactional事务注解 -->
    <tx:annotation-driven />

    <!-- 以为外部文件的方式导入配置 -->
    <import resource="classpath:applicationContext-shiro.xml" />

    <!-- 切面 :* cn.itsource.crm.service..*.*(..))" ..* :匹配在之前目录下的所有资源(包括子或子的子)
        最后一个.* :匹配类的方法,代表所有方法的意思 (..) :方法上的任意参数 -->
    <aop:config>
        <aop:pointcut id="crmPointcut"
            expression="execution(* cn.itsource.crm.service..*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="crmPointcut" />
    </aop:config>

    <!-- 通知 -->

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="query*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="list*" propagation="SUPPORTS" read-only="true" />
            <!-- 除了查询外,都要开启事务 -->
            <tx:method name="*" propagation="REQUIRED" />
        </tx:attributes>
    </tx:advice>

    <!-- 系统日志 -->
<!--     <bean id="systemLogUtils" class="cn.itsource.crm.util.SystemLogUtils"> -->
<!--     </bean> -->
<!--     <aop:config> -->
<!--         <aop:pointcut expression="execution(* cn.itsource.crm.service..*.*(..))" -->
<!--             id="logPointcut" /> -->
<!--         <aop:aspect    ref="systemLogUtils"> -->
<!--             <aop:after method="writeLog" pointcut-ref="logPointcut"/> -->
<!--         </aop:aspect> -->
<!--     </aop:config> -->
</beans>

转载于:https://www.cnblogs.com/nlfjzzh/p/7428639.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值