类 ResourceDatabasePopulator
- java.lang.Object
- org.springframework.jdbc.datasource.init.ResourceDatabasePopulator
- 所有已实现的接口:
DatabasePopulator
public class ResourceDatabasePopulator extends Object implements DatabasePopulator
Populates, initializes, or cleans up a database using SQL scripts defined in external resources.- Call
addScript(org.springframework.core.io.Resource)to add a single SQL script location. - Call
addScripts(org.springframework.core.io.Resource...)to add multiple SQL script locations. - Consult the setter methods in this class for further configuration options.
- Call
populate(java.sql.Connection)orexecute(javax.sql.DataSource)to initialize or clean up the database using the configured scripts.
- 从以下版本开始:
- 3.0
- 作者:
- Keith Donald, Dave Syer, Juergen Hoeller, Chris Beams, Oliver Gierke, Sam Brannen, Chris Baldwin, Phillip Webb
- 另请参阅:
DatabasePopulatorUtils,ScriptUtils
构造器概要
构造器 构造器 说明 ResourceDatabasePopulator()Construct a newResourceDatabasePopulatorwith default settings.ResourceDatabasePopulator(boolean continueOnError, boolean ignoreFailedDrops, String sqlScriptEncoding, Resource... scripts)Construct a newResourceDatabasePopulatorwith the supplied values.ResourceDatabasePopulator(Resource... scripts)Construct a newResourceDatabasePopulatorwith default settings for the supplied scripts.
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 voidaddScript(Resource script)Add a script to execute to initialize or clean up the database.voidaddScripts(Resource... scripts)Add multiple scripts to execute to initialize or clean up the database.voidexecute(DataSource dataSource)Execute thisResourceDatabasePopulatoragainst the givenDataSource.voidpopulate(Connection connection)Populate, initialize, or clean up the database using the provided JDBC connection.voidsetBlockCommentEndDelimiter(String blockCommentEndDelimiter)Set the end delimiter that identifies block comments within the SQL scripts.voidsetBlockCommentStartDelimiter(String blockCommentStartDelimiter)Set the start delimiter that identifies block comments within the SQL scripts.voidsetCommentPrefix(String commentPrefix)Set the prefix that identifies single-line comments within the SQL scripts.voidsetCommentPrefixes(String... commentPrefixes)Set the prefixes that identify single-line comments within the SQL scripts.voidsetContinueOnError(boolean continueOnError)Flag to indicate that all failures in SQL should be logged but not cause a failure.voidsetIgnoreFailedDrops(boolean ignoreFailedDrops)Flag to indicate that a failed SQLDROPstatement can be ignored.voidsetScripts(Resource... scripts)Set the scripts to execute to initialize or clean up the database, replacing any previously added scripts.voidsetSeparator(String separator)Specify the statement separator, if a custom one.voidsetSqlScriptEncoding(String sqlScriptEncoding)Specify the encoding for the configured SQL scripts, if different from the platform encoding.
构造器详细资料
ResourceDatabasePopulator
public ResourceDatabasePopulator()
Construct a newResourceDatabasePopulatorwith default settings.- 从以下版本开始:
- 4.0.3
ResourceDatabasePopulator
public ResourceDatabasePopulator(Resource... scripts)
Construct a newResourceDatabasePopulatorwith default settings for the supplied scripts.- 参数:
scripts- the scripts to execute to initialize or clean up the database (nevernull)- 从以下版本开始:
- 4.0.3
ResourceDatabasePopulator
public ResourceDatabasePopulator(boolean continueOnError, boolean ignoreFailedDrops, @Nullable String sqlScriptEncoding, Resource... scripts)
Construct a newResourceDatabasePopulatorwith the supplied values.- 参数:
continueOnError- flag to indicate that all failures in SQL should be logged but not cause a failureignoreFailedDrops- flag to indicate that a failed SQLDROPstatement can be ignoredsqlScriptEncoding- the encoding for the supplied SQL scripts (may benullor empty to indicate platform encoding)scripts- the scripts to execute to initialize or clean up the database (nevernull)- 从以下版本开始:
- 4.0.3
方法详细资料
addScript
public void addScript(Resource script)
Add a script to execute to initialize or clean up the database.- 参数:
script- the path to an SQL script (nevernull)
addScripts
public void addScripts(Resource... scripts)
Add multiple scripts to execute to initialize or clean up the database.- 参数:
scripts- the scripts to execute (nevernull)
setScripts
public void setScripts(Resource... scripts)
Set the scripts to execute to initialize or clean up the database, replacing any previously added scripts.- 参数:
scripts- the scripts to execute (nevernull)
setSqlScriptEncoding
public void setSqlScriptEncoding(@Nullable String sqlScriptEncoding)
Specify the encoding for the configured SQL scripts, if different from the platform encoding.- 参数:
sqlScriptEncoding- the encoding used in scripts (may benullor empty to indicate platform encoding)- 另请参阅:
addScript(Resource)
setSeparator
public void setSeparator(String separator)
Specify the statement separator, if a custom one.Defaults to
";"if not specified and falls back to"\n"as a last resort; may be set toScriptUtils.EOF_STATEMENT_SEPARATORto signal that each script contains a single statement without a separator.- 参数:
separator- the script statement separator
setCommentPrefix
public void setCommentPrefix(String commentPrefix)
Set the prefix that identifies single-line comments within the SQL scripts.Defaults to
"--".- 参数:
commentPrefix- the prefix for single-line comments- 另请参阅:
setCommentPrefixes(String...)
setCommentPrefixes
public void setCommentPrefixes(String... commentPrefixes)
Set the prefixes that identify single-line comments within the SQL scripts.Defaults to
["--"].- 参数:
commentPrefixes- the prefixes for single-line comments- 从以下版本开始:
- 5.2
setBlockCommentStartDelimiter
public void setBlockCommentStartDelimiter(String blockCommentStartDelimiter)
Set the start delimiter that identifies block comments within the SQL scripts.Defaults to
"/*".- 参数:
blockCommentStartDelimiter- the start delimiter for block comments (nevernullor empty)- 从以下版本开始:
- 4.0.3
- 另请参阅:
setBlockCommentEndDelimiter(java.lang.String)
setBlockCommentEndDelimiter
public void setBlockCommentEndDelimiter(String blockCommentEndDelimiter)
Set the end delimiter that identifies block comments within the SQL scripts.Defaults to
"*/".- 参数:
blockCommentEndDelimiter- the end delimiter for block comments (nevernullor empty)- 从以下版本开始:
- 4.0.3
- 另请参阅:
setBlockCommentStartDelimiter(java.lang.String)
setContinueOnError
public void setContinueOnError(boolean continueOnError)
Flag to indicate that all failures in SQL should be logged but not cause a failure.Defaults to
false.- 参数:
continueOnError-trueif script execution should continue on error
setIgnoreFailedDrops
public void setIgnoreFailedDrops(boolean ignoreFailedDrops)
Flag to indicate that a failed SQLDROPstatement can be ignored.This is useful for a non-embedded database whose SQL dialect does not support an
IF EXISTSclause in aDROPstatement.The default is
falseso that if the populator runs accidentally, it will fail fast if a script starts with aDROPstatement.- 参数:
ignoreFailedDrops-trueif failed drop statements should be ignored
populate
public void populate(Connection connection) throws ScriptException
Populate, initialize, or clean up the database using the provided JDBC connection.Concrete implementations may throw an
SQLExceptionif an error is encountered but are strongly encouraged to throw a specificScriptExceptioninstead. For example, Spring'sResourceDatabasePopulatorandDatabasePopulatorUtilswrap allSQLExceptionsinScriptExceptions.- 指定者:
populate在接口中DatabasePopulator- 参数:
connection- the JDBC connection to use to populate the db; already configured and ready to use; nevernull- 抛出:
ScriptException- in all other error cases- 另请参阅:
execute(DataSource)
execute
public void execute(DataSource dataSource) throws ScriptException
Execute thisResourceDatabasePopulatoragainst the givenDataSource.Delegates to
DatabasePopulatorUtils.execute(org.springframework.jdbc.datasource.init.DatabasePopulator, javax.sql.DataSource).- 参数:
dataSource- theDataSourceto execute against (nevernull)- 抛出:
ScriptException- if an error occurs- 从以下版本开始:
- 4.1
- 另请参阅:
populate(Connection)