org.mybatis.spring.mapper.MapperScannerConfigurer 類作用

來源:互聯網
上載者:User

標籤:spring

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="annotationClass" value="org.springframework.stereotype.Repository"/>
        <property name="basePackage" value="cn.jesong.webcall.report.dao"/>
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>



MapperFactoryBean的出現為了代替手工使用SqlSessionDaoSupport或SqlSessionTemplate編寫Data Access Objects(DAO)的代碼,使用動態代理實現。

<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"><property name="mapperInterface" value="org.mybatis.spring.sample.mapper.UserMapper" /><property name="sqlSessionFactory" ref="sqlSessionFactory" /></bean>
org.mybatis.spring.sample.mapper.UserMapper是一個介面,我們建立一個MapperFactoryBean執行個體,然後注入這個介面和sqlSessionFactory(mybatis中提供的SqlSessionFactory介面,MapperFactoryBean會使用SqlSessionFactory建立SqlSession)這兩個屬性。
之後想使用這個UserMapper介面的話,直接通過spring注入這個bean,然後就可以直接使用了,spring內部會建立一個這個介面的動態代理。
當發現要使用多個MapperFactoryBean的時候,一個一個定義肯定非常麻煩,於是mybatis-spring提供了MapperScannerConfigurer這個類,它將會尋找類路徑下的映射器並自動將它們建立成MapperFactoryBean。
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="org.mybatis.spring.sample.mapper" /></bean>
這段配置會掃描org.mybatis.spring.sample.mapper下的所有介面,然後建立各自介面的動態代理類。

org.mybatis.spring.mapper.MapperScannerConfigurer 類作用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.