本篇文章給大家分享的是有關(guān)Spring Boot 如何實現(xiàn)與MyBatis搭配使用,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
目前創(chuàng)新互聯(lián)已為近千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)絡(luò)空間、網(wǎng)站改版維護、企業(yè)網(wǎng)站設(shè)計、陽明網(wǎng)站維護等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
Spring Boot是由Pivotal團隊提供的全新框架,其設(shè)計目的是用來簡化新Spring應(yīng)用的初始搭建以及開發(fā)過程。該框架使用了特定的方式來進行配置,從而使開發(fā)人員不再需要定義樣板化的配置。通過這種方式,Spring Boot致力于在蓬勃發(fā)展的快速應(yīng)用開發(fā)領(lǐng)域(rapid application development)成為領(lǐng)導(dǎo)者。
在集成MyBatis前,我們先配置一個druid數(shù)據(jù)源。
Spring Boot 系列
1.Spring Boot 入門
2.Spring Boot 屬性配置和使用
3.Spring Boot 集成MyBatis
4.Spring Boot 靜態(tài)資源處理
5.Spring Boot - 配置排序依賴技巧
Spring Boot 集成druid
druid有很多個配置選項,使用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-boot-starter
另外一種方式就是仍然用類似mybatis-spring的配置方式,這種方式需要自己寫一些代碼,但是可以很方便的控制MyBatis的各項配置。
一、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>屬性修改默認版本。
mybatis-spring使用版本1.2.3,可以通過:
<mybatis-spring.version>1.2.3</mybatis-spring.version>修改默認版本。
在application.yml中增加配置:
mybatis:
mapperLocations: classpath:mapper/*.xml
typeAliasesPackage: tk.mapper.model
除了上面常見的兩項配置,還有:
二、mybatis-spring方式
這種方式和平常的用法比較接近。需要添加mybatis依賴和mybatis-spring依賴。
然后創(chuàng)建一個MyBatisConfig配置類:
/**
* MyBatis基礎(chǔ)配置
*
* @author liuzh
* @since 2015-12-19 10:11
*/
@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)建了一個SqlSessionFactory和一個SqlSessionTemplate,為了支持注解事務(wù),增加了@EnableTransactionManagement注解,并且反回了一個PlatformTransactionManagerBean。
另外應(yīng)該注意到這個配置中沒有MapperScannerConfigurer,如果我們想要掃描MyBatis的Mapper接口,我們就需要配置這個類,這個配置我們需要單獨放到一個類中。
/**
* MyBatis掃描接口
*
* @author liuzh
* @since 2015-12-19 14:46
*/
@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;
}
}這個配置一定要注意@AutoConfigureAfter(MyBatisConfig.class),必須有這個配置,否則會有異常。原因就是這個類執(zhí)行的比較早,由于sqlSessionFactory還不存在,后續(xù)執(zhí)行出錯。
做好上面配置以后就可以使用MyBatis了。
關(guān)于分頁插件和通用Mapper集成
分頁插件作為插件的例子在上面代碼中有。
通用Mapper配置實際就是配置MapperScannerConfigurer的時候使用tk.mybatis.spring.mapper.MapperScannerConfigurer即可,配置屬性使用Properties。
以上就是Spring Boot 如何實現(xiàn)與MyBatis搭配使用,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降摹OM隳芡ㄟ^這篇文章學到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)站欄目:SpringBoot如何實現(xiàn)與MyBatis搭配使用
當前路徑:http://chinadenli.net/article26/jgjpjg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動網(wǎng)站建設(shè)、全網(wǎng)營銷推廣、做網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站制作、定制開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)