Spring Boot + Mybatis + Spring MVC環境配置(三):DataSource配置
一、 在application.properties中設定資料來源
#設定Tomcat埠,預設8080 server.port=8080 #設定專案ContextPath server.context-path=/ #設定Tomcat編碼 server.tomcat.uri-encoding=UTF-8 #設定檢視解析器路徑 spring.mvc.view.prefix=/WEB-INF/views/ #設定檢視解析器字尾 spring.mvc.view.suffix=.jsp #資料庫配置mybatis generator spring.datasource.driver-class-name = com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?setUnicode=true&characterEncoding=utf8 spring.datasource.username=root spring.datasource.password=root #資料庫配置 spring.datasource.test.driver-class-name = com.mysql.jdbc.Driver spring.datasource.test.jdbc-url=jdbc:mysql://localhost:3306/mybatis spring.datasource.test.username=root spring.datasource.test.password=root #配置.xml檔案路徑 mybatis.mapper-locations=classpath:/com/kai/demo/mapper/*.xml #配置模型路徑 mybatis.type-aliases-package=com.kai.demo.model
二、DataSource建立,DataSourceConfig.java
@Configuration @MapperScan(basePackages = "com.kai.demo.dao") @Primary @PropertySource("classpath:application.properties") public class DataSourceConfig { //mybatis 的mapper配置檔案地址 @Value("${mybatis.mapper-locations}") private String mybatisMapper; @Primary @Bean(name = "testDataSource") @ConfigurationProperties(prefix = "spring.datasource.test") public DataSource testDataSource() { return DataSourceBuilder.create().build(); } @Primary @Bean(name = "testSqlSessionFactory") public SqlSessionFactory testSqlSessionFactory(@Qualifier("testDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mybatisMapper)); try { return bean.getObject(); }catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } @Primary @Bean(name = "testTransactionManager") public DataSourceTransactionManager testTransactionManager(@Qualifier("testDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Primary @Bean(name = "testSqlSessionTemplate") public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("testSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } }
指定要掃描的Mapper類的包的路徑,如果不指定,需要在每個Mapper類裡新增@Mapper註解
@MapperScan(basePackages = "com.kai.demo.dao")
指定配置檔案地址,配置檔案是application.properties時,可以省略
@PropertySource("classpath:application.properties")
當有多個資料來源配置是,使用@Primary指定當前資料庫為主要的資料來源
指名用的是哪個資料來源,testDataSource為DataSourceConfg開始建立的資料來源
@Qualifier("testDataSource")
進行了自定義的DataSource的話,Application.java 中需要加(exclude= {DataSourceAutoConfiguration.class})來排除掉自動配置的DataSource
@EnableAutoConfiguration(exclude= {DataSourceAutoConfiguration.class})
三、如果使用的Spring Boot自動配置的DataSource,只需要進行MapperLocation配置就可使用Mybatis了
@Configuration @MapperScan(basePackages = "com.kai.demo.dao") @Primary public class DefaultDataSource { //mybatis 的mapper配置檔案地址 @Value("${mybatis.mapper-locations}") private String mybatisMapper; @Bean public SqlSessionFactory setSqlSessionFactory(DataSource dataSource) throws IOException { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mybatisMapper)); try { return bean.getObject(); }catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } }
這個時候Appliation.java中就不能有(exclude= {DataSourceAutoConfiguration.class})
完整環境下載地址: https://github.com/CatherineHu/Spring-Boot-Mybatis-MVC
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10314474/viewspace-2200334/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Spring Boot + Mybatis + Spring MVC環境配置(二):Mybatis Generator配置Spring BootMyBatisMVC
- Spring Boot + Mybatis + Spring MVC環境配置(四):MVC框架搭建Spring BootMyBatisMVC框架
- Spring Boot + Mybatis + Spring MVC環境配置(五):templates模板使用Spring BootMyBatisMVC
- Spring Boot + Mybatis + Spring MVC環境配置(一) :Spring Boot初始化,依賴新增Spring BootMyBatisMVC
- Spring Boot:Spring Boot配置MybatisSpring BootMyBatis
- Spring Boot - Profile不同環境配置Spring Boot
- IDEA配置SSM(Spring-Spring MVC-MyBatisIdeaSSMSpringMVCMyBatis
- spring boot學習4 多環境配置Spring Boot
- Spring Boot 2.4 對多環境配置的支援更改Spring Boot
- Spring Boot:Spring Boot配置SwaggerSpring BootSwagger
- 【教程】Spring+Mybatis環境配置多資料來源SpringMyBatis
- Linux系統CentOS 7配置Spring Boot執行環境LinuxCentOSSpring Boot
- Spring Boot & 配置Spring Boot
- Spring Boot MyBatis配置多種資料庫Spring BootMyBatis資料庫
- Spring boot學習(三) Spring boot整合mybatisSpring BootMyBatis
- Spring(環境搭建&配置詳解)Spring
- spring、spring MVC、spring BootMVCSpring Boot
- SMM(spring +springmvc+mybatis)依賴註解等環境配置SpringMVCMyBatis
- MyBatis Spring MapperScannerConfigurer 配置MyBatisSpringAPP
- MyBatis Spring SqlSessionFactoryBean 配置MyBatisSpringSQLSessionBean
- spring boot 配置 JPASpring Boot
- Spring Boot核心配置Spring Boot
- spring boot環境抽象Spring Boot抽象
- Spring Boot - 多模組多環境配置,大廠必備技能Spring Boot
- Spring Boot系列(三):Spring Boot整合Mybatis原始碼解析Spring BootMyBatis原始碼
- Spring MVC整合redis的配置SpringMVCRedis
- Spring Boot 原始碼分析 資料來源 + Mybatis 配置Spring Boot原始碼MyBatis
- Spring boot學習(二) Spring boot基礎配置Spring Boot
- Spring Boot學習筆記:Spring Boot核心配置Spring Boot筆記
- Spring Boot 配置檔案Spring Boot
- Spring Boot中配置 AuditListenerSpring Boot
- spring boot 配置外接tomcatSpring BootTomcat
- Spring Boot Security配置教程Spring Boot
- Spring Boot 配置介面 WebMvcConfigurerSpring BootWebMVC
- Spring Boot 支援 JSP配置Spring BootJS
- 【Spring Boot】yaml配置注入Spring BootYAML
- Spring Boot日誌配置Spring Boot
- spring cloud 信用盤程式搭建環境配置SpringCloud