SpringBoot - persistence technology

a brief introdction

The data layer persistence technology we used before used MyBatis or MyBatis-plus, which are actually the same. Before using, we need to import the corresponding coordinates, and then configure MyBatis-specific configurations, such as the Mapper interface, or XML configuration files. In addition to MyBatis, SpringBoot also provides a set of default persistence solutions. In fact, we used to I have already come into contact with it when I was in Spring, which is JdbcTemplate .

Instructions

Regarding the JdbcTemplate method, we have actually demonstrated it before. Here we will use it again in SpringBoot:

Before starting to use, we need to import the jdbc package first, so that we can use the JdbcTemplate belonging to the jdbc technology. In addition, we can also look at the contents of this package: 

In this starter, in addition to the previously imported SpringBoot and the SpringJdbc we want to use, there is also a Hikari, which is the default data source we just saw before, that is to say, the data source used at the bottom of the JdbcTemplate is also Hikari technology.

After importing the corresponding starter, we do not need to do other configuration, we can use it directly in the test class:

 Here we first use the automatic injection method to obtain the JdbcTemplate object, because we have imported the coordinates, so a corresponding object will be automatically generated in the container, and then we can use the methods in this object as before: 

Simply put, JdbcTemplate is a mode in which you need to write SQL statements yourself, and after the calling method is executed, you need to encapsulate the data in the dataset into objects. This model is definitely not as convenient as the previous MyBais, but this method is more flexible and more compatible, so it still has some audiences. 

In fact, SpringBoot's default persistence solution comes from its early Spring.

About the configuration of JdbcTemplate

We did not configure it when using it before because we already have some sufficient configurations. If we want to make special configurations, it is also possible:

There are three configurations for JdbcTemplate. From top to bottom, they are [Number of Cache Lines], [Maximum Number of Lines in a Single Query], and [Maximum Query Time].

The introduction of SpringBoot's built-in persistence solution is over. As far as technology is concerned, choosing the right technology in the right scene, and the emergence of multiple technical supports are just to facilitate our technology selection in various environments. 

 

Guess you like

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