手记

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

说到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层调用即可

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

0人推荐
随时随地看视频
慕课网APP