SpringBoot快速配置數(shù)據(jù)源的方法
SpringBoot如何快速配置數(shù)據(jù)源;有如下兩種方式:
通過(guò)spring-boot-starter-jdbc快速配置數(shù)據(jù)源 自定義數(shù)據(jù)源DataSource首先我們需要明確數(shù)據(jù)源DataSource有什么作用:
通過(guò)DataSource可以獲取數(shù)據(jù)庫(kù)連接Connection 通過(guò)DataSource創(chuàng)建JdbcTemplate操作數(shù)據(jù)庫(kù)實(shí)際項(xiàng)目中,我們?cè)谂渲脭?shù)據(jù)源的時(shí)候會(huì)指定數(shù)據(jù)庫(kù)連接池,比如流行的Hikari(spring默認(rèn)的數(shù)據(jù)庫(kù)連接池)、C3p0、Dbcp2以及阿里巴巴的Druid。
一、使用數(shù)據(jù)庫(kù)連接池
應(yīng)用在操作數(shù)據(jù)庫(kù)的時(shí)候,直接從數(shù)據(jù)庫(kù)連接池獲取連接,而不需要每次創(chuàng)建新的連接。
至于數(shù)據(jù)庫(kù)連接池的好處,總結(jié)就是: 應(yīng)用創(chuàng)建和銷(xiāo)毀連接的代價(jià)是很大的,使用數(shù)據(jù)庫(kù)連接池可以很好的復(fù)用連接,節(jié)省開(kāi)銷(xiāo),方便管理,簡(jiǎn)化開(kāi)發(fā)。
可能有些場(chǎng)景我們不想使用SpringBoot JDBC默認(rèn)的數(shù)據(jù)源,我需要引入數(shù)據(jù)庫(kù)連接池,然后自定義數(shù)據(jù)源,指定數(shù)據(jù)源類(lèi)型。
下面以Dbcp2數(shù)據(jù)庫(kù)連接池配置數(shù)據(jù)源為例。
二、配置依賴
引入dbcp2的數(shù)據(jù)庫(kù)連接池已經(jīng)相關(guān)依賴。
<!-- dbcp2數(shù)據(jù)庫(kù)連接池 --><dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-dbcp2</artifactId> <version>2.7.0</version></dependency><!--數(shù)據(jù)庫(kù)驅(qū)動(dòng)--><dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.18</version></dependency><!-- 提供操作數(shù)據(jù)庫(kù)的標(biāo)準(zhǔn)口徑 --><dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.2.2.RELEASE</version> <scope>compile</scope></dependency>
三、編寫(xiě)配置項(xiàng)
在application.properties文件中配置數(shù)據(jù)庫(kù)連接屬性。
customize.datasource.url=jdbc:mysql://localhost:3306/blue?serverTimezone=UTCcustomize.datasource.username=rootcustomize.datasource.password=wan4380797customize.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
四、自定義DataSource
import org.apache.commons.dbcp2.BasicDataSource;@Configurationpublic class Dbcp2DataSource { @Bean('myDbcp2DataSource') @ConfigurationProperties(prefix = 'customize.datasource') public DataSource getDataSource(){ return DataSourceBuilder.create().type(BasicDataSource.class).build(); }}
這邊我們可以看到我們創(chuàng)建的DataSource類(lèi)型為BasicDataSource類(lèi)型的。并且BasicDataSource來(lái)源于之前配置的dbcp2依賴的jar包中。
五、調(diào)用驗(yàn)證
下面我們使用junit來(lái)驗(yàn)證以下數(shù)據(jù)源配置的正確與否:
@SpringBootTest@RunWith(SpringRunner.class)public class JdbcCustomizeDatasourceApplicationTests { @Autowired @Qualifier('myDbcp2DataSource') private DataSource dataSource; @Test public void springJdbcTemplateTest(){ try{ JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); String queryStr = 'select * from student'; List<Student> resultList = new ArrayList<>(); jdbcTemplate.query(queryStr, (ResultSet resultSet)->{Student student = new Student();student.setId(resultSet.getString('id'));student.setStudentId(resultSet.getString('student_id'));student.setStudentName(resultSet.getString('student_name'));student.setAge(resultSet.getInt('age'));resultList.add(student); }); resultList.forEach((Student student) -> System.out.println(student)); }catch (Exception exception){ exception.printStackTrace(); } }}
以上就是SpringBoot快速配置數(shù)據(jù)源的方法的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot 配置數(shù)據(jù)源的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. jsp實(shí)現(xiàn)局部刷新頁(yè)面、異步加載頁(yè)面的方法2. ASP基礎(chǔ)入門(mén)第二篇(ASP基礎(chǔ)知識(shí))3. ASP中Server.HTMLEncode用法(附自定義函數(shù))4. ASP和PHP文件操作速度的對(duì)比5. Spring依賴注入的三種方式實(shí)例詳解6. adodb.recordset.open(rs.open)方法參數(shù)詳解7. JavaServlet的文件上傳和下載實(shí)現(xiàn)方法8. 怎樣打開(kāi)XML文件?xml文件如何打開(kāi)?9. jsp實(shí)現(xiàn)簡(jiǎn)單用戶7天內(nèi)免登錄10. asp文件如何打開(kāi)
