測試工具-JdbcUtil

/*** @ClassName: JdbcUtil

* @Description: 封裝測試常用的數(shù)據(jù)庫操作**/

public class JdbcUtil {

private static final Loglog = LogFactory.getLog(JdbcUtil.class);

? /**

* 向指定表中插入數(shù)據(jù)

*

? ? * @param jdbcTemplate

? ? *? ? ? ? ? ? 包含數(shù)據(jù)源信息的NamedParameterJdbcTemplate

? ? * @param tableName

? ? * @param columnNameValuePairs

? ? */

? public static int insertData(NamedParameterJdbcTemplate jdbcTemplate,

? ? ? ? String tableName, Map columnNameValuePairs) {

Assert.notNull(jdbcTemplate, "jdbcTemplate must not be null");

? ? ? Assert.notNull(tableName, "tableName must not be null");

? ? ? Assert.notEmpty(columnNameValuePairs,

? ? ? ? ? ? "columnNameValuePairs must not be empty");

? ? ? StringBuffer preSql =new StringBuffer("INSERT INTO " + tableName+"(");

? ? ? StringBuffer afterSql =new StringBuffer(" VALUES(");

? ? ? StringBuffer afterSqlToLog =new StringBuffer(" VALUES(");

? ? ? Set columnNames = columnNameValuePairs.keySet();

? ? ? for (String columnName : columnNames) {

preSql.append(columnName+",");

? ? ? ? afterSql.append(":"+columnName+",");

? ? ? ? afterSqlToLog.append(columnNameValuePairs.get(columnName)+",");

? ? ? }

preSql.deleteCharAt(preSql.toString().length()-1).append(") ");

? ? ? afterSql.deleteCharAt(afterSql.toString().length()-1).append(")");

? ? ? afterSqlToLog.deleteCharAt(afterSqlToLog.toString().length()-1).append(")");

? ? ? //待執(zhí)行的sql

? ? ? String sql = preSql.toString()+afterSql.toString();

? ? ? String sqlToLog = preSql.toString()+afterSqlToLog.toString();

? ? ? log.info(sqlToLog);

? ? ? return jdbcTemplate.update(sql,columnNameValuePairs);

? }

/**

* 根據(jù)id刪除表(表必須有id這個字段)中數(shù)據(jù)

*

? ? * @param jdbcTemplate

? ? *? ? ? ? ? ? 包含數(shù)據(jù)源信息的NamedParameterJdbcTemplate

? ? * @param tableName

? ? * @param id

? ? */

? public static int deleteDataById(NamedParameterJdbcTemplate jdbcTemplate,

? ? ? ? String tableName, Object idValue) {

Map columnNameValuePairs =new HashMap();

? ? ? columnNameValuePairs.put("id", idValue);

? ? ? return deleteData(jdbcTemplate, tableName, columnNameValuePairs);

? }

/**

* 根據(jù)主鍵刪除表中數(shù)據(jù)

*

? ? * @param jdbcTemplate

? ? *? ? ? ? ? ? 包含數(shù)據(jù)源信息的NamedParameterJdbcTemplate

? ? * @param tableName

? ? *? ? ? ? ? ? 表名

? ? * @param primaryKeyName

? ? *? ? ? ? ? ? 主鍵字段名

? ? * @param primaryKeyValue

? ? */

? public static int deleteDataByPrimaryKey(NamedParameterJdbcTemplate jdbcTemplate,

? ? ? ? String tableName, String primaryKeyName, Object primaryKeyValue) {

Map columnNameValuePairs =new HashMap();

? ? ? columnNameValuePairs.put(primaryKeyName, primaryKeyValue);

? ? ? return deleteData(jdbcTemplate, tableName, columnNameValuePairs);

? }

/**

* 根據(jù)數(shù)據(jù)庫表字段名稱和值刪除

*

? ? * @param jdbcTemplate

? ? * @param tableName

? ? * @param columnNameValuePairs

? ? * @return

? ? */

? public static int deleteData(NamedParameterJdbcTemplate jdbcTemplate,

? ? ? ? String tableName, Map columnNameValuePairs) {

Assert.notNull(jdbcTemplate, "jdbcTemplate must not be null");

? ? ? Assert.notNull(tableName, "tableName must not be null");

? ? ? Assert.notEmpty(columnNameValuePairs,

? ? ? ? ? ? "columnNameValuePairs must not be empty");

? ? ? StringBuffer sql =new StringBuffer("DELETE FROM " + tableName

+" WHERE 1=1 ");

? ? ? StringBuffer sqlToLog =new StringBuffer(sql);

? ? ? Set columnNames = columnNameValuePairs.keySet();

? ? ? for (String columnName : columnNames) {

sql.append(" AND " + columnName +"=:" + columnName);

? ? ? ? sqlToLog.append(" AND " + columnName +"='"

? ? ? ? ? ? ? + columnNameValuePairs.get(columnName) +"'");

? ? ? }

log.info(sqlToLog);

? ? ? return jdbcTemplate.update(sql.toString(), columnNameValuePairs);

? }

/**

* 根據(jù)主鍵查詢表(表必須有id這個字段)中數(shù)據(jù)

*

? ? * @param jdbcTemplate

? ? *? ? ? ? ? ? 包含數(shù)據(jù)源信息的NamedParameterJdbcTemplate

? ? * @param tableName

? ? *? ? ? ? ? ? 表名

? ? * @param idValue

? ? *? ? ? ? ? ? id值

? ? * @return

? ? */

? public static List>queryDataById(

NamedParameterJdbcTemplate jdbcTemplate, String tableName, Object idValue) {

Map columnNameValuePairs =new HashMap();

? ? ? columnNameValuePairs.put("id", idValue);

? ? ? return queryData(jdbcTemplate, tableName, columnNameValuePairs);

? }

/**

* 根據(jù)主鍵查詢表中數(shù)據(jù)

*

? ? * @param jdbcTemplate

? ? *? ? ? ? ? ? 包含數(shù)據(jù)源信息的NamedParameterJdbcTemplate

? ? * @param tableName

? ? *? ? ? ? ? ? 表名

? ? * @param primaryKeyName

? ? *? ? ? ? ? ? 主鍵字段名

? ? * @param primaryKeyValue

? ? *? ? ? ? ? ? 主鍵值

? ? * @return

? ? */

? public static List>queryDataByPrimaryKey(

NamedParameterJdbcTemplate jdbcTemplate, String tableName,

? ? ? ? String primaryKeyName, Object primaryKeyValue) {

Assert.notNull(jdbcTemplate, "jdbcTemplate must not be null");

? ? ? Assert.notNull(tableName, "tableName must not be null");

? ? ? Assert.notNull(primaryKeyName, "primaryKeyName must not be null");

? ? ? Map columnNameValuePairs =new HashMap();

? ? ? columnNameValuePairs.put(primaryKeyName, primaryKeyValue);

? ? ? return queryData(jdbcTemplate, tableName, columnNameValuePairs);

? }

/**

* 根據(jù)數(shù)據(jù)庫表字段名稱和值查詢

*

? ? * @param jdbcTemplate

? ? * @param tableName

? ? * @param columnNameValuePairs

? ? * @return

? ? */

? public static List>queryData(

NamedParameterJdbcTemplate jdbcTemplate, String tableName,

? ? ? ? Map columnNameValuePairs) {

Assert.notNull(jdbcTemplate, "jdbcTemplate must not be null");

? ? ? Assert.notNull(tableName, "tableName must not be null");

? ? ? Assert.notEmpty(columnNameValuePairs,

? ? ? ? ? ? "columnNameValuePairs must not be empty");

? ? ? StringBuffer sql =new StringBuffer("SELECT * FROM " + tableName

+" WHERE 1=1 ");

? ? ? StringBuffer sqlToLog =new StringBuffer(sql);

? ? ? Set columnNames = columnNameValuePairs.keySet();

? ? ? for (String columnName : columnNames) {

sql.append(" AND " + columnName +"=:" + columnName);

? ? ? ? sqlToLog.append(" AND " + columnName +"='"

? ? ? ? ? ? ? + columnNameValuePairs.get(columnName) +"'");

? ? ? }

log.info(sqlToLog);

? ? ? return jdbcTemplate.queryForList(sql.toString(), columnNameValuePairs);

? }

/**

* 根據(jù)主鍵判斷表(表必須有id這個字段)中是否存在數(shù)據(jù)

*

? ? * @param jdbcTemplate

? ? * @param tableName

? ? * @param idValue

? ? * @return

? ? */

? public static boolean isExistsInDb(NamedParameterJdbcTemplate jdbcTemplate,

? ? ? ? String tableName, Object idValue) {

return queryDataById(jdbcTemplate, tableName, idValue).size() >0;

? }

/**

* 根據(jù)主鍵判斷表中是否存在數(shù)據(jù)

*

? ? * @param jdbcTemplate

? ? * @param tableName

? ? * @param primaryKeyName

? ? * @param primaryKeyValue

? ? * @return

? ? */

? public static boolean isExistsInDb(NamedParameterJdbcTemplate jdbcTemplate,

? ? ? ? String tableName, String primaryKeyName,Object primaryKeyValue) {

return queryDataByPrimaryKey(jdbcTemplate, tableName, primaryKeyName,

? ? ? ? ? ? primaryKeyValue).size() >0;

? }

/**

*

? ? * @param jdbcTemplate

? ? * @param tableName

? ? * @param columnNameValuePairs

? ? * @return

? ? */

? public static boolean isExistsInDb(NamedParameterJdbcTemplate jdbcTemplate,

? ? ? ? String tableName, Map columnNameValuePairs) {

return queryData(jdbcTemplate, tableName, columnNameValuePairs).size() >0;

? }

/**

* 組裝Oracle分頁查詢語句

*

? ? * @param sql

? ? *? ? ? ? ? ? 原始sql

? ? * @param start

? ? *? ? ? ? ? ? 起始行

? ? * @param max

? ? *? ? ? ? ? ? 最大記錄數(shù)

? ? * @return

? ? */

? public static StringgetPagingSqlForOracle(String sql, int start, int max) {

StringBuffer pagingSelect =new StringBuffer();

? ? ? pagingSelect

.append("SELECT * FROM (SELECT row_.*,rownum rownum_ FROM( ");

? ? ? pagingSelect.append(sql);

? ? ? pagingSelect.append(" ) row_ WHERE rownum <= " + (start + max)

+") WHERE rownum_ > " + start);

? ? ? return pagingSelect.toString();

? }

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容