在springboot中使用jdbcTemplate(2)-多数据源
最近项目中开发后端接口需要从多个数据源获取数据,本来想用mybatis-plus结果失败了。
又重新配置了JdbcTemplate竟然成功了,感觉还是得用成熟得技术啊。
不多说,上代码,首先application.propterties配置多个数据源信息
一、配置文件
spring.datasource.dynamic.primary=master #master库 spring.datasource.dynamic.datasource.master.username=user spring.datasource.dynamic.datasource.master.password=pswd spring.datasource.dynamic.datasource.master.driver-class-name=com.mysql.jdbc.Driver spring.datasource.dynamic.datasource.master.url=jdbc:mysql://ip:3306/db?useUnicode=true&characterEncoding=utf8&useSSL=false spring.datasource.dynamic.datasource.master.hikari.connection-timeout=60000 spring.datasource.dynamic.datasource.master.hikari.minIdle=10 spring.datasource.dynamic.datasource.master.hikari.maxPoolSize=20 spring.datasource.dynamic.datasource.master.hikari.isAutoCommit=true spring.datasource.dynamic.datasource.master.hikari.idle-timeout=600000 spring.datasource.dynamic.datasource.master.hikari.max-lifetime=28740000 spring.datasource.dynamic.datasource.master.hikari.connection-test-query=SELECT 1 #slave库 spring.datasource.dynamic.datasource.slave1.username=user spring.datasource.dynamic.datasource.slave1.password=pswd spring.datasource.dynamic.datasource.slave1.driver-class-name=com.mysql.jdbc.Driver spring.datasource.dynamic.datasource.slave1.url=jdbc:mysql://ip:3306/db2?useUnicode=true&characterEncoding=utf8&useSSL=false spring.datasource.dynamic.datasource.slave1.hikari.connection-timeout=60000 spring.datasource.dynamic.datasource.slave1.hikari.minIdle=10 spring.datasource.dynamic.datasource.slave1.hikari.maxPoolSize=20 spring.datasource.dynamic.datasource.slave1.hikari.isAutoCommit=true spring.datasource.dynamic.datasource.slave1.hikari.idle-timeout=600000 spring.datasource.dynamic.datasource.slave1.hikari.max-lifetime=28740000 spring.datasource.dynamic.datasource.slave1.hikari.connection-test-query=SELECT 1
二、maven依赖
项目依赖中用到了druid,这里把部分依赖包也发一下
<!--druid--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.9</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
三、数据源配置类
@Configuration public class DataSourceConfig { private static final Logger logger = LoggerFactory.getLogger(DataSourceConfig.class); @Primary @Bean(name = "masterDataSource") @ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.master") public DataSource masterDataSource() { return DruidDataSourceBuilder.create().build(); } @Bean(name = "slave1DataSource") @ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.slave1") public DataSource slave1DataSource() { return DruidDataSourceBuilder.create().build(); } @Bean(name = "masterJdbcTemplate") public JdbcTemplate primaryJdbcTemplate(@Qualifier("masterDataSource") DataSource dataSource) { return new JdbcTemplate(dataSource); } @Bean(name = "slave1JdbcTemplate") public JdbcTemplate secondaryJdbcTemplate(@Qualifier("slave1DataSource") DataSource dataSource) { return new JdbcTemplate(dataSource); } }
四、使用
@Service public class PatrolServiceImpl implements PatrolService { @Autowired @Qualifier("masterJdbcTemplate") private JdbcTemplate masterJdbcTemplate; @Autowired @Qualifier("slave1JdbcTemplate") private JdbcTemplate slave1JdbcTemplate; @Override public List<Patrol1> selectAll() { List<Patrol1> query = masterJdbcTemplate.query("select * from table1 limit 10", new BeanPropertyRowMapper<>(Patrol1.class)); return query; } @Override public Integer selectCount() { Integer count = masterJdbcTemplate.queryForObject("select count(id) from patrol_task ", Integer.class); return count; } @Override public List selectByCondition() { return slave1JdbcTemplate.queryForList("select * from customer limit 10"); } }
这就大功告成了