Recently, I checked a spring data jpa problem. In fact, it can not be regarded as a framework level problem, but rather a configuration problem; Because spring data jpa has not been used before (more mybatis or self-developed orm components), it is expected to record the configuration of spring data jpa through this article.
Starting from the configuration source code, this chapter first intuitively understands the configuration of spring data jpa, and then describes the function of each configuration in combination with specific case s; At the same time, the configuration similar to spring.datasource. * is also briefly introduced. Don't be confused.
It is divided into three parts: Part I, Part II and comprehensive practice. This is the first part of jdbc configuration
jdbc configuration and jpa configuration
Before using it, we always think that the configuration of datasource, such as URL, class drive name, etc., is also under spring data jpa. It doesn't look like it. From another perspective, URL and class drive name belong to jdbc configurations, not orm configurations, so they should be taken for granted. Let's take a look at the configuration of JpaProperties, HibernateProperties and jdbc.
jdbc configuration
In spring's jdbc code module, jdbc configuration includes two:
- JdbcProperties: prefix is spring.jdbc*
- DataSourceProperties: prefix is spring.datasource*
JdbcProperties
The following are JdbcProperties. These configurations really look a little strange
@ConfigurationProperties(prefix = "spring.jdbc") public class JdbcProperties { private final Template template = new Template(); public static class Template { private int fetchSize = -1; private int maxRows = -1; @DurationUnit(ChronoUnit.SECONDS) private Duration queryTimeout;
Through code analysis, the configuration of JdbcProperties is currently only used in the JdbcTemplate; In other words, if you don't use JdbcTemplate in your project, spring.jdbc.template. * is useless. Throw a question: if you use JdbcTemplate and configure queryTimeout to - 1, will it time out? Is it 0? The answer is yes or no;
From the code point of view, these parameters of jdbc ultimately serve the underlying Statement. The execution process and principle are not introduced in detail here; Here is an article on troubleshooting the query timeout problem you saw earlier for your understanding
- fetchSize: when querying from the database and result set, pull the data with the specified fetchSize every time, and cycle until it is finished.
- maxRows: bottom layer Statement Object generates all ResultSet The maximum number of rows an object can contain is limited, and subsequent data will be discarded; This parameter should be set to avoid memory overload when the result set is very large.
DataSourceProperties
This is easy to understand, that is, data source configuration. Common URLs and driverclassname are configured here.
@ConfigurationProperties(prefix = "spring.datasource") public class DataSourceProperties implements BeanClassLoaderAware, InitializingBean { private ClassLoader classLoader; // datasource name. If embedded database is used, the default name is "testdb" private String name; // Do you want to create a datasource name randomly private boolean generateUniqueName = true; // The fully qualified name of the specific connection pool implementation class private Class<? extends DataSource> type; // The fully qualified name of the specific JDBC driver implementation class private String driverClassName; /** * JDBC URL of the database. */ private String url; /** * Login username of the database. */ private String username; /** * Login password of the database. */ private String password; /** * The JNDI location of the data source. If you use this, url and username, you don't need them. (not used) */ private String jndiName; // Initialization mode, such as whether to use available DDL and DML scripts private DataSourceInitializationMode initializationMode = DataSourceInitializationMode.EMBEDDED; /** * Platform used in DDL or DML scripts (such as schema-${platform}.sql or * data-${platform}.sql). */ private String platform = "all"; // Resource location of Schema (DDL) script private List<String> schema; private String schemaUsername; private String schemaPassword; // Data (DML) script resource location private List<String> data; private String dataUsername; private String dataPassword; // Whether to stop when an error occurs while initializing the database. private boolean continueOnError = false; // Statement separator in SQL initialization script. private String separator = ";"; // Coding format private Charset sqlScriptEncoding;
In general projects, the common configurations are as follows. It should be essential for any spring configuration data source:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.username=root spring.datasource.password= spring.datasource.url=jdbc:mysql://localhost:3306/glmapper?createDatabaseIfNotExist=true&xxx=xxx
Configure initialized DDL and DML
spring.datasource.schema=schema.sql spring.datasource.data=data.sql
If platform is specified in the configuration
spring.datasource.platform=test
Then you need to modify the DDL and DML of configuration initialization to
spring.datasource.schema=schema-test.sql spring.datasource.data=data-test.sql
Otherwise, an error will appear that the resource cannot be found, as follows:
Invalid value 'schema.sql' for configuration property 'spring.datasource.schema' (originating from 'class path resource [application.properties] - 7:26'). Validation failed for the following reason: The specified resource does not exist.
Summary
This article mainly introduces the jdbc configuration in the Spring Data JPA configuration section. If you have any questions, please correct them.