使用Spring掃描Mybatis的mapper接口的三種配置
Spring掃描Mybatis的mapper接口的配置
1.前言
mybatis支持與spring結(jié)合使用,使得mybatis中的mapper接口可以作為spring容器中的bean被應(yīng)用代碼中相關(guān)類,如Service類,通過@Autowired自動(dòng)注入進(jìn)來。
在使用方面需要在項(xiàng)目中引入以下包:
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
2. 在spring中可以通過三種方式
來自動(dòng)掃描獲取應(yīng)用代碼的mybatis相關(guān)的mapper接口定義:
2.1在applicationContext.xml中使用<mybatis:scan />
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
...
<!-- mybatis -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:mybatis/mapper/**/*.xml" />
<property name="configLocation" value="classpath:mybatis/mybitas-config.xml"
/>
</bean>
<mybatis:scan base-package="cn.cggeeker.dao"/>
...
</beans>
2.2在applicationContext.xml中
使用bean標(biāo)簽注冊(cè)MapperScannerConfigurer,即:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
...
<!-- mybatis -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:mybatis/mapper/**/*.xml" />
<property name="configLocation" value="classpath:mybatis/mybitas-config.xml" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.cggeeker.dao" />
</bean>
...
</beans>
2.3如果應(yīng)用是使用Java配置方式而不是XML
則在@Configuration配置類使用@MapperScan或者@MapperScans注解:
@Configuration
@MapperScan("cn.cggeeker.dao")
public class AppConfig {
@Bean
public DataSource dataSource() {
return new PooledDataSource("com.mysql.jdbc.Driver",
"jdbc:mysql://localhost:3306/test", "root", "root");
}
@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBeansessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
return sessionFactory.getObject();
}
}
Spring配置掃描mybatis的mapper文件注意
一般會(huì)將不業(yè)務(wù)的mapper文件放到不同的包中:

spring配置掃描就需要配置下面的方式(兩個(gè)*):
<!-- mybatis文件配置,掃描所有mapper文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
p:dataSource-ref="dataSource" p:configLocation="classpath:conf/mybatis-config.xml"
p:mapperLocations="classpath:mapper/**/*.xml" />
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
分享關(guān)于JAVA 中使用Preferences讀寫注冊(cè)表時(shí)要注意的地方
這篇文章介紹了關(guān)于JAVA 中使用Preferences讀寫注冊(cè)表時(shí)要注意的地方,有需要的朋友可以參考一下2013-08-08
java計(jì)算任意位水仙花數(shù)示例(回文數(shù))
這篇文章主要介紹了java計(jì)算任意位水仙花數(shù)示例(回文數(shù)),需要的朋友可以參考下2014-05-05
SpringBoot獲取application.properties文件中文亂碼問題及解決
這篇文章主要介紹了SpringBoot獲取application.properties文件中文亂碼問題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
圖解Java?ReentrantLock的條件變量Condition機(jī)制
想必大家都使用過wait()和notify()這兩個(gè)方法把,他們主要用于多線程間的協(xié)同處理。而RenentrantLock也支持這樣條件變量的能力,而且相對(duì)于synchronized?更加強(qiáng)大,能夠支持多個(gè)條件變量,本文就來詳細(xì)說說2022-10-10
Springboot使用Rabbitmq的延時(shí)隊(duì)列+死信隊(duì)列實(shí)現(xiàn)消息延期消費(fèi)
本文介紹了RabbitMQ的延時(shí)隊(duì)列和死信隊(duì)列,解釋了它們的工作原理及其應(yīng)用場(chǎng)景,延時(shí)隊(duì)列允許消息在設(shè)定的時(shí)間后被消費(fèi),結(jié)合實(shí)際案例,展示了如何實(shí)現(xiàn)和使用延時(shí)隊(duì)列和死信隊(duì)列,感興趣的朋友一起看看吧2025-01-01
Sharding-Proxy分庫分表和數(shù)據(jù)加密使用場(chǎng)景分析
這篇文章主要介紹了Sharding-Proxy分庫分表和數(shù)據(jù)加密使用經(jīng)驗(yàn)分享,通過場(chǎng)景模擬分析結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04

