温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Spring Boot + Mybatis + Spring MVC环境配置中DataSource如何配置

发布时间:2021-12-07 11:38:52 来源:亿速云 阅读:278 作者:小新 栏目:编程语言

小编给大家分享一下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})

以上是“Spring Boot + Mybatis + Spring MVC环境配置中DataSource如何配置”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI