Spring Boot集成MyBatis的方法
Spring Boot 集成MyBatis
在集成MyBatis前,我們先配置一個(gè)druid數(shù)據(jù)源。
Spring Boot 集成druid
druid
有很多個(gè)配置選項(xiàng),使用Spring Boot 的配置文件可以方便的配置druid
。
在application.yml
配置文件中寫上:
spring: datasource: name: test url: jdbc:mysql://192.168.16.137:3306/test username: root password: # 使用druid數(shù)據(jù)源 type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver filters: stat maxActive: 20 initialSize: 1 maxWait: 60000 minIdle: 1 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: select 'x' testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true maxOpenPreparedStatements: 20
這里通過type: com.alibaba.druid.pool.DruidDataSource
配置即可!
Spring Boot 集成MyBatis
Spring Boot 集成MyBatis有兩種方式,一種簡單的方式就是使用MyBatis官方提供的:
另外一種方式就是仍然用類似mybatis-spring
的配置方式,這種方式需要自己寫一些代碼,但是可以很方便的控制MyBatis的各項(xiàng)配置。
一、mybatis-spring-boot-starter方式
在pom.xml
中添加依賴:
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.0.0</version> </dependency>
mybatis-spring-boot-starter
依賴樹如下:
- 其中mybatis使用的3.3.0版本,可以通過: <mybatis.version>3.3.0</mybatis.version>屬性修改默認(rèn)版本。
- mybatis-spring使用版本1.2.3,可以通過: <mybatis-spring.version>1.2.3</mybatis-spring.version>修改默認(rèn)版本。
在application.yml中增加配置:
mybatis: mapperLocations: classpath:mapper/*.xml typeAliasesPackage: tk.mapper.model
除了上面常見的兩項(xiàng)配置,還有:
- mybatis.config:mybatis-config.xml配置文件的路徑
- mybatis.typeHandlersPackage:掃描typeHandlers的包
- mybatis.checkConfigLocation:檢查配置文件是否存在
- mybatis.executorType:設(shè)置執(zhí)行模式(SIMPLE, REUSE, BATCH),默認(rèn)為SIMPLE
二、mybatis-spring方式
這種方式和平常的用法比較接近。需要添加mybatis依賴和mybatis-spring依賴。
然后創(chuàng)建一個(gè)MyBatisConfig配置類:
/** * MyBatis基礎(chǔ)配置 */ @Configuration @EnableTransactionManagement public class MyBatisConfig implements TransactionManagementConfigurer { @Autowired DataSource dataSource; @Bean(name = "sqlSessionFactory") public SqlSessionFactory sqlSessionFactoryBean() { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setTypeAliasesPackage("tk.mybatis.springboot.model"); //分頁插件 PageHelper pageHelper = new PageHelper(); Properties properties = new Properties(); properties.setProperty("reasonable", "true"); properties.setProperty("supportMethodsArguments", "true"); properties.setProperty("returnPageInfo", "check"); properties.setProperty("params", "count=countSql"); pageHelper.setProperties(properties); //添加插件 bean.setPlugins(new Interceptor[]{pageHelper}); //添加XML目錄 ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); try { bean.setMapperLocations(resolver.getResources("classpath:mapper/*.xml")); return bean.getObject(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } @Bean public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } @Bean @Override public PlatformTransactionManager annotationDrivenTransactionManager() { return new DataSourceTransactionManager(dataSource); } }
上面代碼創(chuàng)建了一個(gè)SqlSessionFactory
和一個(gè)SqlSessionTemplate
,為了支持注解事務(wù),增加了@EnableTransactionManagement
注解,并且反回了一個(gè)PlatformTransactionManagerBean
。
另外應(yīng)該注意到這個(gè)配置中沒有MapperScannerConfigurer
,如果我們想要掃描MyBatis的Mapper接口,我們就需要配置這個(gè)類,這個(gè)配置我們需要單獨(dú)放到一個(gè)類中。
/** * MyBatis掃描接口 */ @Configuration //TODO 注意,由于MapperScannerConfigurer執(zhí)行的比較早,所以必須有下面的注解 @AutoConfigureAfter(MyBatisConfig.class) public class MyBatisMapperScannerConfig { @Bean public MapperScannerConfigurer mapperScannerConfigurer() { MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer(); mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory"); mapperScannerConfigurer.setBasePackage("tk.mybatis.springboot.mapper"); return mapperScannerConfigurer; } }
這個(gè)配置一定要注意@AutoConfigureAfter(MyBatisConfig.class)
,必須有這個(gè)配置,否則會(huì)有異常。原因就是這個(gè)類執(zhí)行的比較早,由于sqlSessionFactory
還不存在,后續(xù)執(zhí)行出錯(cuò)。
做好上面配置以后就可以使用MyBatis了。
關(guān)于分頁插件和通用Mapper集成
分頁插件作為插件的例子在上面代碼中有。
通用Mapper配置實(shí)際就是配置MapperScannerConfigurer
的時(shí)候使用tk.mybatis.spring.mapper.MapperScannerConfigurer
即可,配置屬性使用Properties
。
Spring Boot集成MyBatis的基礎(chǔ)項(xiàng)目
我上傳到github一個(gè)采用第二種方式的集成項(xiàng)目,并且集成了分頁插件和通用Mapper,項(xiàng)目包含了簡單的配置和操作,僅作為參考。
項(xiàng)目地址:https://github.com/abel533/MyBatis-Spring-Boot
分頁插件和通用Mapper的相關(guān)信息可以通過上面地址找到。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
- 淺談Spring Boot 整合ActiveMQ的過程
- 詳解spring boot整合JMS(ActiveMQ實(shí)現(xiàn))
- springboot集成activemq的實(shí)例代碼
- SpringBoot AOP控制Redis自動(dòng)緩存和更新的示例
- Spring Boot整合logback一個(gè)簡單的日志集成架構(gòu)
- 在SpringBoot項(xiàng)目中利用maven的generate插件
- SpringBoot靜態(tài)資源目錄訪問
- Spring Boot 靜態(tài)資源處理
- 詳解Spring Boot中PATCH上傳文件的問題
- Spring Boot與ActiveMQ整合的步驟
相關(guān)文章
maven將項(xiàng)目打包上傳到nexus私服的詳細(xì)教程
這篇文章主要介紹了maven將項(xiàng)目打包上傳到nexus私服,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2020-07-07Synchronized?和?ReentrantLock?的實(shí)現(xiàn)原理及區(qū)別
這篇文章主要介紹了Synchronized?和?ReentrantLock?的實(shí)現(xiàn)原理及區(qū)別,文章為榮啊主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09分享幾個(gè)Java工作中實(shí)用的代碼優(yōu)化技巧
這篇文章主要給大家分享幾個(gè)Java工作中實(shí)用代碼優(yōu)化技巧,文章基于Java的相關(guān)資料展開對(duì)其優(yōu)化技巧的分享,需要的小伙伴可以參考一下2022-04-04SpringCloud OpenFeign超詳細(xì)講解模板化遠(yuǎn)程通信的實(shí)現(xiàn)
這篇文章主要介紹了SpringCloudSpringboot集成OpenFeign實(shí)現(xiàn)模板化遠(yuǎn)程通信,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2022-07-07Mybatis-plus通用查詢方法封裝的實(shí)現(xiàn)
本文主要介紹了Mybatis-plus通用查詢方法封裝的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07Springboot使用redis實(shí)現(xiàn)接口Api限流的示例代碼
本文主要介紹了Springboot使用redis實(shí)現(xiàn)接口Api限流的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07JAVA中 Spring定時(shí)器的兩種實(shí)現(xiàn)方式
本文向您介紹Spring定時(shí)器的兩種實(shí)現(xiàn)方式,包括Java Timer定時(shí)和Quartz定時(shí)器,兩種Spring定時(shí)器的實(shí)現(xiàn)方式各有優(yōu)點(diǎn),可結(jié)合具體項(xiàng)目考慮是否采用。2015-09-09