天天看点

mybatis的xml配置

<?xml version="1.0" encoding="UTF-8"?>

xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
   xmlns="http://www.springframework.org/schema/beans"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd

       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!--要使用注解的话,必须打开该配置项。通过这个配置项,就可以打开针对注解的处理器把它注入到Spring容器中-->
<!-- 数据库连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <!-- 基本参数 -->
    <property name="driverClassName" value="${jdbc.mysql.driver}"/>
    <property name="url" value="${jdbc.mysql.url}"/>
    <property name="username" value="${jdbc.mysql.username}"/>
    <property name="password" value="${jdbc.mysql.password}"/>
    <property name="connectionProperties" value="${jdbc.mysql.connectionProperties}"/>
    <!-- 连接数据相关参数 -->
    <property name="initialSize" value="${dbcp.initialSize}"/>
    <property name="maxActive" value="${dbcp.maxActive}"/>
    <property name="maxIdle" value="${dbcp.maxIdle}"/>
    <property name="minIdle" value="${dbcp.minIdle}"/>
    <property name="maxWait" value="${dbcp.maxWait}"/>
    <!-- 事务相关的属性 -->
    <property name="defaultAutoCommit" value="${dbcp.defaultAutoCommit}"/>
    <!-- 连接健康情况 -->
    <property name="timeBetweenEvictionRunsMillis" value="${dbcp.timeBetweenEvictionRunsMillis}"/>
    <property name="numTestsPerEvictionRun" value="${dbcp.numTestsPerEvictionRun}"/>
    <property name="minEvictableIdleTimeMillis" value="${dbcp.minEvictableIdleTimeMillis}"/>
    <property name="testWhileIdle" value="${dbcp.testWhileIdle}"/>
    <property name="testOnBorrow" value="${dbcp.testOnBorrow}"/>
    <property name="testOnReturn" value="${dbcp.testOnReturn}"/>
    <property name="validationQuery" value="${dbcp.validationQuery}"/>
    <!-- 连接泄漏回收参数 -->
    <property name="removeAbandoned" value="${dbcp.removeAbandoned}"/>
    <property name="removeAbandonedTimeout" value="${dbcp.removeAbandonedTimeout}"/>
    <property name="logAbandoned" value="${dbcp.logAbandoned}"/>
</bean>

<!-- 使用JDBC事物 -->
<bean id="transactionManager"
      class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
</bean>
<!-- 使用annotation注解方式配置事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>

<!-- mybatis 配置 sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="configLocation" value="classpath:spring/mybatis-config.xml"></property>
    <property name="mapperLocations"
              value="classpath:mapper/*Mapper.xml">
    </property>
</bean>
<!-- mybatis 配置 SQLSession模板 -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"
      scope="prototype">
    <constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
<!-- mybatis 配置  自动生成Dao接口实现 -->
<bean id="mybatisMapper" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage"
              value="**.com.公司.系统.**.dao.**"/>
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<!--spring 的JDBC模板 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource">
    </property>
</bean>
<!-- 激活扫描 @Repository @Service标注-->
<context:component-scan base-package="**.com.公司.系统.**">
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>