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配置
①自動檢測已存在的Datasource
之前,需要在spring-mybatis.xml中配置datasource的Bean,現(xiàn)在只需要在application.yml中配置到spring.datasource節(jié)點下就可以。因為mybatis-spring-boot支持自動檢測已存在的Datasource。
②將創(chuàng)建并注冊SqlSessionFactoryBean實例,并傳入Datasource。
在mybatis中,sqlsession可以有SqlSessionFactory創(chuàng)建;而在mybatis-spring中則需要SqlSessionFactoryBean來創(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支持自動創(chuàng)建并注冊SqlSessionFactoryBean,所以以上的配置都不需要了。
③將從SqlSessionFactoryBean中創(chuàng)建并注冊SqlSessionTemplate
SqlSessionTemplate是SqlSession的實現(xiàn)類,較SqlSession的默認實現(xiàn)類DefaultSqlSession來說,是線程安全的。
在mybatis-spring中需要如下配置:
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory" /> </bean>
現(xiàn)在,mybatis-spring-boot支持自動創(chuàng)建并注冊SqlSessionTemplate,所以不需要以上配置了。
SqlSession對象注入,如下:
@Autowired private SqlSession sqlSession;
::真不知道既然下面④都能注入mappers了,那還要SqlSession對象有什么用。。::
④自動掃描mappers,將其關(guān)聯(lián)到SqlSessionTemplate,并將mappers注冊到spring容器中,以便注入到我們的beans中。
默認情況下,mybatis-spring-boot將搜索被@Mapper注釋標(biāo)注的mappers。
文檔中描述可以用mybatis-spring提供的@MapperScan標(biāo)注,但我不會用。
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接口類了,所以就不需要上述配置。
::其實感覺上述配置還是挺好的,不用每個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)化詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
LambdaQueryWrapper與QueryWrapper的使用方式
這篇文章主要介紹了LambdaQueryWrapper與QueryWrapper的使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05Mybatis配置之typeAlias標(biāo)簽的用法
這篇文章主要介紹了Mybatis配置之typeAlias標(biāo)簽的用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07解釋為什么Java中“1000==1000”為false而”100==100“為true
在日常編程中,我們經(jīng)常遇到一些看似簡單卻隱藏著復(fù)雜邏輯的問題,這篇文章主要介紹了解釋為什么Java中“1000==1000”為false而”100==100“為true,需要的朋友可以參考下2024-01-01SpringCloud+Redis實現(xiàn)Api接口限流防止惡意刷接口
接口限流是為了保護系統(tǒng)和服務(wù),防止因為過多的請求而崩潰,本文主要介紹了SpringCloud+Redis實現(xiàn)Api接口限流防止惡意刷接口,具有一定的參考價值,感興趣的可以了解一下2024-03-03Spring Cloud Gateway自定義異常處理Exception Handler的方法小結(jié)
這篇文章主要介紹了Spring Cloud Gateway自定義異常處理Exception Handler的方法,本文通過兩種方法結(jié)合實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08