/*** @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();
? }
}