SpringBoot快速配置數(shù)據(jù)源的方法
SpringBoot如何快速配置數(shù)據(jù)源;有如下兩種方式:
通過spring-boot-starter-jdbc快速配置數(shù)據(jù)源 自定義數(shù)據(jù)源DataSource首先我們需要明確數(shù)據(jù)源DataSource有什么作用:
通過DataSource可以獲取數(shù)據(jù)庫(kù)連接Connection 通過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)建和銷毀連接的代價(jià)是很大的,使用數(shù)據(jù)庫(kù)連接池可以很好的復(fù)用連接,節(jié)省開銷,方便管理,簡(jiǎn)化開發(fā)。
可能有些場(chǎng)景我們不想使用SpringBoot JDBC默認(rèn)的數(shù)據(jù)源,我需要引入數(shù)據(jù)庫(kù)連接池,然后自定義數(shù)據(jù)源,指定數(shù)據(jù)源類型。
下面以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à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類型為BasicDataSource類型的。并且BasicDataSource來源于之前配置的dbcp2依賴的jar包中。
五、調(diào)用驗(yàn)證
下面我們使用junit來驗(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. ASP.NET MVC實(shí)現(xiàn)下拉框多選2. JSP中param動(dòng)作的實(shí)例詳解3. Jsp servlet驗(yàn)證碼工具類分享4. .NET中的MassTransit分布式應(yīng)用框架詳解5. 解決request.getParameter取值后的if判斷為NULL的問題6. .NET Framework各版本(.NET2.0 3.0 3.5 4.0)區(qū)別7. .Net反向代理組件Yarp用法詳解8. ASP.NET MVC增加一條記錄同時(shí)添加N條集合屬性所對(duì)應(yīng)的個(gè)體9. 刪除docker里建立容器的操作方法10. 詳解如何使用Net將HTML簡(jiǎn)歷導(dǎo)出為PDF格式
