mybatis中<mappers> ,mapperLocations,和MapperScannerConfigurer 用法
mybatis中<mappers> ,mapperLocations,和MapperScannerConfigurer 用法
1. mappers標(biāo)簽
在mybatis單獨(dú)使用時(shí),mybatis需要在mybatis-config.xml中配置mappers。mappers 標(biāo)簽下有許多 mapper 標(biāo)簽,每一個(gè) mapper 標(biāo)簽中配置的都是一個(gè)獨(dú)立的映射配置文件的路徑,配置方式有以下幾種:
1.1 第一種方式:mapper標(biāo)簽,通過resource屬性引入classpath路徑的相對(duì)資源
1.2mapper標(biāo)簽,通過url引入網(wǎng)絡(luò)資源或者本地磁盤資源
<!-- Using url fully qualified paths --> <mappers><mapper url="file:///var/mappers/AuthorMapper.xml"/><mapper url="file:///var/mappers/BlogMapper.xml"/><mapper url="file:///var/mappers/PostMapper.xml"/> </mappers>1.3 第三種方式:mapper標(biāo)簽,通過class屬性指定mapper接口名稱,此時(shí)對(duì)應(yīng)的映射文件必須與接口位于同一路徑下,并且名稱相同
如mapper接口采用注解的方式,則無需映射文件
windows系統(tǒng)下,映射文件不區(qū)分大小寫,linux系統(tǒng)沒有驗(yàn)證
1.4第四種方式:package標(biāo)簽,通過name屬性指定mapper接口所在的包名 ,此時(shí)對(duì)應(yīng)的映射文件必須與接口位于同一路徑下,并且名稱相同
如mapper接口采用注解的方式,則無需映射文件
windows系統(tǒng)下,映射文件不區(qū)分大小寫,linux系統(tǒng)沒有驗(yàn)證
2. mapperLocation屬性(位于SqlSessionFactoryBean中):mapperLocation屬性,主要用于指定mapper.xml文件所處的位置。
2.1如果Mapper.xml與Mapper.class在同一個(gè)包下且同名,spring 中MapperScannerConfigurer 掃描Mapper.class的同時(shí)會(huì)自動(dòng)掃描同名的Mapper.xml并裝配到Mapper.class。
2.2如果Mapper.xml與Mapper.class不在同一個(gè)包下或者不同名,就必須使用配置mapperLocations指定mapper.xml的位置。(如idea中 maven 默認(rèn)不打包java文件夾下的xml文件,未在pom.xml中配置resource的情況下)
此時(shí)spring是通過識(shí)別mapper.xml中的
namespace的值來確定對(duì)應(yīng)的Mapper.class的。
3. MapperScannerConfigurer:
自動(dòng)掃描 將Mapper接口生成代理注入到Spring
3.1要?jiǎng)?chuàng)建 MapperScannerConfigurer,可以在 Spring 的配置中添加如下代碼:
basePackage 屬性是讓你為映射器接口文件設(shè)置基本的包路徑。 你可以使用分號(hào)或逗號(hào) 作為分隔符設(shè)置多于一個(gè)的包路徑。每個(gè)映射器將會(huì)在指定的包路徑中遞歸地被搜索到。
mybatis 整合spring之mapperLocations配置的問題
今天嘗試spring整合mybatis時(shí)遇到這么一個(gè)問題,就是在配置sqlSessionFactory時(shí)是否要配置mapperLocations的問題。
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="datasource"></property><property name="typeAliasesPackage" value="com.fan.entity"/><!-- 當(dāng)mybatis的xml文件和mapper接口不在相同包下時(shí),需要用mapperLocations屬性指定xml文件的路徑。 *是個(gè)通配符,代表所有的文件,**代表所有目錄下 --> <property name="mapperLocations" value="classpath:com/fan/mapper/*.xml" /> <!--也可以引入mybatis配置文件 <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"></property> --> </bean> <!-- 通過掃描的模式,掃描目錄在com.lanyuan.mapper目錄下的mapper--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.fan.mapper"></property> </bean>結(jié)論是:如果Mapper.xml與Mapper.class在同一個(gè)包下且同名,spring掃描Mapper.class的同時(shí)會(huì)自動(dòng)掃描同名的Mapper.xml并裝配到Mapper.class。
如果Mapper.xml與Mapper.class不在同一個(gè)包下或者不同名,就必須使用配置mapperLocations指定mapper.xml的位置。
此時(shí)spring是通過識(shí)別mapper.xml中的 namespace的值來確定對(duì)應(yīng)的Mapper.class的。
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的mybatis中<mappers> ,mapperLocations,和MapperScannerConfigurer 用法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 机器学习实践:onnx模型转为Tenso
- 下一篇: 机器学习实践:本地远程查看服务器训练Te