`

spring和mybatis整合,简单配置文件

 
阅读更多

Mybatis支持自动扫描、探寻与注入,不需要我们手动去操作,这在节省我们劳动力的同时,也可以大大的节省我们的配置文件。

下面列出比较省时省力简单的配置文件示例:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
	http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<!--spring扫描的包路径-->
    <context:component-scan base-package="com.tyyd.*"/>

    <!-- 启用@Aspect支持 -->
    <aop:aspectj-autoproxy/>

    <!-- 等同于下面注掉部分  多个配置文件可用,号分隔 -->
    <context:property-placeholder location="classpath:init.properties"/>

    <!--
    <bean id="placeholderConfig"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:init.properties</value>
            </list>
        </property>
    </bean>
     -->

    <bean name="rDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${readPlatformDriverClassName}"></property>
        <property name="url" value="${readPlatformUrl}"></property>
        <property name="username" value="${readPlatformUsername}"/>
        <property name="password" value="${readPlatformPassword}"></property>
        <property name="initialSize" value="${readPlatformInitialSize}"></property>
        <property name="maxActive" value="${readPlatformMaxActive}"></property>
        <property name="maxIdle" value="${readPlatformMaxIdle}"></property>
        <property name="maxWait" value="${readPlatformMaxWait}"></property>
    </bean>

    <bean id="readSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="rDataSource" />

        <!--mybatis的config文件,非mapper映射文件 没有可以不指定-->
        <!--<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property>-->

        <!--指定mapper映射文件 可以不指定,在下面的MapperScannerConfigurer配置扫描路径即可-->
        <!--<property name="mapperLocations" value="classpath*:/META-INF/mybatis/**/*.xml" />-->
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

        <!--扫描mybatis mapper映射文件的包路径-->
        <property name="basePackage" value="com.tyyd.dw.mapper" />

        <!--这里指定一个注解类,表示扫描下面配置的basePackage路径下注解了该注解类型的class-->
        <!--如果不指定,则表示扫描全部-->
        <!--<property name="annotationClass" value="org.springframework.stereotype.Repository" />-->

        <!--指定SqlSessionFactory,如果只有一个SqlSessionFactory可能不指定,mybatis会自动进行装配-->
        <!--<property name="sqlSessionFactoryBeanName" value="readSqlSessionFactory"/>-->
    </bean>

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

    <!-- 配置事务的传播特性 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="del*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="*" read-only="true" />
        </tx:attributes>
    </tx:advice>

    <!-- 配置哪些方法使用事务 -->
    <aop:config>
        <aop:pointcut id="allManagerMethod"
                      expression="execution(* com.tyyd.dw.manager.*.impl.*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod" />
    </aop:config>

</beans>

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics