基本上和mybatis的多数据源配置是一样的,区别是将与原生的SqlSessionFactory替换成mybatis plus的就行
mybatis多数据源配置:https://blog.csdn.net/csdndemimang/article/details/108017714
示例:(就是这句不一样MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();)
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = "com.hn.demo.ds1", sqlSessionTemplateRef = "ds1SqlSessionTemplate")
public class DataSourceConfig1 {
@Bean(name = "ds1DataSource")
@ConfigurationProperties(prefix = "spring.datasource.ds1")
public DataSource testDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "ds1SqlSessionFactory")
public SqlSessionFactory testSqlSessionFactory(@Qualifier("ds1DataSource") DataSource dataSource) throws Exception {
//用的是mybatisplus,要用mp的SqlSession
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/ds1/*.xml"));
return bean.getObject();
}
@Bean(name = "ds1TransactionManager")
public DataSourceTransactionManager testTransactionManager(@Qualifier("ds1DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "ds1SqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("ds1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}