您现在的位置是:亿华云 > IT科技

Spring Boot + Mybatis + Spring MVC环境配置(三):DataSource配置

亿华云2025-10-02 19:02:31【IT科技】1人已围观

简介一、 在application.properties中设置数据源 1 2 3

一、环境 在application.properties中设置数据源 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #设置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

Spring Boot + Mybatis + Spring MVC环境配置(三):DataSource配置

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 @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注解

Spring Boot + Mybatis + Spring MVC环境配置(三):DataSource配置

1 @MapperScan(basePackages = "com.kai.demo.dao")

指定配置文件地址,亿华云计算环境配置文件是配置配置application.properties时,可以省略

Spring Boot + Mybatis + Spring MVC环境配置(三):DataSource配置

1 @PropertySource("classpath:application.properties")

当有多个数据源配置是环境,使用@Primary指定当前数据库为主要的配置配置数据源

指名用的是哪个数据源,testDataSource为DataSourceConfg开始创建的环境数据源

1 @Qualifier("testDataSource")

进行了自定义的源码库DataSource的话,Application.java 中需要加(exclude= { DataSourceAutoConfiguration.class})来排除掉自动配置的配置配置DataSource

1 @EnableAutoConfiguration(exclude= { DataSourceAutoConfiguration.class})

三、如果使用的环境Spring Boot自动配置的DataSource,只需要进行MapperLocation配置就可使用Mybatis了

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 @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  

很赞哦!(93)