SpringBoot - built-in data source

a brief introdction:

When we introduced SpringBoot's data layer solution before, we mentioned that the data layer is composed of data sources, persistence technology and databases. We have been using the solution of Druid+MyBatis+MySQL before. We have previously introduced how to integrate these three solutions and their basic usage. We have also said before that SpringBoot is an out-of-the-box framework. These things are imported when we import start later. This time we are going to talk about some of the data layer solutions that come with SpringBoot.

Built-in data sources:

As we mentioned when we first used the web layer, SpringBoot integrates a TomCat kernel into the framework by default, allowing us to use the Tomcat server directly with very little configuration or even without writing configuration. . The same is true for data sources. SpringBoot has a built-in lightweight data source Hikari.

Environment introduction:

The environment is a basic SpringBoot environment, but only the basic Dao layer for reading the database is retained for demonstration:

In the test class, all the data in the database is read and printed on the console:

The final result is shown above.

But this time our focus is not on the results, but on the data source he used: As you  can see here, the generated data source is Druid because we set it to use the Druid data source when we made the configuration file: 

This data source was configured by ourselves later. Before that, we imported the coordinates of Druid and configured the data source type. If we didn't use Druid, would there still be a data source here? 

We delete the configuration that sets the data source type, and then re-execute the test case:  This time you can see that the previous DruidDataSource is no longer there, but there is still a DataSource called HikarDataSource. This is what we are mainly talking about today. SpringBoot's default built-in data source: Hikar

Data source objects provided by SpringBoot by default

SpringBoot provides three data source objects by default:

  • Hikar (default built-in data source object)
  • Tomcat's DataSource (when the default Hikar is not available and the current environment is a Web environment, the data source object configured by Tomcat will be used)
  • DBCP (DBCP data source object will be used only when neither of the current two is available)

Hikar is the fastest lightweight data source. In addition to Hikar, there are two other data sources as backup options. These three do not require additional import of other coordinates. You can configure and switch data sources directly in the configuration file:

We mentioned DBCP2 before when we were working on native JavaWeb. You can find relevant content in previous articles. In fact, the use of data sources is similar.

The configurations of these three data sources, except for some unique configurations, are basically the same. If you want to switch to other data source solutions, you can find the complete configuration of the corresponding solution. 

This chapter mainly explains the various data source solutions built into SpringBoot and enriches our options when choosing data source solutions.

Guess you like

Origin blog.csdn.net/hssjsh/article/details/131906406