Spring与Mabatis整合 IOC成功的配置

context-common.spring.xml:

<?xml version="1.0" encoding="UTF-8"?>
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xmlns:util="http://www.springframework.org/schema/util"
       default-autowire="byName">

    <!-- 自动扫描 -->
    <context:component-scan base-package="testwebapp.com.wangzuojia.*"/>

    <!-- 数据源配置 -->
    <bean id="dataSource"
          class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/test" />
        <property name="username" value="root" />
        <property name="password" value="admin" />
        <!-- 初始化连接大小 -->  
        <property name="initialSize" value="0"></property>  
        <!-- 连接池最大数量 -->  
        <property name="maxActive" value="20"></property>  
        <!-- 连接池最大空闲 -->  
        <property name="maxIdle" value="20"></property>  
        <!-- 连接池最小空闲 -->  
        <property name="minIdle" value="1"></property>  
        <!-- 获取连接最大等待时间 -->  
        <property name="maxWait" value="60000"></property> 
        <property name="defaultAutoCommit" value="false" />
    </bean>

    <!-- 使用AspectJ方式配置AOP -->
    <aop:aspectj-autoproxy/>
    <!-- 配置事务传播特性(通知)-->
    <aop:config   proxy-target-class="true">
        <!-- 切面 -->
        <aop:pointcut id="servicePointcut"
                      expression="execution(* testwebapp.com.wangzuojia.service..*.*(..))" />
        <!-- 事务处理方法  order="1"-->
        <aop:advisor pointcut-ref="servicePointcut" advice-ref="txAdvice"/>
    </aop:config>
    <!--配置哪些类的哪些方法参与事务 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <!-- 定义事务管理的规则 -->
        <tx:attributes>
            <tx:method name="add*" />
            <tx:method name="insert*" />
            <tx:method name="remove*" />
            <tx:method name="save*" />
            <tx:method name="update*" />
            <tx:method name="delete*" />
        </tx:attributes>
    </tx:advice>

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

    <!-- 申明式事务 --><!-- 可通过注解控制事务 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- mybatsi相关 --><!-- 创建SqlSessionFactory,同时指定数据源 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
        <property name="mapperLocations">
            <array>
                <value>classpath*:testwebapp/com/wangzuojia/mapping/*.xml</value><!-- * 匹配0或者任意数量的字符;** 匹配0或者更多的目录 -->
            </array>
        </property>
        <property name="configLocation" value="classpath:mybatis-config.xml" />
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="testwebapp.com.wangzuojia.dao"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>

    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype">
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>

</beans>

mybatis-config.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
<configuration>
    <settings>
        <setting name="cacheEnabled" value="true" />
        <setting name="lazyLoadingEnabled" value="false" />
    </settings>

    <typeAliases>
        <package name="testwebapp.com.wangzuojia.entity" />
    </typeAliases>

</configuration>
Spring与Mabatis整合结束


applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:cache="http://www.springframework.org/schema/cache"

     <import resource="classpath*:spring/context-common.spring.xml" />
     <!-- <import resource="classpath*:spring-mybatis.xml" /> -->
    <!--<import resource="classpath*:spring/context-cache.spring.xml" />
    <import resource="classpath*:spring/context-dao.spring.xml" />
    <import resource="classpath*:spring/schedule-context.xml" />
    <import resource="classpath*:spring/context-datahub-common.spring.xml" />
    <import resource="classpath*:spring/webservice-client.xml" />
    <import resource="classpath*:spring/webservice-server.xml" />
    <import resource="classpath*:spring/context-camel.spring.xml" />
    <import resource="classpath*:spring/context-taskexecutor.spring.xml" /> -->

</beans>

web.xml:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

<web-app>
  <display-name>Archetype Created Web Application</display-name>

   <!-- 指定spring的配置文件,默认从web根目录寻找配置文件,我们可以通过spring提供的classpath:前缀指定从类路径下寻找 -->
    <context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>classpath*:applicationContext.xml</param-value>
    </context-param>

       <!-- 对Spring容器进行实例化 -->
    <listener>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

       <!-- Spring刷新Introspector防止内存泄露 -->
    <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>
</web-app>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值