博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springboot入门_多数据源
阅读量:6533 次
发布时间:2019-06-24

本文共 7564 字,大约阅读时间需要 25 分钟。

本文记录在springboot中配置多数据源,使用mybatis框架。

创建项目并添加依赖

1 
2
mysql
3
mysql-connector-java
4
runtime
5
6
7
org.mybatis.spring.boot
8
mybatis-spring-boot-starter
9
1.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 List
getCityById(@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 }

转载于:https://www.cnblogs.com/wlzq/p/9804798.html

你可能感兴趣的文章
SQLSERVER是怎麽通过索引和统计信息来找到目标数据的(第一篇)
查看>>
LocalAlloc,VirtualAlloc,malloc,new的异同
查看>>
回调函数
查看>>
win7 x64 jdk1.7.0_51
查看>>
这些开源项目,你都知道吗?(持续更新中...)[原创]
查看>>
linux中利用iptables+geoip过滤指定IP
查看>>
在myeclipse中写sql语句的细节问题
查看>>
使用ShellExecute打开目标文件所在文件夹并选中目标文件
查看>>
HDU 4614 Vases and Flowers (2013多校2 1004 线段树)
查看>>
Minix中的字符判定ctype.c
查看>>
91平台iOS接入demo
查看>>
五个优秀的硬盘检测工具
查看>>
用js实现table内容从下到上连续滚动
查看>>
基于ffmpeg的流媒体服务器
查看>>
项目积累——Blockingqueue,ConcurrentLinkedQueue,Executors
查看>>
JVM学习笔记(一)------基本结构
查看>>
活动目录之备份与恢复
查看>>
删除 Eclipse 的 configuration 目录
查看>>
MOXA的智能通信产品也大力支持WinCE.net了
查看>>
ActiveX开发知多少?
查看>>