小编给大家分享一下Spring Boot + Mybatis + Spring MVC环境配置中DataSource如何配置,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
一、 在application.properties中设置数据源
#设置Tomcat端口,默认8080server.port=8080#设置项目ContextPathserver.context-path=/#设置Tomcat编码server.tomcat.uri-encoding=UTF-8#设置视图解析器路径spring.mvc.view.prefix=/WEB-INF/views/#设置视图解析器后缀spring.mvc.view.suffix=.jsp#数据库配置mybatis generatorspring.datasource.driver-class-name = com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/mybatis?setUnicode=true&characterEncoding=utf8spring.datasource.username=rootspring.datasource.password=root#数据库配置spring.datasource.test.driver-class-name = com.mysql.jdbc.Driverspring.datasource.test.jdbc-url=jdbc:mysql://localhost:3306/mybatisspring.datasource.test.username=rootspring.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")@Primarypublic 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})
以上是“Spring Boot + Mybatis + Spring MVC环境配置中DataSource如何配置”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注编程网行业资讯频道!