日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

mybatis中<mappers> ,mapperLocations,和MapperScannerConfigurer 用法

發(fā)布時(shí)間:2025/3/12 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mybatis中<mappers> ,mapperLocations,和MapperScannerConfigurer 用法 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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ì)資源

<!-- Using classpath relative resources --> <mappers><mapper resource="org/mybatis/builder/AuthorMapper.xml"/><mapper resource="org/mybatis/builder/BlogMapper.xml"/><mapper resource="org/mybatis/builder/PostMapper.xml"/> </mappers>

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)證

<!-- Using mapper interface classes --> <mappers><mapper class="org.mybatis.builder.AuthorMapper"/><mapper class="org.mybatis.builder.BlogMapper"/><mapper class="org.mybatis.builder.PostMapper"/> </mappers>

1.4第四種方式:package標(biāo)簽,通過name屬性指定mapper接口所在的包名 ,此時(shí)對(duì)應(yīng)的映射文件必須與接口位于同一路徑下,并且名稱相同

如mapper接口采用注解的方式,則無需映射文件
windows系統(tǒng)下,映射文件不區(qū)分大小寫,linux系統(tǒng)沒有驗(yàn)證

<!-- Register all interfaces in a package as mappers --> <mappers><package name="org.mybatis.builder"/> </mappers>

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的。

<!--創(chuàng)建sqlSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!--關(guān)聯(lián)到連接池--><property name="dataSource" ref="dataSource"/><!--加載mybatis 全局配置文件--><property name="configLocation" value="classpath:mybatis-config.xml"/><!--加載mapper的xml映射文件 ,配置該屬性后全局配置文件可以不需要再配置<mappers>--><property name="mapperLocations" value="classpath:mapper/*.xml"/> </bean>

3. MapperScannerConfigurer:

自動(dòng)掃描 將Mapper接口生成代理注入到Spring
3.1要?jiǎng)?chuàng)建 MapperScannerConfigurer,可以在 Spring 的配置中添加如下代碼:

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="org.mybatis.spring.sample.mapper" /> </bean>

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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。