一、前言
关于spring-test源码包中org.springframework.test.jdbc.JdbcTestUtils数据库测试工具类,基于org.springframework.jdbc.core.JdbcTemplate模板操作类,对数据常用dml增删改查、DDL及脚本Script等进行代码示例操作,详情参见源码说明部分。
二、源码说明
package org.springframework.test.jdbc;@b@@b@import java.io.IOException;@b@import java.io.LineNumberReader;@b@import java.util.List;@b@import org.apache.commons.logging.Log;@b@import org.apache.commons.logging.LogFactory;@b@import org.springframework.core.io.Resource;@b@import org.springframework.core.io.ResourceLoader;@b@import org.springframework.core.io.support.EncodedResource;@b@import org.springframework.dao.DataAccessException;@b@import org.springframework.jdbc.core.JdbcTemplate;@b@import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;@b@import org.springframework.jdbc.datasource.init.ScriptUtils;@b@import org.springframework.util.StringUtils;@b@@b@public class JdbcTestUtils@b@{@b@ private static final Log logger = LogFactory.getLog(JdbcTestUtils.class);@b@@b@ public static int countRowsInTable(JdbcTemplate jdbcTemplate, String tableName)@b@ {@b@ return ((Integer)jdbcTemplate.queryForObject("SELECT COUNT(0) FROM " + tableName, Integer.class)).intValue();@b@ }@b@@b@ public static int countRowsInTableWhere(JdbcTemplate jdbcTemplate, String tableName, String whereClause)@b@ {@b@ String sql = "SELECT COUNT(0) FROM " + tableName;@b@ if (StringUtils.hasText(whereClause))@b@ sql = sql + " WHERE " + whereClause;@b@@b@ return ((Integer)jdbcTemplate.queryForObject(sql, Integer.class)).intValue();@b@ }@b@@b@ public static int deleteFromTables(JdbcTemplate jdbcTemplate, String[] tableNames)@b@ {@b@ int totalRowCount = 0;@b@ String[] arrayOfString = tableNames; int i = arrayOfString.length; for (int j = 0; j < i; ++j) { String tableName = arrayOfString[j];@b@ int rowCount = jdbcTemplate.update("DELETE FROM " + tableName);@b@ totalRowCount += rowCount;@b@ if (logger.isInfoEnabled())@b@ logger.info("Deleted " + rowCount + " rows from table " + tableName);@b@ }@b@@b@ return totalRowCount;@b@ }@b@@b@ public static int deleteFromTableWhere(JdbcTemplate jdbcTemplate, String tableName, String whereClause, Object[] args)@b@ {@b@ String sql = "DELETE FROM " + tableName;@b@ if (StringUtils.hasText(whereClause))@b@ sql = sql + " WHERE " + whereClause;@b@@b@ int rowCount = ((args != null) && (args.length > 0)) ? jdbcTemplate.update(sql, args) : jdbcTemplate.update(sql);@b@ if (logger.isInfoEnabled())@b@ logger.info("Deleted " + rowCount + " rows from table " + tableName);@b@@b@ return rowCount;@b@ }@b@@b@ public static void dropTables(JdbcTemplate jdbcTemplate, String[] tableNames)@b@ {@b@ String[] arrayOfString = tableNames; int i = arrayOfString.length; for (int j = 0; j < i; ++j) { String tableName = arrayOfString[j];@b@ jdbcTemplate.execute("DROP TABLE " + tableName);@b@ if (logger.isInfoEnabled())@b@ logger.info("Dropped table " + tableName);@b@ }@b@ }@b@@b@ @Deprecated@b@ public static void executeSqlScript(JdbcTemplate jdbcTemplate, ResourceLoader resourceLoader, String sqlResourcePath, boolean continueOnError)@b@ throws DataAccessException@b@ {@b@ Resource resource = resourceLoader.getResource(sqlResourcePath);@b@ executeSqlScript(jdbcTemplate, resource, continueOnError);@b@ }@b@@b@ @Deprecated@b@ public static void executeSqlScript(JdbcTemplate jdbcTemplate, Resource resource, boolean continueOnError)@b@ throws DataAccessException@b@ {@b@ executeSqlScript(jdbcTemplate, new EncodedResource(resource), continueOnError);@b@ }@b@@b@ @Deprecated@b@ public static void executeSqlScript(JdbcTemplate jdbcTemplate, EncodedResource resource, boolean continueOnError)@b@ throws DataAccessException@b@ {@b@ new ResourceDatabasePopulator(continueOnError, false, resource.getEncoding(), new Resource[] { resource.getResource() }).execute(jdbcTemplate.getDataSource());@b@ }@b@@b@ @Deprecated@b@ public static String readScript(LineNumberReader lineNumberReader)@b@ throws IOException@b@ {@b@ return readScript(lineNumberReader, "--");@b@ }@b@@b@ @Deprecated@b@ public static String readScript(LineNumberReader lineNumberReader, String commentPrefix)@b@ throws IOException@b@ {@b@ return ScriptUtils.readScript(lineNumberReader, commentPrefix, ";");@b@ }@b@@b@ @Deprecated@b@ public static boolean containsSqlScriptDelimiters(String script, char delim)@b@ {@b@ return ScriptUtils.containsSqlScriptDelimiters(script, String.valueOf(delim));@b@ }@b@@b@ @Deprecated@b@ public static void splitSqlScript(String script, char delim, List<String> statements)@b@ {@b@ ScriptUtils.splitSqlScript(script, delim, statements);@b@ }@b@}