mybatisd的mapper.xml文件自动扫描注入的方法

1、配置好项目后在applicationContext-dao.xml中的 session 工厂配置里加入 mapperLocations 项 即可自动扫描目录下所有xml映射文件

<!-- spring和MyBatis完美整合 -->  
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
        <!-- 指定数据源 -->  
        <property name="dataSource" ref="dataSource"/>  
        <!-- 具体指定xml文件,可不配 -->  
        <property name="configLocation" value="classpath:mybatis-config.xml"/>  
        <!-- 自动扫描mapping.xml文件,**表示迭代查找 ,,也可在mybatis-config.xml中单独指定xml文件 -->
        <!-- 配置单个xml文件 -->
        <!--<property name="mapperLocations" value="classpath:com/yaosiyuan/dao/xml/MsMerchantMapper.xml"/> -->
        <!-- 配置多个xml文件 -->  
        <property name="mapperLocations">
        <list>
				<value>classpath:com/yaosiyuan/dao/xml/*.xml</value>
		</list>
          <!-- <array>
              <value>classpath:com/yaosiyuan/dao/xml/MsMerchantMapper.xml</value>
              <value>classpath:com/yaosiyuan/dao/xml/MsMerchantMapper.xml</value>
          </array>
           -->
			
          
      </property>
        
    </bean>  

2、mybatis-config.xml可以直接这样写个空白的

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>  
    <!-- 在这个文件放置一些全局性的配置
    <typeAliases>
        <typeAlias type="实体类" alias="别名"/>
    </typeAliases> -->
 
   <settings>   
        <setting name="cacheEnabled" value="false" />   
    </settings>  
    <!-- 以下内容不再需要手动配置  已经自动映射 -->
    <mappers>   
        <!-- <mapper resource="com/gfan/api/framework/model/sqlMapper/TestuserMapper.xml" /> -->
    </mappers>  
    
</configuration>

推荐阅读
●网站推荐
●软件推荐
●点击加入QQ群874514813交流学习,或获取更多资料

猜你喜欢

转载自blog.csdn.net/qq_33840251/article/details/89814071