类 EmbeddedDatabaseBuilder


  • public class EmbeddedDatabaseBuilder
    extends Object
    A builder that provides a convenient API for constructing an embedded database.

    Usage Example

     EmbeddedDatabase db = new EmbeddedDatabaseBuilder()
         .generateUniqueName(true)
         .setType(H2)
         .setScriptEncoding("UTF-8")
         .ignoreFailedDrops(true)
         .addScript("schema.sql")
         .addScripts("user_data.sql", "country_data.sql")
         .build();
    
     // perform actions against the db (EmbeddedDatabase extends javax.sql.DataSource)
    
     db.shutdown();
     
    从以下版本开始:
    3.0
    作者:
    Keith Donald, Juergen Hoeller, Dave Syer, Sam Brannen
    另请参阅:
    ScriptUtils, ResourceDatabasePopulator, DatabasePopulatorUtils
    • 方法详细资料

      • generateUniqueName

        public EmbeddedDatabaseBuilder generateUniqueName​(boolean flag)
        Specify whether a unique ID should be generated and used as the database name.

        If the configuration for this builder is reused across multiple application contexts within a single JVM, this flag should be enabled (i.e., set to true) in order to ensure that each application context gets its own embedded database.

        Enabling this flag overrides any explicit name set via setName(java.lang.String).

        参数:
        flag - true if a unique database name should be generated
        返回:
        this, to facilitate method chaining
        从以下版本开始:
        4.2
        另请参阅:
        setName(java.lang.String)
      • setType

        public EmbeddedDatabaseBuilder setType​(EmbeddedDatabaseType databaseType)
        Set the type of embedded database.

        Defaults to HSQL if not called.

        参数:
        databaseType - the type of embedded database to build
        返回:
        this, to facilitate method chaining
      • setDataSourceFactory

        public EmbeddedDatabaseBuilder setDataSourceFactory​(DataSourceFactory dataSourceFactory)
        Set the factory to use to create the DataSource instance that connects to the embedded database.

        Defaults to SimpleDriverDataSourceFactory but can be overridden, for example to introduce connection pooling.

        返回:
        this, to facilitate method chaining
        从以下版本开始:
        4.0.3
      • addDefaultScripts

        public EmbeddedDatabaseBuilder addDefaultScripts()
        Add default SQL scripts to execute to populate the database.

        The default scripts are "schema.sql" to create the database schema and "data.sql" to populate the database with data.

        返回:
        this, to facilitate method chaining
      • addScript

        public EmbeddedDatabaseBuilder addScript​(String script)
        Add an SQL script to execute to initialize or populate the database.
        参数:
        script - the script to execute
        返回:
        this, to facilitate method chaining
      • addScripts

        public EmbeddedDatabaseBuilder addScripts​(String... scripts)
        Add multiple SQL scripts to execute to initialize or populate the database.
        参数:
        scripts - the scripts to execute
        返回:
        this, to facilitate method chaining
        从以下版本开始:
        4.0.3
      • setScriptEncoding

        public EmbeddedDatabaseBuilder setScriptEncoding​(String scriptEncoding)
        Specify the character encoding used in all SQL scripts, if different from the platform encoding.
        参数:
        scriptEncoding - the encoding used in scripts
        返回:
        this, to facilitate method chaining
        从以下版本开始:
        4.0.3
      • setSeparator

        public EmbeddedDatabaseBuilder setSeparator​(String separator)
        Specify the statement separator used in all SQL scripts, if a custom one.

        Defaults to ";" if not specified and falls back to "\n" as a last resort; may be set to ScriptUtils.EOF_STATEMENT_SEPARATOR to signal that each script contains a single statement without a separator.

        参数:
        separator - the statement separator
        返回:
        this, to facilitate method chaining
        从以下版本开始:
        4.0.3
      • setCommentPrefixes

        public EmbeddedDatabaseBuilder setCommentPrefixes​(String... commentPrefixes)
        Specify the prefixes that identify single-line comments within all SQL scripts.

        Defaults to ["--"].

        参数:
        commentPrefixes - the prefixes for single-line comments
        返回:
        this, to facilitate method chaining
        从以下版本开始:
        5.2
      • continueOnError

        public EmbeddedDatabaseBuilder continueOnError​(boolean flag)
        Specify that all failures which occur while executing SQL scripts should be logged but should not cause a failure.

        Defaults to false.

        参数:
        flag - true if script execution should continue on error
        返回:
        this, to facilitate method chaining
        从以下版本开始:
        4.0.3
      • ignoreFailedDrops

        public EmbeddedDatabaseBuilder ignoreFailedDrops​(boolean flag)
        Specify that a failed SQL DROP statement within an executed script can be ignored.

        This is useful for a database whose SQL dialect does not support an IF EXISTS clause in a DROP statement.

        The default is false so that building will fail fast if a script starts with a DROP statement.

        参数:
        flag - true if failed drop statements should be ignored
        返回:
        this, to facilitate method chaining
        从以下版本开始:
        4.0.3