配置数据源
如何配置单数据源
搭建项目
主要相关的依赖是H2 和 JDBC, 如果是别的数据库就选别的,入Mysql
查看datasource是否配置成功
@SpringBootApplication @Slf4j public class DataSourceDemoApplication implements CommandLineRunner { @Autowired private DataSource dataSource; @Autowired private JdbcTemplate jdbcTemplate; public static void main(String[] args) { SpringApplication.run(DataSourceDemoApplication.class, args); } @Override public void run(String... args) throws Exception { showConnection(); showData(); } private void showConnection() throws SQLException { log.info(dataSource.toString()); Connection conn = dataSource.getConnection(); log.info(conn.toString()); conn.close(); } private void showData() { jdbcTemplate.queryForList("SELECT * FROM FOO") .forEach(row -> log.info(row.toString())); } }
启动程序控制台输出:
说明spingboot自动帮我们配置好了dataSource和jdbcTemplate.
当然也可以在配置文件中改变数据源的一些连接信息:
spring.datasource.url=jdbc:h2:mem:testdb spring.datasource.username=sa spring.datasource.password= spring.datasource.hikari.maximumPoolSize=5 spring.datasource.hikari.minimumIdle=5 spring.datasource.hikari.idleTimeout=600000 spring.datasource.hikari.connectionTimeout=30000 spring.datasource.hikari.maxLifetime=1800000
CommandLineRunner解释:在spirngboot启动完成后会执行它的run方法。
如果不是springboot项目,而是spring项目我们需要怎么配置呢?
我们需要手动地配置dataSource,transactionManager以及JdbcTemplate这些bean.
@Configuration @EnableTransactionManagement public class DataSourceDemo { @Autowired private DataSource dataSource; public static void main(String[] args) throws SQLException { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext*.xml"); showBeans(applicationContext); dataSourceDemo(applicationContext); } @Bean(destroyMethod = "close") public DataSource dataSource() throws Exception { Properties properties = new Properties(); properties.setProperty("driverClassName", "org.h2.Driver"); properties.setProperty("url", "jdbc:h2:mem:testdb"); properties.setProperty("username", "sa"); return BasicDataSourceFactory.createDataSource(properties); } @Bean public PlatformTransactionManager transactionManager() throws Exception { return new DataSourceTransactionManager(dataSource()); } private static void showBeans(ApplicationContext applicationContext) { System.out.println(Arrays.toString(applicationContext.getBeanDefinitionNames())); } private static void dataSourceDemo(ApplicationContext applicationContext) throws SQLException { DataSourceDemo demo = applicationContext.getBean("dataSourceDemo", DataSourceDemo.class); demo.showDataSource(); } public void showDataSource() throws SQLException { System.out.println(dataSource.toString()); Connection conn = dataSource.getConnection(); System.out.println(conn.toString()); conn.close(); } }
所以可以看得出springboot帮我们做了很多的配置bean的工作,如果我们没有配置,它会帮我们配置,如果我们自己配置了,那就不会自动配置了
如何配置多数据源
配置多数据源的注意事项
不同数据源的配置要分开
关注每次使⽤用的数据源
• 有多个DataSource时系统如何判断
• 对应的设施(事务、ORM等)如何选择DataSource
Spring Boot中的多数据源配置
⼿手⼯工配置两组 DataSource 及相关内容
与Spring Boot协同⼯工作(⼆选一)
• 配置@Primary类型的Bean
• 排除Spring Boot的⾃自动配置
• DataSourceAutoConfiguration
• DataSourceTransactionManagerAutoConfiguration
• JdbcTemplateAutoConfiguration
package geektime.spring.data.multidatasourcedemo; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.PlatformTransactionManager; import javax.annotation.Resource; import javax.sql.DataSource; @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, JdbcTemplateAutoConfiguration.class}) @Slf4j public class MultiDataSourceDemoApplication { public static void main(String[] args) { SpringApplication.run(MultiDataSourceDemoApplication.class, args); } @Bean @ConfigurationProperties("foo.datasource") public DataSourceProperties fooDataSourceProperties() { return new DataSourceProperties(); } @Bean public DataSource fooDataSource() { DataSourceProperties dataSourceProperties = fooDataSourceProperties(); log.info("foo datasource: {}", dataSourceProperties.getUrl()); return dataSourceProperties.initializeDataSourceBuilder().build(); } @Bean @Resource public PlatformTransactionManager fooTxManager(DataSource fooDataSource) { return new DataSourceTransactionManager(fooDataSource); } @Bean @ConfigurationProperties("bar.datasource") public DataSourceProperties barDataSourceProperties() { return new DataSourceProperties(); } @Bean public DataSource barDataSource() { DataSourceProperties dataSourceProperties = barDataSourceProperties(); log.info("bar datasource: {}", dataSourceProperties.getUrl()); return dataSourceProperties.initializeDataSourceBuilder().build(); } @Bean @Resource public PlatformTransactionManager barTxManager(DataSource barDataSource) { return new DataSourceTransactionManager(barDataSource); } }