为了账号安全,请及时绑定邮箱和手机立即绑定

Spring boot最最简单的多数据源datasource连接方式

标签:
Oracle

说到Spring boot多数据源spring.datasource配置,下面是基于Mybatis,有些dao操作要连接数据库1,有些dao操作要连接数据库2,网上有比较多方式,但是都比较复杂,难懂,有些还有点老旧了,上一个最简单的方式,希望对大家有帮助。

代码如下,

1、配置文件application-prod.properties

spring.datasource.ds1.url=jdbc:mysql://url-1:3306/user?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&roundRobinLoadBalance=true&autoReconnect=true
spring.datasource.ds1.username=
spring.datasource.ds1.password=
spring.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver

#mysql
spring.datasource.ds2.url=jdbc:mysql://url-2:3306/bbs?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&roundRobinLoadBalance=true&autoReconnect=true
spring.datasource.ds2.username=
spring.datasource.ds2.password=
spring.datasource.ds2.driver-class-name=com.mysql.jdbc.Driver

2、最重要的DataSource配置

package com.test.ds.cloud.datasource;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
@Configuration
@MapperScan(basePackages = “com.test.ds.dao.mapuser”, sqlSessionTemplateRef = “ds1SqlSessionTemplate”)
public class DataSourceDs1Config {

@Bean(name = "ds1DataSource")
@ConfigurationProperties(prefix = "spring.datasource.ds1")
@Primary
public DataSource testDataSource() {
    return DataSourceBuilder.create().build();
}

@Bean(name = "ds1SqlSessionFactory")
@Primary
public SqlSessionFactory testSqlSessionFactory(@Qualifier("ds1DataSource") DataSource dataSource) throws Exception {
    SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    bean.setDataSource(dataSource);
    bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:com/test/ds/dao/mapuser/*.xml"));
    return bean.getObject();
}

@Bean(name = "ds1TransactionManager")
@Primary
public DataSourceTransactionManager testTransactionManager(@Qualifier("ds1DataSource") DataSource dataSource) {
    return new DataSourceTransactionManager(dataSource);
}

@Bean(name = "ds1SqlSessionTemplate")
@Primary
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("ds1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
    return new SqlSessionTemplate(sqlSessionFactory);
}

}
注意:

1)指明扫描dao层:@MapperScan(basePackages = “com.test.ds.dao.mapuser”, sqlSessionTemplateRef = “test1SqlSessionTemplate”)

2)@ConfigurationProperties(prefix = “spring.datasource.ds1”)要跟富贵论坛配置文件前缀一致。

3)mapping配置路径也要配置正确:bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(“classpath:com/test/ds/dao/mapuser/*.xml”));

DataSourceDs2Config.java同上,相应的dao配置文件目录对清楚即可,比如com/test/ds/dao/mapbbs,就不贴上了。

3、dao层

package com.test.ds.cloud.dao.maptopic;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.HashMap;
import java.util.List;

@Mapper
public interface CommunityTopicMapper {

@Select("select count(id) from t_user where name ='${name}'")
Integer countNumByName(@Param("name") String name);

}

4、Service层调用即可

非常方便快捷,希望对大家有帮忙

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消