Explain the use of jdbctemplate in spring boot
This article describes how spring boot works with the jdbctemplate.
Spring's database operations are deeply encapsulated in JDBC. Using spring's injection function, you can register the datasource into the jdbctemplate. Jdbctemplate provides more abstract encapsulation based on JDBC API and transaction management capability based on method annotation. By using the spring boot auto configuration function and replacing us to automatically configure beans
Data source configuration
In maven, we need to add the spring boot starter JDBC module
Through this module, we have done the following things for us
tomcat-jdbc-{version}. Jar automatically configures the datasource for us
If you do not define any datasource, springboot will automatically configure an in memory database resource setting. If you do not set any beans, springboot will automatically register it to initialize the database. If we define a schema in the classpath SQL and data SQL files, springboot will use these files to automatically initialize the database (but you must select the Library) in addition to loading schema SQL and data In addition to SQL, springboot will also load schema - ${platform} SQL and data - ${platform} SQL, if it exists under your classpath.
Embedded database support
Embedded database is usually used in development and test environment, not recommended for production environment. Spring boot provides automatically configured embedded databases such as H2, hsql and Derby. You can use them without providing any connection configuration.
For example, we can use POM The following configuration is introduced into XML to use hsql
Connect production data source configuration
Taking MySQL database as an example, first introduce the dependency package of MySQL connection in POM Add in XML:
In Src / main / resources / application Configure data source information in properties
Connect JNDI data source configuration
When you deploy an application on an application server and want the data source to be managed by the application server, you can use the following configuration method to introduce JNDI data sources.
Custom data source configuration
If you don't want to use the default configuration data source, for example, if you want to use Alibaba's data pool to manage the data source, you can also configure it yourself
Exclude the default configuration datasource of Tomcat JDBC first
Define your own data resources. Alibaba's data pool management is used here. You can also use basic datasource
package com. example;
import javax. sql. DataSource;
You can also use other:
Code example
Create a solid object
/src/main/java/com/example/domain/User. java
Create persistence layer
With the above data source configuration, we can inject the jdbctemplate into the data access component and interact with the database.
/src/main/java/com/example/repositories/UserRepository. java
unit testing
You may have noticed that most of the time, we do these configuration things in the application.
Create unit tests to test our persistence layer methods
/src/test/java/SpringBootJdbcDemoApplicationTests. java
The above is what Xiaobian introduced to you about the use of jdbctemplate in spring boot. I hope it will be helpful to you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!