填坑填坑填坑填坑填坑填坑填坑填坑填坑填坑填坑,如果使用druid连接池一定要查看下面这篇文章:
springboot数据库信息配置文件:
#mySQL spring.datasource.secondary.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.secondary.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.secondary.url=jdbc:mysql://127.0.0.1:3306/testdb spring.datasource.secondary.username=root spring.datasource.secondary.password=root #Oracle spring.datasource.driver-class-name=oracle.jdbc.OracleDriver spring.datasource.url=jdbc:oracle:thin:@127.0.0.1:1521:testuserdb spring.datasource.username=root spring.datasource.password=root
注解介绍:
@Primary:自动装配时当出现多个Bean候选者时,被注解为@Primary的Bean将作为首选者,否则将抛出异常
@Configuration:把一个类作为一个IoC容器,它的某个方法头上如果注册了@Bean,就会作为这个Spring容器中的Bean。
@MapperScan:通过使用@MapperScan可以指定要扫描的Mapper类的包的路径
配置第一个数据源:
@Configuration // 扫描 Mapper 接口并容器管理 @SpringBootApplication @MapperScan(basePackages = OneMappingConfig.PACKAGE, sqlSessionFactoryRef = "oneSqlSessionFactory") public class OneMappingConfig { // 精确到 onedao目录,以便跟其他数据源隔离 static final String PACKAGE = "com.my.maple.onedao"; static final String MAPPER_LOCATION = "classpath:/mapper/onemapper/*.xml"; @Bean(name = "oneDataSource") @ConfigurationProperties(prefix = "spring.datasource.one") @Primary public DataSource oneDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "oneSqlSessionFactory") @Primary public SqlSessionFactory oneSqlSessionFactory(@Qualifier("oneDataSource") DataSource oneDataSource) throws Exception { SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(oneDataSource); sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver() .getResources(OneMappingConfig.MAPPER_LOCATION)); return sessionFactory.getObject(); } @Bean(name = "oneTransactionManager") @Primary public DataSourceTransactionManager oneTransactionManager(@Qualifier("oneDataSource") DataSource oneDataSource) { return new DataSourceTransactionManager(oneDataSource); } @Bean(name = "oneSqlSessionTemplate") @Primary public SqlSessionTemplate oneSqlSessionTemplate(@Qualifier("oneSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } }
配置第二个数据源:
@Configuration @SpringBootApplication // 扫描 Mapper 接口并容器管理 @MapperScan(basePackages = TwoMappingConfig.PACKAGE, sqlSessionFactoryRef = "twoSqlSessionFactory") public class TwoMappingConfig { static final String PACKAGE = "com.my.maple.twodao"; static final String MAPPER_LOCATION = "classpath:/mapper/twomapper/*.xml"; @Bean(name = "twoDataSource") @ConfigurationProperties(prefix = "spring.datasource.two") public DataSource twoDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "twoSqlSessionFactory") public SqlSessionFactory twoSqlSessionFactory(@Qualifier("twoDataSource") DataSource masterDataSource) throws Exception { final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(masterDataSource); sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(TwoMappingConfig.MAPPER_LOCATION)); return sessionFactory.getObject(); } @Bean(name = "twoTransactionManager") public DataSourceTransactionManager twoTransactionManager(@Qualifier("twoDataSource") DataSource masterDataSource) { return new DataSourceTransactionManager(masterDataSource); } @Bean(name = "twoSqlSessionTemplate") public SqlSessionTemplate twoSqlSessionTemplate(@Qualifier("twoSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } }
不同数据源获取测试:
Controller
@RestController @RequestMapping("/test") public class TestController { @Autowired private TestService testService; @PostMapping(value = "/index") public JSONObject index(){ JSONObject json = new JSONObject(); List<One> oneData = testService.selectOneData(); List<Two> twoData = testService.selectTwoData(); json.put("oneData", oneData); json.put("twoData", twoData); return json; } }
Service
@Service("testService") public class TestServiceImpl implements TestService{ @Autowired private OneMapper oneMapper; @Autowired private TwoMapper twoMapper; @Override public List<One> selectOneData() { return oneMapper.getOneData(); } @Override public List<Two> selectTwoData() { return twoMapper.getTwoData(); } }
OneMapper.java
@Mapper public interface OneMapper extends BaseMapper<One> { List<One> getOneData(); }
TwoMapper.java
@Mapper public interface TwoMapper extends BaseMapper<Two> { List<Two> getTwoData(); }
OneMapper.xml
<select id="getOneData" resultMap="BaseResultMap"> SELECT * FROM ONE </select>
TwoMapper.xml
<select id="getTwoData" resultMap="BaseResultMap"> SELECT * FROM TWO </select>
测试结果:
{ "twoData": [ { "id": 1, "twoAaa": "数据库2数据aaa", "twoBbb": "数据库2数据bbb", "twoCcc": "数据库2数据ccc", "twoDdd": "数据库2数据ddd" }, { "id": 2, "twoAaa": "数据库2", "twoBbb": "数据库2", "twoCcc": "数据库2", "twoDdd": "数据库2" } ], "oneData": [ { "id": 1, "oneAaa": "数据库1数据aaa", "oneBbb": "数据库1数据bbb", "oneCcc": "数据库1数据ccc", "oneDdd": "数据库1数据ddd" }, { "id": 2, "oneAaa": "数据库1", "oneBbb": "数据库1", "oneCcc": "数据库1", "oneDdd": "数据库1" } ] }