springboot入门_多数据源
最近事情比较多,springboot内容的学习有所懈怠,今天抽出点时间,记录下springboot中多数据源的使用。
一般在稍微大一点的项目中,根据业务的划分,会用到不同的数据库,下面来看下在springboot中如何配置使用多数据源。本文我们使用mybatis。
1 我们创建一个springboot整合mybatis的工程,此处就在啰嗦了,不会的同学可以参考我的上一篇笔记http://www.cnblogs.com/Allen-Wl/p/8482148.html。
2 在application.properties配置文件中添加多个数据源信息,并在数据库中创建表(数据源1库:springboot,表:customer;数据源2库:test,表:user)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 ## dataSource 2 spring.datasource.driverClassName = com.mysql.jdbc.Driver 3 spring.datasource.url = jdbc:mysql://localhost:3306/springboot 4 spring.datasource.username = root 5 spring.datasource.password = 123456 6 7 ## dataSource2 8 spring.datasource.second.driverClassName = com.mysql.jdbc.Driver 9 spring.datasource.second.url = jdbc:mysql://localhost:3306/test 10 spring.datasource.second.username = root 11 spring.datasource.second.password = 123456
3 分别创建2个不同的数据源的config文件。有多个数据源时,我们需要指定一个数据源作为默认的主数据源,使用@Primary注解即可。我们可以通过@MapperScan(basePackages={...})来指定哪些文件会访问当前数据源。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 @Configuration 2 @MapperScan(basePackages = {"com.allen.springboot.learn.mapper"}, sqlSessionTemplateRef="sqlSessionTemplate") 3 public class MybatisDataSourceConfigDefault { 4 5 @Bean(name = "datasource") 6 @Primary //指定为主数据源 7 @ConfigurationProperties(prefix = "spring.datasource") 8 public DataSource dataSource(){ 9 return DataSourceBuilder.create().build(); 10 } 11 12 13 @Bean(name="sqlSessionFactory") 14 @Primary 15 public SqlSessionFactory sqlSessionFactory(@Qualifier("datasource") DataSource dataSource) throws Exception { 16 SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); 17 factoryBean.setDataSource(dataSource); // 使用dataSource数据源, 连接springboot库 18 factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/*.xml")); 19 return factoryBean.getObject(); 20 } 21 22 23 @Bean(name="sqlSessionTemplate") 24 @Primary 25 public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { 26 SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory); // 使用上面配置的Factory 27 return template; 28 } 29 30 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 @Configuration 2 @MapperScan(basePackages = {"com.allen.springboot.learn.mapper1"}, sqlSessionTemplateRef="sqlSessionTemplate1") 3 public class MybatisDataSourceConfigSecond { 4 5 @Bean(name="secondDataSource") 6 @ConfigurationProperties(prefix = "spring.datasource.second") 7 public DataSource secondDataSource(){ 8 return DataSourceBuilder.create().build(); 9 } 10 11 @Bean(name="sqlSessionFactory1") 12 public SqlSessionFactory sqlSessionFactory1(@Qualifier("secondDataSource")DataSource secondDataSource) throws Exception { 13 SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); 14 factoryBean.setDataSource(secondDataSource); // 使用secondDataSource数据源, 连接test库 15 factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/*.xml")); 16 return factoryBean.getObject(); 17 } 18 19 @Bean(name="sqlSessionTemplate1") 20 public SqlSessionTemplate sqlSessionTemplate1(@Qualifier("sqlSessionFactory1")SqlSessionFactory sqlSessionFactory1) throws Exception { 21 SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory1); // 使用上面配置的Factory 22 return template; 23 } 24 25 }
4 创建mapper接口文件,包名要与数据源配置文件中的@MapperScan注解中的保持一直,这样就与数据源对应起来了。
5 创建mapper接口的service服务类。
6 创建controller测试多数据源
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 @RestController 2 public class HelloController { 3 4 @Autowired 5 private CustomerService customerService; 6 @Autowired 7 private UserService userService; 8 9 @RequestMapping("/hello") 10 public Map<String, Object> hello(Model model){ 11 Map<String, Object> map = new HashMap<String, Object>(); 12 System.out.println("----------查询customer信息----------"); 13 Customer c = customerService.selectByPrimaryKey(1); 14 map.put("customer", c); 15 16 System.out.println("----------查询user信息----------"); 17 List<User> userList = userService.getAllUser(); 18 map.put("userList", userList); 19 return map; 20 } 21 22 @RequestMapping("/add") 23 public Map<String, Object> add(Model model){ 24 Map<String, Object> map = new HashMap<String, Object>(); 25 System.out.println("----------添加customer信息----------"); 26 Customer customer = new Customer("花和尚", 20); 27 customerService.insert(customer); 28 map.put("customer", "添加customer成功"); 29 30 System.out.println("----------添加user信息----------"); 31 User user = new User("武松", 18); 32 userService.insert(user); 33 map.put("user", "添加user成功"); 34 return map; 35 } 36 }
通过访问add方法可以看到,数据已经分别插入到了对应的数据库,执行hello方法查询数据,页面可以看到下图内容,说明数据源已经配置成功
至此在springboot中使用多数据源已经实现。有不正确的地方,希望网友可以指明。