SSM框架 spring配置文件

2018-4-18 / 5 评论 / 15465 阅读
<?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:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<!--  spring的配置文件  一般写入数据源     事务控制    mybatis扫描 mapper 文件中  IOC控制器中  -->

 <!-- 扫描service 层下面所有的 @Server  加入ICO容器中   -->
    <context:annotation-config />
 <context:component-scan base-package="com.rain.service" />

  <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
         <!-- 基本属性 url、user、password -->
         <property name="url" value="jdbc:mysql://localhost:3306/power?characterEncoding=UTF-8" />
         <property name="username" value="root" />
         <property name="password" value="root" />
         <property name="driverClassName" value="com.mysql.jdbc.Driver" />

         <!-- 配置初始化大小、最小、最大 -->
         <property name="initialSize" value="3" />
         <property name="minIdle" value="3" />
         <property name="maxActive" value="20" />

         <!-- 配置获取连接等待超时的时间 -->
         <property name="maxWait" value="60000" />

         <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
         <property name="timeBetweenEvictionRunsMillis" value="60000" />

         <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
         <property name="minEvictableIdleTimeMillis" value="300000" />

         <property name="validationQuery" value="SELECT 1" />
         <property name="testWhileIdle" value="true" />
         <property name="testOnBorrow" value="false" />
         <property name="testOnReturn" value="false" />

         <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
         <property name="poolPreparedStatements" value="true" />
         <property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
  </bean>

<!--  mybatis 和spring 进行整合  -->
   <bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="typeAliasesPackage" value="com.rain.pojo" />
        <property name="dataSource" ref="dataSource"/>
        <!--    指定mybatis mapper文件的地址 -->
        <property name="mapperLocations" value="classpath:com/rain/mapper/*.xml"/>

        <property name="plugins">
            <array>
             <!-- pagehelper -->
              <bean class="com.github.pagehelper.PageInterceptor">
                <property name="properties">
                  <!--使用下面的pagehelper方式配置参数,一行配置一个 -->
                  <value>
                  </value>
                </property>
              </bean>
            </array>
          </property>   

    </bean>

 <!--  配置扫描器  将 mybatis 接口的实现加入 ioc 容器中  -->
 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
      <!-- 扫描 该包路径下 所有 DAO接口的实现 加入 到 IOC容器中  -->
 <property name="basePackage" value="com.rain.mapper"/>
 </bean>

    <!-- 配置spring的PlatformTransactionManager,名字为默认值 -->  
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
        <property name="dataSource" ref="dataSource" />  
    </bean>  

    <!-- 开启事务控制的注解支持   比较多的使用 xml配置形式的事务-->  
    <aop:config>
     <!-- 切入点表达式  -->
     <aop:pointcut expression="execution(* com.rain.service..*(..))" id="txPoint"/>
     <!-- 事务增强 -->
     <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
    </aop:config>

    <!-- 配置事务增强  如何切入  -->
    <tx:advice id="txAdvice">
     <tx:attributes>
     <!-- 所有方法都是事务方法 -->
     <tx:method name="*" />
     <!-- 以get开始的所有方法 -->
     <tx:method name="get*" read-only="true"/>
     </tx:attributes>

     <!--  spring配置文件核心点    数据源   与mybatis 整合     -->
    </tx:advice>

</beans>

    评论一下?

    OωO
    取消
      1. 头像
        长沙麻将单机
        第4楼
        文章写得很好。
        回复
      1. 头像
        RainFly
        地板
        ceshi1
        回复
      1. 头像
        武胜婚庆公司
        板凳
        这篇文章写得很好。
        回复
        1. 头像
          跨境电商教程
          @武胜婚庆公司:纯顶。不过内容很好
          回复
      1. 头像
        武胜
        沙发
        文章很好值得一看。
        回复