项目框架图:
yml配置文件:
server:
port: 8081
db1url:
db1username:
db1password:
db1driverclassname: com.mysql.jdbc.Driver
db2url:
db2username:
db2password:
db2driverclassname: com.mysql.jdbc.Driver
看启动类 这里要注意要用exclude把自动配置数据库排除
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@ComponentScan("com.baibing.demo")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
datasourceConfig类 建立两个数据源:
@Configuration
public class DataSourceConfig {
@Value("${db1url}")
private String db1url;
@Value("${db1username}")
private String db1username;
@Value("${db1password}")
private String db1password;
@Value("${db1driverclassname}")
private String db1driverclassname;
@Value("${db2url}")
private String db2url;
@Value("${db2username}")
private String db2username;
@Value("${db2password}")
private String db2password;
@Value("${db2driverclassname}")
private String db2driverclassname;
@Bean(name = "db1")
public DataSource dataSource1() {
DruidDataSource datasource = new DruidDataSource();
datasource.setUrl(db1url);
datasource.setUsername(db1username);
datasource.setPassword(db1password);
datasource.setDriverClassName(db1driverclassname);
return datasource;
}
@Bean(name = "db2")
public DataSource dataSource2() {
DruidDataSource datasource = new DruidDataSource();
datasource.setUrl(db2url);
datasource.setUsername(db2username);
datasource.setPassword(db2password);
datasource.setDriverClassName(db2driverclassname);
return datasource;
}
}
mybatis1Config :
@Configuration
@MapperScan(basePackages = {"com.baibing.demo.dao1"}, sqlSessionFactoryRef = "sqlSessionFactory1")
public class MybatisDb1Config {
@Autowired
@Qualifier("db1")
private DataSource db1;
@Bean
public SqlSessionFactory sqlSessionFactory1() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(db1);
factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("com/baibing/demo/mapper1/*.xml")
);
return factoryBean.getObject();
}
@Bean
public SqlSessionTemplate sqlSessionTemplate1() throws Exception {
SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory1());
return template;
}
}
同理第二个mybatis配置:
@Configuration
@MapperScan(basePackages = {"com.baibing.demo.dao"}, sqlSessionFactoryRef = "sqlSessionFactory2")
public class MybatisDb2Config {
@Autowired
@Qualifier("db2")
private DataSource db2;
@Bean
public SqlSessionFactory sqlSessionFactory2() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(db2);
factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("com/baibing/demo/mapper/*.xml")
);
return factoryBean.getObject();
}
@Bean
public SqlSessionTemplate sqlSessionTemplate2() throws Exception {
SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory2());
return template;
}
}
dao和mapper不再详解
Controller:
@Controller
@RequestMapping("/test")
public class TestController {
@Resource
userDao userdao;
@Resource
userDao1 userdao1;
@RequestMapping("/getUser")
@ResponseBody
public User getUser(@RequestBody String params) {
User userByName = userdao.getUserByName("baibing");
return userByName;
}
@RequestMapping("/getUser1")
@ResponseBody
public User getUser1(@RequestBody String params) {
User userByName = userdao1.getUserByName("baibing");
return userByName;
}
}
分别调用两个接口后可以看出初始化了两个数据源