今日学习总计

建立spring-mybatis.xml配置文件

    这个文件就是用来完成spring和mybatis的整合的。这里面也没多少行配置,主要的就是自动扫描,自动注入,配置数据库。注释也很详细,大家看看就明白了。

 

spring-mybatis.xml

 

<?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:p="http://www.springframework.org/schema/p"  

    xmlns:context="http://www.springframework.org/schema/context"  

    xmlns:mvc="http://www.springframework.org/schema/mvc"  

    xsi:schemaLocation="http://www.springframework.org/schema/beans    

                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd    

                        http://www.springframework.org/schema/context    

                        http://www.springframework.org/schema/context/spring-context-3.1.xsd    

                        http://www.springframework.org/schema/mvc    

                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">  

    <!-- 自动扫描 -->  

    <context:component-scan base-package="com.javen" />  

    

    <!-- 引入配置文件 -->  

    <bean id="propertyConfigurer"  

        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  

        <property name="location" value="classpath:jdbc.properties" />  

    </bean>  

  

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  

        destroy-method="close">  

        <property name="driverClassName" value="${driver}" />  

        <property name="url" value="${url}" />  

        <property name="username" value="${username}" />  

        <property name="password" value="${password}" />  

        <!-- 初始化连接大小 -->  

        <property name="initialSize" value="${initialSize}"></property>  

        <!-- 连接池最大数量 -->  

        <property name="maxActive" value="${maxActive}"></property>  

        <!-- 连接池最大空闲 -->  

        <property name="maxIdle" value="${maxIdle}"></property>  

        <!-- 连接池最小空闲 -->  

        <property name="minIdle" value="${minIdle}"></property>  

        <!-- 获取连接最大等待时间 -->  

        <property name="maxWait" value="${maxWait}"></property>  

    </bean>  

  

    <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->  

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  

        <property name="dataSource" ref="dataSource" />  

        <!-- 自动扫描mapping.xml文件 -->  

        <property name="mapperLocations" value="classpath:com/javen/mapping/*.xml"></property>  

    </bean>  

  

    <!-- DAO接口所在包名,Spring会自动查找其下的类 -->  

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

        <property name="basePackage" value="com.javen.dao" />  

        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>  

    </bean>  

  

    <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->  

    <bean id="transactionManager"  

        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  

        <property name="dataSource" ref="dataSource" />  

    </bean>  

  

</beans>  

复制代码

3.4、JUnit测试

  经过以上步骤,我们已经完成了Spring和mybatis的整合,这样我们就可以编写一段测试代码来试试是否成功了。

 

posted @ 2021-11-14 16:19  禁小呆  阅读(30)  评论(0)    收藏  举报