代码结构
配置文件代码
package com.example.demo.config; 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.context.annotation.Primary; import javax.sql.DataSource; @Configuration public class DataSourceConfig { @Primary @Bean(name = "primaryDataSource") //指定数据读取配置的前缀 @ConfigurationProperties(prefix = "spring.datasource.primary") public DataSource primaryDataSource(){ return DataSourceBuilder.create().build() ; } @Bean(name = "slaveDataSource") @ConfigurationProperties(prefix = "spring.datasource.slave") public DataSource slaveDataSource(){ return DataSourceBuilder.create().build() ; } } package com.example.demo.config; 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.Autowired; import org.springframework.beans.factory.annotation.Qualifier; 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 javax.sql.DataSource; /** * 数据源primary */ @Configuration @MapperScan(basePackages = {"com.example.demo.mapper.primary"}, sqlSessionFactoryRef = "sqlSessionFactoryPrimary") public class MybatisPrimaryConfig { @Autowired @Qualifier("primaryDataSource") private DataSource primaryDataSource ; @Bean @Primary public SqlSessionFactory sqlSessionFactoryPrimary() throws Exception { SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean() ; factoryBean.setDataSource(primaryDataSource);//设置数据源 factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:primaryMybatis/*Mapper.xml")); return factoryBean.getObject() ; } @Bean public SqlSessionTemplate sqlSessionTemplatePrimary() throws Exception { SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactoryPrimary()) ; return template ; } } package com.example.demo.config; 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.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import javax.sql.DataSource; /** * 数据源二 */ @Configuration //指定basePackages下的mapper使用当前数据源 @MapperScan(basePackages = {"com.example.demo.mapper.slave"}, sqlSessionFactoryRef = "sqlSessionFactorySlave") public class MybatisSlaveConfig { @Autowired @Qualifier("slaveDataSource") private DataSource slaveDataSource ; @Bean public SqlSessionFactory sqlSessionFactorySlave() throws Exception { SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean() ; factoryBean.setDataSource(slaveDataSource); factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources( "classpath*:slaveMybatis/*Mapper.xml")); return factoryBean.getObject() ; } @Bean public SqlSessionTemplate sqlSessionTemplateSlave() throws Exception { SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactorySlave()) ; return template ; } } 测试数据源一 package com.example.demo.mapper.primary; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; @Mapper public interface UserPrimaryMapper { @Select("SELECT addr FROM info WHERE id=1;;") String queryOne( ); } 测试数据源二 package com.example.demo.mapper.slave; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; @Mapper public interface UserSlaveMapper { @Select("SELECT name FROM account WHERE id=1001;") String queryAcc(); } 整合mapper的service package com.example.demo.service; import java.util.Map; public interface TestService { public Map<String,Object> testMysql(); } package com.example.demo.service.impl; import com.example.demo.mapper.primary.UserPrimaryMapper; import com.example.demo.mapper.slave.UserSlaveMapper; import com.example.demo.service.TestService; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.HashMap; import java.util.Map; @Service public class TestServiceImpl implements TestService { @Resource private UserPrimaryMapper userPrimaryMapper; @Resource private UserSlaveMapper userSlaveMapper; public Map<String,Object> testMysql() { String queryOne = userPrimaryMapper.queryOne(); String queryAcc = userSlaveMapper.queryAcc(); Map<String,Object> map = new HashMap<String, Object>(); map.put("primary",queryOne); map.put("slave",queryAcc); return map; } } 测试service的test类 package com.example.demo; import com.example.demo.service.TestService; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import javax.annotation.Resource; import java.util.Map; @RunWith(SpringRunner.class) @SpringBootTest public class ServiceTest { @Resource private TestService testService; @Test public void testMysqlT(){ Map<String,Object> map = testService.testMysql(); System.out.println("<<<<<<<<<<<<<<<<<<<<<<<"+map+">>>>>>>>>>>>>>>>>>>>>>>>>"); Assert.assertNotNull(map); } } 运行结果