spring Boot與Mybatis整合優(yōu)化詳解
SpringBoot官方文檔http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/
關(guān)于spring-boot與mybatis整合優(yōu)化方面的介紹,就是Mybatis-Spring-boot-starter的介紹:
1、取消spring-mybatis.xml配置
①自動(dòng)檢測(cè)已存在的Datasource
之前,需要在spring-mybatis.xml中配置datasource的Bean,現(xiàn)在只需要在application.yml中配置到spring.datasource節(jié)點(diǎn)下就可以。因?yàn)閙ybatis-spring-boot支持自動(dòng)檢測(cè)已存在的Datasource。
②將創(chuàng)建并注冊(cè)SqlSessionFactoryBean實(shí)例,并傳入Datasource。
在mybatis中,sqlsession可以有SqlSessionFactory創(chuàng)建;而在mybatis-spring中則需要SqlSessionFactoryBean來(lái)創(chuàng)建,并傳入datasource。
如:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation"> <value>classpath:mybatis/mapper.xml</value> </property> <property name="dataSource" ref="dataSource" /> </bean>
現(xiàn)在,mybatis-spring-boot支持自動(dòng)創(chuàng)建并注冊(cè)SqlSessionFactoryBean,所以以上的配置都不需要了。
③將從SqlSessionFactoryBean中創(chuàng)建并注冊(cè)SqlSessionTemplate
SqlSessionTemplate是SqlSession的實(shí)現(xiàn)類(lèi),較SqlSession的默認(rèn)實(shí)現(xiàn)類(lèi)DefaultSqlSession來(lái)說(shuō),是線程安全的。
在mybatis-spring中需要如下配置:
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory" /> </bean>
現(xiàn)在,mybatis-spring-boot支持自動(dòng)創(chuàng)建并注冊(cè)SqlSessionTemplate,所以不需要以上配置了。
SqlSession對(duì)象注入,如下:
@Autowired private SqlSession sqlSession;
::真不知道既然下面④都能注入mappers了,那還要SqlSession對(duì)象有什么用。。::
④自動(dòng)掃描mappers,將其關(guān)聯(lián)到SqlSessionTemplate,并將mappers注冊(cè)到spring容器中,以便注入到我們的beans中。
默認(rèn)情況下,mybatis-spring-boot將搜索被@Mapper注釋標(biāo)注的mappers。
文檔中描述可以用mybatis-spring提供的@MapperScan標(biāo)注,但我不會(huì)用。
Mybatis-Spring文檔中解釋@MapperScan注釋跟配置MapperScannerConfigurer是同樣的效果:
public @interface MapperScan Use this annotation to register MyBatis mapper interfaces when using Java Config. It performs when same work as MapperScannerConfigurer via MapperScannerRegistrar. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.example.mappers" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean>
現(xiàn)在,mybatis-spring-boot支持使用@Mapper注釋標(biāo)注mappers接口類(lèi)了,所以就不需要上述配置。
::其實(shí)感覺(jué)上述配置還是挺好的,不用每個(gè)mapper接口都注釋@Mapper。。。::
@Mapper標(biāo)注使用如下:
@Mapper public interface UserMapper { UserInfo queryUser(@Param(value = "userId") int id); }
那么在mybatis-spring-boot中需要配置的是mapper.xml目錄:
mybatis: mapper-locations: classpath:mapper/*.xml
總結(jié)
以上所述是小編給大家介紹的spring Boot與Mybatis整合優(yōu)化詳解,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
LambdaQueryWrapper與QueryWrapper的使用方式
這篇文章主要介紹了LambdaQueryWrapper與QueryWrapper的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05Mybatis配置之typeAlias標(biāo)簽的用法
這篇文章主要介紹了Mybatis配置之typeAlias標(biāo)簽的用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07解釋為什么Java中“1000==1000”為false而”100==100“為true
在日常編程中,我們經(jīng)常遇到一些看似簡(jiǎn)單卻隱藏著復(fù)雜邏輯的問(wèn)題,這篇文章主要介紹了解釋為什么Java中“1000==1000”為false而”100==100“為true,需要的朋友可以參考下2024-01-01SpringCloud+Redis實(shí)現(xiàn)Api接口限流防止惡意刷接口
接口限流是為了保護(hù)系統(tǒng)和服務(wù),防止因?yàn)檫^(guò)多的請(qǐng)求而崩潰,本文主要介紹了SpringCloud+Redis實(shí)現(xiàn)Api接口限流防止惡意刷接口,具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03Spring Cloud Gateway自定義異常處理Exception Handler的方法小結(jié)
這篇文章主要介紹了Spring Cloud Gateway自定義異常處理Exception Handler的方法,本文通過(guò)兩種方法結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08