Some Spring Boot tips, little knowledge
Initialization data
We are doing tests often need to initialize the time to import some data, how to deal with it? There will be two options, one is to use Jpa, another is the Spring JDBC. The difference between each two methods described in detail below.
Use Jpa
In use spring boot jpa
to set the case of the spring.jpa.hibernate.ddl-auto
property is set to create
or create-drop
when the default when Spring Boot started, it scans your classpath (project resources directory usually) whether there is import.sql
, if you have the opportunity to execute import.sql
the script.
Use Spring JDBC
Use Spring JDBC need to add the following configuration in the configuration file
spring: datasource: schema: database/data.sql sql-script-encoding: utf-8 jpa: hibernate: ddl-auto: none
- schema: Set the script path
- sql-script-encoding: set the encoding of the script
Spring Boot project start automatically when executing the script.
Explain four values ddl-auto
- create: hibernate is loaded each time will delete the last generation of the table, and then re-generate a new table based on your model type, even if the two do not have any such change should be executed, and this is resulting in a database table data loss is an important the reason.
- create-drop: loading each model class-table according to hibernate, but sessionFactory a closed table is automatically deleted.
- update: most commonly used properties, when you first load hibernate automatically set up according to the model class structure of the table (the premise is to set up a good database), automatically updates the table structure based on model class is loaded hibernate later, but even if the table structure changes there are still rows in a table does not delete the previous row. Note that when deployed to the server, the table structure will not be immediately established, it is to wait after the first application up and running before.
- validate: Each time hibernate loaded, verified create a database table structure, and the only table in the database are compared, it does not create a new table, but will insert a new value.
5, none: do nothing.
difference
The first way to start when Jpa will automatically create a table, import.sql only responsible for creating initialization data form. The second way does not start when you create a table, you need to determine whether there is a table, re-initialization script step in the initialization script.
In production, these two models are recommended with caution!
Html tag is not set check Thymeleaf
The default configuration, Thymeleaf for the content .html very strict, for example <meta charset="UTF-8" />
, if a small closed symbols /
, and the error will go to the wrong page. For example, you can also use this library Vue.js, then there is <div v-cloak></div>
this html code will be deemed not to meet the requirements Thymeleaf throw an error.
By setting Thymeleaf template can solve this problem, the following is the specific configuration:
spring.thymeleaf.cache=false spring.thymeleaf.mode=LEGACYHTML5
LEGACYHTML5 need to match an additional library NekoHTML available, used in the project build tool is Maven dependencies, add the following to complete:
<dependency> <groupId>net.sourceforge.nekohtml</groupId> <artifactId>nekohtml</artifactId> <version>1.9.22</version> </dependency>
Article content has been upgraded to Spring Boot 2.x
reference:
- thymeleaf template for HTML5 tags do not have analytical solutions terminator error
- howto-database-initialization
This article comes from: https://www.cnblogs.com/ityouknow/p/7089170.html
Some Spring Boot tips, little knowledge
Initialization data
We are doing tests often need to initialize the time to import some data, how to deal with it? There will be two options, one is to use Jpa, another is the Spring JDBC. The difference between each two methods described in detail below.
Use Jpa
In use spring boot jpa
to set the case of the spring.jpa.hibernate.ddl-auto
property is set to create
or create-drop
when the default when Spring Boot started, it scans your classpath (project resources directory usually) whether there is import.sql
, if you have the opportunity to execute import.sql
the script.
Use Spring JDBC
Use Spring JDBC need to add the following configuration in the configuration file
spring: datasource: schema: database/data.sql sql-script-encoding: utf-8 jpa: hibernate: ddl-auto: none
- schema: Set the script path
- sql-script-encoding: set the encoding of the script
Spring Boot project start automatically when executing the script.
Explain four values ddl-auto
- create: hibernate is loaded each time will delete the last generation of the table, and then re-generate a new table based on your model type, even if the two do not have any such change should be executed, and this is resulting in a database table data loss is an important the reason.
- create-drop: loading each model class-table according to hibernate, but sessionFactory a closed table is automatically deleted.
- update: most commonly used properties, when you first load hibernate automatically set up according to the model class structure of the table (the premise is to set up a good database), automatically updates the table structure based on model class is loaded hibernate later, but even if the table structure changes there are still rows in a table does not delete the previous row. Note that when deployed to the server, the table structure will not be immediately established, it is to wait after the first application up and running before.
- validate: Each time hibernate loaded, verified create a database table structure, and the only table in the database are compared, it does not create a new table, but will insert a new value.
5, none: do nothing.
difference
The first way to start when Jpa will automatically create a table, import.sql only responsible for creating initialization data form. The second way does not start when you create a table, you need to determine whether there is a table, re-initialization script step in the initialization script.
In production, these two models are recommended with caution!
Html tag is not set check Thymeleaf
The default configuration, Thymeleaf for the content .html very strict, for example <meta charset="UTF-8" />
, if a small closed symbols /
, and the error will go to the wrong page. For example, you can also use this library Vue.js, then there is <div v-cloak></div>
this html code will be deemed not to meet the requirements Thymeleaf throw an error.
By setting Thymeleaf template can solve this problem, the following is the specific configuration:
spring.thymeleaf.cache=false spring.thymeleaf.mode=LEGACYHTML5
LEGACYHTML5 need to match an additional library NekoHTML available, used in the project build tool is Maven dependencies, add the following to complete:
<dependency> <groupId>net.sourceforge.nekohtml</groupId> <artifactId>nekohtml</artifactId> <version>1.9.22</version> </dependency>
Article content has been upgraded to Spring Boot 2.x
reference:
- thymeleaf template for HTML5 tags do not have analytical solutions terminator error
- howto-database-initialization
This article comes from: https://www.cnblogs.com/ityouknow/p/7089170.html