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


小编给大家分享一下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 =&nbsp.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  =&nbsp.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:/kai/demo/mapper/*.xml#配置模型路径mybatis.type-aliases-package.kai.demo.model

二、DataSource创建,DataSourceConfig.java

@Configuration@MapperScan(basePackages = &quot.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 = &quot.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 = &quot.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如何配置”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注主机评测网行业资讯频道!


上一篇:springboot?jpa之返回表中部分字段如何处理

下一篇:spring事务如何配置


Copyright © 2002-2019 测速网 www.inhv.cn 皖ICP备2023010105号
测速城市 测速地区 测速街道 网速测试城市 网速测试地区 网速测试街道
温馨提示:部分文章图片数据来源与网络,仅供参考!版权归原作者所有,如有侵权请联系删除!

热门搜索 城市网站建设 地区网站制作 街道网页设计 大写数字 热点城市 热点地区 热点街道 热点时间 房贷计算器