类 JdbcTestUtils
- java.lang.Object
- org.springframework.test.jdbc.JdbcTestUtils
public abstract class JdbcTestUtils extends Object
JdbcTestUtilsis 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 intcountRowsInTable(JdbcTemplate jdbcTemplate, String tableName)Count the rows in the given table.static intcountRowsInTableWhere(JdbcTemplate jdbcTemplate, String tableName, String whereClause)Count the rows in the given table, using the providedWHEREclause.static intdeleteFromTables(JdbcTemplate jdbcTemplate, String... tableNames)Delete all rows from the specified tables.static intdeleteFromTableWhere(JdbcTemplate jdbcTemplate, String tableName, String whereClause, Object... args)Delete rows from the given table, using the providedWHEREclause.static voiddropTables(JdbcTemplate jdbcTemplate, String... tableNames)Drop the specified tables.
构造器详细资料
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 providedWHEREclause.If the provided
WHEREclause contains text, it will be prefixed with" WHERE "and then appended to the generatedSELECTstatement. 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- theWHEREclause to append to the query- 返回:
- the number of rows in the table that match the provided
WHEREclause
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 providedWHEREclause.If the provided
WHEREclause contains text, it will be prefixed with" WHERE "and then appended to the generatedDELETEstatement. 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 theWHEREclause, binding to the given arguments.- 参数:
jdbcTemplate- the JdbcTemplate with which to perform JDBC operationstableName- the name of the table to delete rows fromwhereClause- theWHEREclause to append to the queryargs- arguments to bind to the query (leaving it to the PreparedStatement to guess the corresponding SQL type); may also containSqlParameterValueobjects 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