本文记录在springboot中配置多数据源,使用mybatis框架。
创建项目并添加依赖
12 6mysql 3mysql-connector-java 4runtime 57 org.mybatis.spring.boot 8mybatis-spring-boot-starter 91.3.2 10
创建与数据库表对应的实体类对象
1 public class City { 2 3 private Long id; 4 private String cityCode; 5 private String cityName; 6 7 //省略get和set方法 8 9 @Override10 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=12345610 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 @Primary11 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 @Primary24 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 @Primary32 public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory1") SqlSessionFactory sqlSessionFactory){33 SqlSessionTemplate sft = new SqlSessionTemplate(sqlSessionFactory);34 return sft;35 }36 37 @Bean38 @Primary39 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 @Bean35 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 @Override13 public int delete(Integer id) {14 return cityOneMapper.delete(id);15 }16 17 @Override18 public int update(City city) {19 return cityOneMapper.update(city);20 }21 22 @Override23 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 @Override13 public int delete(Integer id) {14 return cityTwoMapper.delete(id);15 }16 17 @Override18 public int update(City city) {19 return cityTwoMapper.update(city);20 }21 22 @Override23 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 ListgetCityById(@PathVariable("id") int id){12 System.out.println("查询数据,id="+id);13 List cityList = new ArrayList ();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 }