
com.centit.framework.jdbc.dao.DatabaseOptUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of centit-persistence-jdbc Show documentation
Show all versions of centit-persistence-jdbc Show documentation
南大先腾自己的Orm框架,借用spring-jdbc管理链接和事物
The newest version!
package com.centit.framework.jdbc.dao;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.centit.support.algorithm.CollectionsOpt;
import com.centit.support.database.orm.JpaMetadata;
import com.centit.support.database.orm.TableMapInfo;
import com.centit.support.database.utils.PageDesc;
import com.centit.support.database.utils.QueryUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.ImmutableTriple;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 意图将BaseDao中公共的部分独立出来,减少类的函数数量,
* 因为每一个继承BaseDaoImpl的类都有这些函数,而这些行数基本上都是一样的
*/
@SuppressWarnings("unused")
public abstract class DatabaseOptUtils {
protected static Logger logger = LoggerFactory.getLogger(DatabaseOptUtils.class);
public static Map collectRequestParameters(HttpServletRequest request) {
Map parameterMap = request.getParameterMap();
Map map = new HashMap<>();
//map.put("isValid", "T");
for (Map.Entry ent : parameterMap.entrySet()) {
String key = ent.getKey();
if(key.startsWith("_"))
continue;
String[] values = CollectionsOpt.removeBlankString(ent.getValue());
if(values==null)
continue;
Object paramValue = values.length==1 ? values[0] : values;
String pretreatmentSql = key;
ImmutableTriple paramDesc = QueryUtils.parseParameter(pretreatmentSql);
String pretreatment = paramDesc.getRight();
String valueName = StringUtils.isBlank(paramDesc.getMiddle()) ? paramDesc.getLeft() : paramDesc.getMiddle();
if(StringUtils.isNotBlank(pretreatment)){
paramValue = QueryUtils.pretreatParameter(pretreatment, paramValue);
}
map.put(valueName, paramValue);
}
return map;
}
/**
* 获取 类的所有字段
* @param poClass Entity java 对象
* @return 字段名
*/
public static List extraPoAllFieldNames(Class> poClass){
TableMapInfo mapInfo = JpaMetadata.fetchTableMapInfo(poClass);
if(mapInfo==null)
return null;
return mapInfo.getAllFieldsName();
}
/**
* 获取 类的所有字段
* @param poClass Entity java 对象
* @return 字段名
*/
public static String[] extraPoAllFieldNamesAsArray(Class> poClass){
List filedNames = extraPoAllFieldNames(poClass);
if(filedNames==null)
return null;
return filedNames.toArray(new String[filedNames.size()]);
}
/**
* @param baseDao 数据库链接
* @param procName procName
* @param sqlType
* 返回值类型
* @param paramObjs paramObjs
* @return 调用数据库函数
* */
public static Object callFunction(BaseDaoImpl, ?> baseDao, String procName,
int sqlType, Object... paramObjs){
return JdbcTemplateUtils.callFunction(baseDao.getJdbcTemplate(), procName,
sqlType, paramObjs);
}
public final static boolean callProcedure(BaseDaoImpl, ?> baseDao, String procName, Object... paramObjs){
return JdbcTemplateUtils.callProcedure(baseDao.getJdbcTemplate(), procName, paramObjs);
}
public final static boolean doExecuteSql(BaseDaoImpl, ?> baseDao, String sSql) throws DataAccessException {
return JdbcTemplateUtils.doExecuteSql(baseDao.getJdbcTemplate(), sSql);
}
/*
* 直接运行行带参数的 SQL,update delete insert
*/
public final static int doExecuteSql(BaseDaoImpl, ?> baseDao, String sSql, Object[] values) throws DataAccessException {
return JdbcTemplateUtils.doExecuteSql(baseDao.getJdbcTemplate(), sSql, values);
}
/*
* 执行一个带命名参数的sql语句
*/
public final static int doExecuteNamedSql(BaseDaoImpl, ?> baseDao, String sSql, Map values)
throws DataAccessException {
return JdbcTemplateUtils.doExecuteNamedSql(baseDao.getJdbcTemplate(), sSql, values);
}
/* 下面所有的查询都返回 jsonArray */
public static JSONArray listObjectsByNamedSqlAsJson(BaseDaoImpl, ?> baseDao,
String querySql, String[] fieldNames, String queryCountSql,
Map namedParams, PageDesc pageDesc /*,
Map> dictionaryMapInfo*/) {
return JdbcTemplateUtils.listObjectsByNamedSqlAsJson(baseDao.getJdbcTemplate(),
querySql, fieldNames, queryCountSql, namedParams, pageDesc);
}
public static JSONArray listObjectsByNamedSqlAsJson(BaseDaoImpl, ?> baseDao,
String querySql, String[] fieldNames,
Map namedParams, PageDesc pageDesc) {
return listObjectsByNamedSqlAsJson(baseDao, querySql, fieldNames,
QueryUtils.buildGetCountSQLByReplaceFields( querySql), namedParams, pageDesc);
}
public static JSONArray listObjectsByNamedSqlAsJson(BaseDaoImpl, ?> baseDao,
String querySql, String[] fieldNames,
Map namedParams) {
return JdbcTemplateUtils.listObjectsByNamedSqlAsJson(baseDao.getJdbcTemplate(),
querySql, fieldNames, namedParams);
}
public static JSONArray listObjectsByNamedSqlAsJson(BaseDaoImpl, ?> baseDao,
String querySql, String queryCountSql,
Map namedParams, PageDesc pageDesc) {
return listObjectsByNamedSqlAsJson(baseDao, querySql, null,
queryCountSql, namedParams, pageDesc);
}
public static JSONArray listObjectsByNamedSqlAsJson(BaseDaoImpl, ?> baseDao, String querySql,
Map params) {
return JdbcTemplateUtils.listObjectsByNamedSqlAsJson(baseDao.getJdbcTemplate(),
querySql, params);
}
public static JSONArray listObjectsByNamedSqlAsJson(BaseDaoImpl, ?> baseDao, String querySql,
Map namedParams, PageDesc pageDesc) {
return JdbcTemplateUtils.listObjectsByNamedSqlAsJson(baseDao.getJdbcTemplate(),
querySql, namedParams, pageDesc);
}
public static JSONArray listObjectsBySqlAsJson(BaseDaoImpl, ?> baseDao, String querySql, String[] fieldNames,
String queryCountSql, Object[] params, PageDesc pageDesc) {
return JdbcTemplateUtils.listObjectsBySqlAsJson(baseDao.getJdbcTemplate(),
querySql, fieldNames, queryCountSql, params, pageDesc);
}
public static JSONArray listObjectsBySqlAsJson(BaseDaoImpl, ?> baseDao,
String querySql, String[] fieldNames,
Object[] params) {
return JdbcTemplateUtils.listObjectsBySqlAsJson(baseDao.getJdbcTemplate(),
querySql, fieldNames, params);
}
public static JSONArray listObjectsBySqlAsJson(BaseDaoImpl, ?> baseDao,
String querySql, String[] fieldNames,
Object[] params, PageDesc pageDesc) {
return listObjectsBySqlAsJson(baseDao, querySql, fieldNames,
QueryUtils.buildGetCountSQLByReplaceFields( querySql), params, pageDesc);
}
public static JSONArray listObjectsBySqlAsJson(BaseDaoImpl, ?> baseDao, String querySql, String queryCountSql,
Object[] params, PageDesc pageDesc) {
return listObjectsBySqlAsJson(baseDao, querySql, null, queryCountSql, params, pageDesc);
}
public static JSONArray listObjectsBySqlAsJson(BaseDaoImpl, ?> baseDao,
String querySql, Object[] params, String[] fieldnames) {
return JdbcTemplateUtils.listObjectsBySqlAsJson(baseDao.getJdbcTemplate(),
querySql, params, fieldnames);
}
public static JSONArray listObjectsBySqlAsJson(BaseDaoImpl, ?> baseDao, String querySql, Object[] params) {
return JdbcTemplateUtils.listObjectsBySqlAsJson(baseDao.getJdbcTemplate(),
querySql, params);
}
public static JSONArray listObjectsBySqlAsJson(BaseDaoImpl, ?> baseDao,
String querySql, Object[] params, PageDesc pageDesc) {
return JdbcTemplateUtils.listObjectsBySqlAsJson(baseDao.getJdbcTemplate(),
querySql, params, pageDesc);
}
public static List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy