Spring与MyBatis整合的applicationCont

2017-11-03  本文已影响98人  itcode
<?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:context="http://www.springframework.org/schema/context" 
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!--开启自动扫描,将加注解的bean放入容器-->
    <context:component-scan base-package="com.kaishengit"/>
    <!--加载properties配置文件,选配-->
    <context:property-placeholder location="classpath:config.properties"/>
    <!--配置数据源-->
    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <!--事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--开启基于注解的事务管理-->
    <tx:annotation-driven transaction-manager="transactionManager"/>
    <!--mybatis SqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--数据源-->
        <property name="dataSource" ref="dataSource"/>
        <!--别名的包所在的位置-->
        <property name="typeAliasesPackage" value="com.kaishengit.entity"/>
        <!--mapper文件所在的位置-->
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
        <!--其他配置-->
        <property name="configuration">
            <bean class="org.apache.ibatis.session.Configuration">
                <property name="mapUnderscoreToCamelCase" value="true"/>
            </bean>
        </property>
    </bean>
    <!--扫描mybatis中的Mapper接口,并自动放入spring容器-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.kaishengit.mapper"/>
    </bean>

</beans>
上一篇下一篇

猜你喜欢

热点阅读