mybatis 整合spring之mapperLocations配置的問題

來源:互聯網
上載者:User

標籤:ace   org   ack   相同   mes   dao   div   ring   使用配置   

 今天嘗試spring整合mybatis時遇到這麼一個問題,就是在配置sqlSessionFactory時是否要配置mapperLocations的問題。

 

<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">    <property name="dataSource" ref="datasource"></property>    <property name="typeAliasesPackage" value="com.fan.entity"/>      <!-- 當mybatis的xml檔案和mapper介面不在相同包下時,需要用mapperLocations屬性指定xml檔案的路徑。           *是個萬用字元,代表所有的檔案,**代表所有目錄下 -->       <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>

 

結論是:如果Mapper.xml與Mapper.class在同一個包下且同名,spring掃描Mapper.class的同時會自動掃描同名的Mapper.xml並裝配到Mapper.class。

如果Mapper.xml與Mapper.class不在同一個包下或者不同名,就必須使用配置mapperLocations指定mapper.xml的位置。

此時spring是通過識別mapper.xml中的 <mapper namespace="com.fan.mapper.UserDao"> namespace的值來確定對應的Mapper.class的。

 

mybatis 整合spring之mapperLocations配置的問題

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.