springboot入门_多数据源
本文记录在springboot中配置多数据源,使用mybatis框架。
创建项目并添加依赖
1 <dependency> 2 <groupId>mysql</groupId> 3 <artifactId>mysql-connector-java</artifactId> 4 <scope>runtime</scope> 5 </dependency> 6 <dependency> 7 <groupId>org.mybatis.spring.boot</groupId> 8 <artifactId>mybatis-spring-boot-starter</artifactId> 9 <version>1.3.2</version> 10 </dependency>
创建与数据库表对应的实体类对象
1 public class City { 2 3 private Long id; 4 private String cityCode; 5 private String cityName; 6 7 //省略get和set方法 8 9 @Override 10 public String toString(){ 11 return "{id:"+this.getId()+",cityName:"+this.getCityName()+",cityCode:"+this.getCityCode()+"}"; 12 } 13 14 }
创建资源文件datasource.properties,并配置2个数据库连接信息
1 #数据库1 2 spring.datasource.one.url=jdbc:mysql://localhost:3306/test 3 spring.datasource.one.username=root 4 spring.datasource.one.password=123456 5 spring.datasource.one.driver-class-name=com.mysql.jdbc.Driver 6 #数据库2 7 spring.datasource.two.url=jdbc:mysql://localhost:3306/test2 8 spring.datasource.two.username=root 9 spring.datasource.two.password=123456 10 spring.datasource.two.driver-class-name=com.mysql.jdbc.Driver
创建2个数据源配置类,完成数据源初始化
1 @Configuration 2 @PropertySource("classpath:datasource.properties") 3 @MapperScan(basePackages = "org.allen.demo.dao.dbOne", sqlSessionTemplateRef = "sqlSessionTemplate1") 4 public class DataSource1Config { 5 6 @Autowired 7 private Environment env; 8 9 @Bean("dataSource1") 10 @Primary 11 public DataSource dataSource(){ 12 System.out.println("初始化数据源1开始。。。"); 13 HikariDataSource ds = new HikariDataSource (); 14 ds.setDriverClassName(env.getProperty("spring.datasource.one.driver-class-name").trim()); 15 ds.setJdbcUrl(env.getProperty("spring.datasource.one.url").trim()); 16 ds.setUsername(env.getProperty("spring.datasource.one.username").trim()); 17 ds.setPassword(env.getProperty("spring.datasource.one.password").trim()); 18 System.out.println("初始化数据源1结束。。。"); 19 return ds; 20 } 21 22 @Bean("sqlSessionFactory1") 23 @Primary 24 public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource1") DataSource dataSource) throws Exception { 25 SqlSessionFactoryBean sfb = new SqlSessionFactoryBean(); 26 sfb.setDataSource(dataSource); 27 return sfb.getObject(); 28 } 29 30 @Bean("sqlSessionTemplate1") 31 @Primary 32 public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory1") SqlSessionFactory sqlSessionFactory){ 33 SqlSessionTemplate sft = new SqlSessionTemplate(sqlSessionFactory); 34 return sft; 35 } 36 37 @Bean 38 @Primary 39 public DataSourceTransactionManager transactionManager(@Qualifier("dataSource1") DataSource dataSource){ 40 return new DataSourceTransactionManager(dataSource); 41 } 42 43 }
1 @Configuration 2 @PropertySource("classpath:datasource.properties") 3 @MapperScan(basePackages = "org.allen.demo.dao.dbTwo", sqlSessionTemplateRef = "sqlSessionTemplate2") 4 public class DataSource2Config { 5 6 @Autowired 7 private Environment env; 8 9 @Bean("dataSource2") 10 public DataSource dataSource(){ 11 System.out.println("初始化数据源2开始。。。"); 12 HikariDataSource ds = new HikariDataSource (); 13 ds.setDriverClassName(env.getProperty("spring.datasource.two.driver-class-name").trim()); 14 ds.setJdbcUrl(env.getProperty("spring.datasource.two.url").trim()); 15 ds.setUsername(env.getProperty("spring.datasource.two.username").trim()); 16 ds.setPassword(env.getProperty("spring.datasource.two.password").trim()); 17 System.out.println("初始化数据源2结束。。。"); 18 return ds; 19 } 20 21 @Bean("sqlSessionFactory2") 22 public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource2") DataSource dataSource2) throws Exception { 23 SqlSessionFactoryBean sfb = new SqlSessionFactoryBean(); 24 sfb.setDataSource(dataSource2); 25 return sfb.getObject(); 26 } 27 28 @Bean("sqlSessionTemplate2") 29 public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory2") SqlSessionFactory sqlSessionFactory2){ 30 SqlSessionTemplate sft = new SqlSessionTemplate(sqlSessionFactory2); 31 return sft; 32 } 33 34 @Bean 35 public DataSourceTransactionManager transactionManager(@Qualifier("dataSource2") DataSource dataSource2){ 36 return new DataSourceTransactionManager(dataSource2); 37 } 38 39 }
注解@MapperScan的属性basePackages和sqlSessionTemplateRef将指定的dao层注入到指定的这个SqlSessionTemplate,这是接口和数据源绑定的最重要的一步
创建2个数据源对应的基础包下的接口
1 @Mapper 2 public interface CityOneMapper { 3 4 @Insert("insert into t_city(cityCode, cityName) values(#{cityCode},#{cityName})") 5 int insert(City city); 6 7 @Delete("delete from t_city where id = #{id}") 8 int delete(Integer id); 9 10 @Update("update t_city set cityName = #{cityName} where cityCode = #{cityCode}") 11 int update(City city); 12 13 @Select("select * from t_city where id = #{id}") 14 City selectById(@Param("id") Integer id); 15 16 }
1 @Mapper 2 public interface CityTwoMapper { 3 4 @Insert("insert into t_city(cityCode, cityName) values(#{cityCode},#{cityName})") 5 int insert(City city); 6 7 @Delete("delete from t_city where id = #{id}") 8 int delete(Integer id); 9 10 @Update("update t_city set cityName = #{cityName} where cityCode = #{cityCode}") 11 int update(City city); 12 13 @Select("select * from t_city where id = #{id}") 14 City selectById(@Param("id") Integer id); 15 16 }
分别创建2个service,实现接口方法
1 @Service("cityOneService") 2 public class CityOneService implements CityOneMapper { 3 4 @Autowired 5 private CityOneMapper cityOneMapper; 6 7 @Override 8 public int insert(City city) { 9 return cityOneMapper.insert(city); 10 } 11 12 @Override 13 public int delete(Integer id) { 14 return cityOneMapper.delete(id); 15 } 16 17 @Override 18 public int update(City city) { 19 return cityOneMapper.update(city); 20 } 21 22 @Override 23 public City selectById(Integer id) { 24 return cityOneMapper.selectById(id); 25 } 26 }
1 @Service("cityService") 2 public class CityTwoService implements CityTwoMapper { 3 4 @Autowired 5 private CityTwoMapper cityTwoMapper; 6 7 @Override 8 public int insert(City city) { 9 return cityTwoMapper.insert(city); 10 } 11 12 @Override 13 public int delete(Integer id) { 14 return cityTwoMapper.delete(id); 15 } 16 17 @Override 18 public int update(City city) { 19 return cityTwoMapper.update(city); 20 } 21 22 @Override 23 public City selectById(Integer id) { 24 return cityTwoMapper.selectById(id); 25 } 26 }
创建controller来测试
1 @RestController 2 public class DsTestController { 3 4 @Autowired 5 private CityOneService cityOneService; 6 7 @Autowired 8 private CityTwoService cityTwoService; 9 10 @GetMapping("/{id}") 11 public List<City> getCityById(@PathVariable("id") int id){ 12 System.out.println("查询数据,id="+id); 13 List<City> cityList = new ArrayList<City>(); 14 //查询数据库1的数据 15 City city1 = cityOneService.selectById(id); 16 cityList.add(city1); 17 //查询数据库2的数据 18 City city2 = cityTwoService.selectById(id); 19 cityList.add(city2); 20 return cityList; 21 } 22 23 }