spring-mybatis獲取mapper的四種方式匯總
spring-mybatis獲取mapper方式匯總
項(xiàng)目背景:
pojo下面有一個(gè)user實(shí)體類
Dao包下面寫了usermapper.xml 和usermapper.interface,其中只有一個(gè)方法查詢數(shù)據(jù)庫(kù)中所有的 用戶。
1.用實(shí)現(xiàn)類獲取這個(gè)用戶
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=utf8"/> <property name="username" value="root"/> <property name="password" value="root"/> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory"/> </bean> <!--注冊(cè)實(shí)現(xiàn)類usermapperimpl--> <bean id="userMapper" class="com.kuang.mapper.UserMapperImpl"> <property name="sqlSession" ref="sqlSession"/> </bean>
實(shí)現(xiàn)類usermapperImpl:
public class UserMapperImpl implements UserMapper { private SqlSessionTemplate sqlSession; public List<User> selectAllUser() { return sqlSession.getMapper(UserMapper.class).selectAllUser(); } public void setSqlSession(SqlSessionTemplate sqlSession) { this.sqlSession = sqlSession; } }
test測(cè)試:
@Test public void test2(){ ApplicationContext app = new ClassPathXmlApplicationContext("spring-dao.xml"); UserMapperImpl userMapper = app.getBean(UserMapperImpl.class); List<User> users = userMapper.selectAllUser(); for (User user : users) { System.out.println(user); } }
2.SqlSessionDaoSupport獲取
public class UserMapperImpl1 extends SqlSessionDaoSupport implements UserMapper { ? ? public List<User> selectAllUser() { ? ? ? ? return getSqlSession().getMapper(UserMapper.class).selectAllUser(); ? ? } }
bean的注冊(cè):
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> ? ? ? ? <property name="dataSource" ref="dataSource"/> ? ? ? ? <property name="configLocation" value="classpath:mybatis-config.xml"/> ? ? </bean> <bean id="userimpl1" class="com.kuang.mapper.UserMapperImpl1"> ? ? ? ? <property name="sqlSessionFactory" ref="sqlSessionFactory"/> ? ? </bean>
3.MapperFactoryBean
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> ? ? ? ? <property name="driverClassName" value="com.mysql.jdbc.Driver"/> ? ? ? ? <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=utf8"/> ? ? ? ? <property name="username" value="root"/> ? ? ? ? <property name="password" value="root"/> ? ? </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> ? ? ? ? <property name="dataSource" ref="dataSource"/> ? ? ? ? <property name="configLocation" value="classpath:mybatis-config.xml"/> ? ? </bean> ? ?? <bean id="userimpl" class="org.mybatis.spring.mapper.MapperFactoryBean"> ? ? ? ? <property name="mapperInterface" value="com.kuang.mapper.UserMapper"/> ? ? ? ? <property name="sqlSessionFactory" ref="sqlSessionFactory"/> ? ? </bean>
測(cè)試:
?@Test ? ? public void test3(){ ? ? ? ? ApplicationContext app = new ClassPathXmlApplicationContext("spring-dao.xml"); ? ? ? ? UserMapper userMapper = app.getBean(UserMapper.class); ??? ??? ?// UserMapper userMapper = app.getBean("userimpl"); ? ? ? ? List<User> users = userMapper.selectAllUser(); ? ? ? ? for (User user : users) { ? ? ? ? ? ? System.out.println(user); ? ? ? ? } ? ? }
在使用這個(gè)MapperFactoryBean方式的時(shí)候,通過(guò)app有兩種方式獲取bean,一種是id然后強(qiáng)轉(zhuǎn),另一種是接口的類型class。
4.MapperScannerConfigurer
xml配置
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> ? ? ? ? <property name="driverClassName" value="com.mysql.jdbc.Driver"/> ? ? ? ? <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=utf8"/> ? ? ? ? <property name="username" value="root"/> ? ? ? ? <property name="password" value="root"/> ? ? </bean> ? ? <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> ? ? ? ? <property name="dataSource" ref="dataSource"/> ? ? ? ? <property name="configLocation" value="classpath:mybatis-config.xml"/> ? ? </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> ? ? ? ? <property name="basePackage" value="com.kuang.mapper"/> ? ? ? ? <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> ? ? </bean>
test:
@Test ? ? public void test4(){ ? ? ? ? ApplicationContext app = new ClassPathXmlApplicationContext("spring-dao.xml"); ? ? ? ? UserMapper bean = app.getBean(UserMapper.class); ? ? ? ? for (User user : bean.selectAllUser()) { ? ? ? ? ? ? System.out.println(user); ? ? ? ? } ? ? }
mybatis的mapper注解
從mybatis3.4.0開始加入了@Mapper注解,目的就是為了不再寫mapper映射文件(那個(gè)xml寫的是真的無(wú)語(yǔ)。。。)。很惡心的一個(gè)事實(shí)是源碼中并沒有對(duì)于這個(gè)注解的詳細(xì)解釋
在 Spring 程序中,Mybatis 需要找到對(duì)應(yīng)的 mapper,在編譯的時(shí)候動(dòng)態(tài)生成代理類,實(shí)現(xiàn)數(shù)據(jù)庫(kù)查詢功能,所以我們需要在接口上添加 @Mapper 注解。
@Mapper public interface UserDao { ?? ?... }
但是,僅僅使用@Mapper注解,我們會(huì)發(fā)現(xiàn),在其他變量中依賴注入,IDEA 會(huì)提示錯(cuò)誤,但是不影響運(yùn)行(親測(cè)~)。
因?yàn)槲覀儧]有顯式標(biāo)注這是一個(gè) Bean,IDEA 認(rèn)為運(yùn)行的時(shí)候會(huì)找不到實(shí)例注入,所以提示我們錯(cuò)誤。
如下圖,會(huì)有紅色波浪線。
盡管這個(gè)錯(cuò)誤提示并不影響運(yùn)行,但是看起來(lái)很不舒服,所以我們可以在對(duì)應(yīng)的接口上添加 bean 的聲明,如下:
@Repository // 也可以使用@Component,效果都是一樣的,只是為了聲明為bean @Mapper public interface UserDao { @Insert("insert into user(account, password, user_name) " + "values(#{user.account}, #{user.password}, #{user.name})") int insertUser(@Param("user") User user) throws RuntimeException; }
基于注解的開發(fā)也有其他手段幫助 Mybatis 找到 mapper,那就是 @MapperScan 注解,可以在啟動(dòng)類上添加該注解,自動(dòng)掃描包路徑下的所有接口。
@SpringBootApplication @MapperScan("com.scut.thunderlearn.dao") public class UserEurekaClientApplication { public static void main(String[] args) { SpringApplication.run(UserEurekaClientApplication.class, args); } }
但是感覺有些麻煩。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
完美解決Eclipse導(dǎo)入的項(xiàng)目上有個(gè)紅叉,但不影響項(xiàng)目運(yùn)行的問題
這篇文章主要介紹了完美解決Eclipse導(dǎo)入的項(xiàng)目上有個(gè)紅叉,但不影響項(xiàng)目運(yùn)行的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01Mybatis返回值(resultType&resultMap)的具體使用
返回值屬性有兩種設(shè)置,一種是resultType,一種是resultMap,本文主要介紹了Mybatis返回值(resultType&resultMap)的具體使用,具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08基于Java HttpClient和Htmlparser實(shí)現(xiàn)網(wǎng)絡(luò)爬蟲代碼
這篇文章主要介紹了基于Java HttpClient和Htmlparser實(shí)現(xiàn)網(wǎng)絡(luò)爬蟲代碼的相關(guān)資料,需要的朋友可以參考下2015-12-12Spring中@Autowired和@Qualifier注解的3個(gè)知識(shí)點(diǎn)小結(jié)
這篇文章主要介紹了Spring中@Autowired和@Qualifier注解的3個(gè)知識(shí)點(diǎn)小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09Java中String.split()的最詳細(xì)源碼解讀及注意事項(xiàng)
以前經(jīng)常使用String.split()方法,但是從來(lái)沒有注意,下面這篇文章主要給大家介紹了關(guān)于Java中String.split()最詳細(xì)源碼解讀及注意事項(xiàng)的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07Jmeter參數(shù)化實(shí)現(xiàn)原理及過(guò)程解析
這篇文章主要介紹了Jmeter參數(shù)化實(shí)現(xiàn)原理及過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07java線程并發(fā)countdownlatch類使用示例
javar的CountDownLatch是個(gè)計(jì)數(shù)器,它有一個(gè)初始數(shù),等待這個(gè)計(jì)數(shù)器的線程必須等到計(jì)數(shù)器倒數(shù)到零時(shí)才可繼續(xù)。2014-01-01file.mkdir()、file.mkdirs()和file.createNewFile()的區(qū)別
本文主要介紹了file.mkdir()、file.mkdirs()和file.createNewFile()的區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04