类 JdbcTestUtils
- java.lang.Object
- org.springframework.test.jdbc.JdbcTestUtils
public class JdbcTestUtils extends Object
JdbcTestUtils
is a collection of JDBC related utility functions intended to simplify standard database testing scenarios.- 从以下版本开始:
- 2.5.4
- 作者:
- Thomas Risberg, Sam Brannen, Juergen Hoeller, Phillip Webb, Chris Baldwin
- 另请参阅:
JdbcTemplate
,ScriptUtils
,ResourceDatabasePopulator
,DatabasePopulatorUtils
构造器概要
构造器 构造器 说明 JdbcTestUtils()
方法概要
所有方法 静态方法 具体方法 已过时的方法 修饰符和类型 方法 说明 static boolean
containsSqlScriptDelimiters(String script, char delim)
已过时。as of Spring 4.0.3, in favor of usingScriptUtils.containsSqlScriptDelimiters(java.lang.String, java.lang.String)
static int
countRowsInTable(JdbcTemplate jdbcTemplate, String tableName)
Count the rows in the given table.static int
countRowsInTableWhere(JdbcTemplate jdbcTemplate, String tableName, String whereClause)
Count the rows in the given table, using the providedWHERE
clause.static int
deleteFromTables(JdbcTemplate jdbcTemplate, String... tableNames)
Delete all rows from the specified tables.static int
deleteFromTableWhere(JdbcTemplate jdbcTemplate, String tableName, String whereClause, Object... args)
Delete rows from the given table, using the providedWHERE
clause.static void
dropTables(JdbcTemplate jdbcTemplate, String... tableNames)
Drop the specified tables.static void
executeSqlScript(JdbcTemplate jdbcTemplate, ResourceLoader resourceLoader, String sqlResourcePath, boolean continueOnError)
已过时。as of Spring 4.0.3, in favor of usingScriptUtils.executeSqlScript(java.sql.Connection, org.springframework.core.io.Resource)
orResourceDatabasePopulator
.static void
executeSqlScript(JdbcTemplate jdbcTemplate, Resource resource, boolean continueOnError)
已过时。as of Spring 4.0.3, in favor of usingScriptUtils.executeSqlScript(java.sql.Connection, org.springframework.core.io.Resource)
orResourceDatabasePopulator
.static void
executeSqlScript(JdbcTemplate jdbcTemplate, EncodedResource resource, boolean continueOnError)
已过时。as of Spring 4.0.3, in favor of usingScriptUtils.executeSqlScript(java.sql.Connection, org.springframework.core.io.Resource)
orResourceDatabasePopulator
.static String
readScript(LineNumberReader lineNumberReader)
已过时。as of Spring 4.0.3, in favor of usingScriptUtils.readScript(LineNumberReader, String, String)
static String
readScript(LineNumberReader lineNumberReader, String commentPrefix)
已过时。as of Spring 4.0.3, in favor of usingScriptUtils.readScript(LineNumberReader, String, String)
static void
splitSqlScript(String script, char delim, List<String> statements)
已过时。as of Spring 4.0.3, in favor of usingScriptUtils.splitSqlScript(String, char, List)
构造器详细资料
JdbcTestUtils
public JdbcTestUtils()
方法详细资料
countRowsInTable
public static int countRowsInTable(JdbcTemplate jdbcTemplate, String tableName)
Count the rows in the given table.- 参数:
jdbcTemplate
- the JdbcTemplate with which to perform JDBC operationstableName
- name of the table to count rows in- 返回:
- the number of rows in the table
countRowsInTableWhere
public static int countRowsInTableWhere(JdbcTemplate jdbcTemplate, String tableName, String whereClause)
Count the rows in the given table, using the providedWHERE
clause.If the provided
WHERE
clause contains text, it will be prefixed with" WHERE "
and then appended to the generatedSELECT
statement. For example, if the provided table name is"person"
and the provided where clause is"name = 'Bob' and age > 25"
, the resulting SQL statement to execute will be"SELECT COUNT(0) FROM person WHERE name = 'Bob' and age > 25"
.- 参数:
jdbcTemplate
- the JdbcTemplate with which to perform JDBC operationstableName
- the name of the table to count rows inwhereClause
- theWHERE
clause to append to the query- 返回:
- the number of rows in the table that match the provided
WHERE
clause
deleteFromTables
public static int deleteFromTables(JdbcTemplate jdbcTemplate, String... tableNames)
Delete all rows from the specified tables.- 参数:
jdbcTemplate
- the JdbcTemplate with which to perform JDBC operationstableNames
- the names of the tables to delete from- 返回:
- the total number of rows deleted from all specified tables
deleteFromTableWhere
public static int deleteFromTableWhere(JdbcTemplate jdbcTemplate, String tableName, String whereClause, Object... args)
Delete rows from the given table, using the providedWHERE
clause.If the provided
WHERE
clause contains text, it will be prefixed with" WHERE "
and then appended to the generatedDELETE
statement. For example, if the provided table name is"person"
and the provided where clause is"name = 'Bob' and age > 25"
, the resulting SQL statement to execute will be"DELETE FROM person WHERE name = 'Bob' and age > 25"
.As an alternative to hard-coded values, the
"?"
placeholder can be used within theWHERE
clause, binding to the given arguments.- 参数:
jdbcTemplate
- the JdbcTemplate with which to perform JDBC operationstableName
- the name of the table to delete rows fromwhereClause
- theWHERE
clause to append to the queryargs
- arguments to bind to the query (leaving it to the PreparedStatement to guess the corresponding SQL type); may also containSqlParameterValue
objects which indicate not only the argument value but also the SQL type and optionally the scale.- 返回:
- the number of rows deleted from the table
dropTables
public static void dropTables(JdbcTemplate jdbcTemplate, String... tableNames)
Drop the specified tables.- 参数:
jdbcTemplate
- the JdbcTemplate with which to perform JDBC operationstableNames
- the names of the tables to drop
executeSqlScript
@Deprecated public static void executeSqlScript(JdbcTemplate jdbcTemplate, ResourceLoader resourceLoader, String sqlResourcePath, boolean continueOnError) throws DataAccessException
已过时。as of Spring 4.0.3, in favor of usingScriptUtils.executeSqlScript(java.sql.Connection, org.springframework.core.io.Resource)
orResourceDatabasePopulator
.Execute the given SQL script.The script will typically be loaded from the classpath. There should be one statement per line. Any semicolons and line comments will be removed.
Do not use this method to execute DDL if you expect rollback.
- 参数:
jdbcTemplate
- the JdbcTemplate with which to perform JDBC operationsresourceLoader
- the resource loader with which to load the SQL scriptsqlResourcePath
- the Spring resource path for the SQL scriptcontinueOnError
- whether or not to continue without throwing an exception in the event of an error- 抛出:
DataAccessException
- if there is an error executing a statement andcontinueOnError
isfalse
- 另请参阅:
ResourceDatabasePopulator
,executeSqlScript(JdbcTemplate, Resource, boolean)
executeSqlScript
@Deprecated public static void executeSqlScript(JdbcTemplate jdbcTemplate, Resource resource, boolean continueOnError) throws DataAccessException
已过时。as of Spring 4.0.3, in favor of usingScriptUtils.executeSqlScript(java.sql.Connection, org.springframework.core.io.Resource)
orResourceDatabasePopulator
.Execute the given SQL script.The script will typically be loaded from the classpath. Statements should be delimited with a semicolon. If statements are not delimited with a semicolon then there should be one statement per line. Statements are allowed to span lines only if they are delimited with a semicolon. Any line comments will be removed.
Do not use this method to execute DDL if you expect rollback.
- 参数:
jdbcTemplate
- the JdbcTemplate with which to perform JDBC operationsresource
- the resource to load the SQL script fromcontinueOnError
- whether or not to continue without throwing an exception in the event of an error- 抛出:
DataAccessException
- if there is an error executing a statement andcontinueOnError
isfalse
- 另请参阅:
ResourceDatabasePopulator
,executeSqlScript(JdbcTemplate, EncodedResource, boolean)
executeSqlScript
@Deprecated public static void executeSqlScript(JdbcTemplate jdbcTemplate, EncodedResource resource, boolean continueOnError) throws DataAccessException
已过时。as of Spring 4.0.3, in favor of usingScriptUtils.executeSqlScript(java.sql.Connection, org.springframework.core.io.Resource)
orResourceDatabasePopulator
.Execute the given SQL script.The script will typically be loaded from the classpath. There should be one statement per line. Any semicolons and line comments will be removed.
Do not use this method to execute DDL if you expect rollback.
- 参数:
jdbcTemplate
- the JdbcTemplate with which to perform JDBC operationsresource
- the resource (potentially associated with a specific encoding) to load the SQL script fromcontinueOnError
- whether or not to continue without throwing an exception in the event of an error- 抛出:
DataAccessException
- if there is an error executing a statement andcontinueOnError
isfalse
- 另请参阅:
ResourceDatabasePopulator
readScript
@Deprecated public static String readScript(LineNumberReader lineNumberReader) throws IOException
已过时。as of Spring 4.0.3, in favor of usingScriptUtils.readScript(LineNumberReader, String, String)
Read a script from the providedLineNumberReader
, using "--
" as the comment prefix, and build aString
containing the lines.- 参数:
lineNumberReader
- theLineNumberReader
containing the script to be processed- 返回:
- a
String
containing the script lines - 抛出:
IOException
- 另请参阅:
readScript(LineNumberReader, String)
readScript
@Deprecated public static String readScript(LineNumberReader lineNumberReader, String commentPrefix) throws IOException
已过时。as of Spring 4.0.3, in favor of usingScriptUtils.readScript(LineNumberReader, String, String)
Read a script from the providedLineNumberReader
, using the supplied comment prefix, and build aString
containing the lines.Lines beginning with the comment prefix are excluded from the results; however, line comments anywhere else — for example, within a statement — will be included in the results.
- 参数:
lineNumberReader
- theLineNumberReader
containing the script to be processedcommentPrefix
- the prefix that identifies comments in the SQL script — typically "--"- 返回:
- a
String
containing the script lines - 抛出:
IOException
containsSqlScriptDelimiters
@Deprecated public static boolean containsSqlScriptDelimiters(String script, char delim)
已过时。as of Spring 4.0.3, in favor of usingScriptUtils.containsSqlScriptDelimiters(java.lang.String, java.lang.String)
Determine if the provided SQL script contains the specified delimiter.- 参数:
script
- the SQL scriptdelim
- character delimiting each statement — typically a ';' character- 返回:
true
if the script contains the delimiter;false
otherwise
splitSqlScript
@Deprecated public static void splitSqlScript(String script, char delim, List<String> statements)
已过时。as of Spring 4.0.3, in favor of usingScriptUtils.splitSqlScript(String, char, List)
Split an SQL script into separate statements delimited by the provided delimiter character. Each individual statement will be added to the providedList
.Within a statement, "
--
" will be used as the comment prefix; any text beginning with the comment prefix and extending to the end of the line will be omitted from the statement. In addition, multiple adjacent whitespace characters will be collapsed into a single space.- 参数:
script
- the SQL scriptdelim
- character delimiting each statement — typically a ';' characterstatements
- the list that will contain the individual statements