📄 abstractdaosupport.java
字号:
// 依次返回结果
for (int index = 0; index < outTypes.length; index++) {
results.add(cs.getObject(inValues.length
+ index + 1));
}
return results;
}
}
});
}
/**
* 调用函数并返回执行结果
*
* @param callStr
* 调用语句
* @param inValues
* 调用函数传入的参数值,依次传入
* @param outTypes
* 调用函数返回的结果类型,是sql 类型,见{@link java.sql.Types}.
* @return 根据函数定义的out参数返回执行结果
*/
public Object callFunction(final String callStr, final Object[] inValues,
final int[] outTypes) {
return getJdbcTemplate().execute(callStr,
new CallableStatementCallback() {
public Object doInCallableStatement(CallableStatement cs)
throws SQLException {
int i = 0;
if (outTypes != null) {
for (int index = 0; index < outTypes.length; index++) {
i++;
cs.registerOutParameter(i, outTypes[index]);
}
}
if (inValues != null) {
for (int index = 0; index < inValues.length; index++) {
i++;
// StatementCreatorUtils.setParameterValue(cs,
// i,
// Integer.MIN_VALUE, null,
// inValues[index]);
Object inValue = inValues[index];
if (inValue instanceof StringBuffer
|| inValue instanceof StringWriter) {
cs.setString(i, inValue.toString());
} else if ((inValue instanceof java.util.Date)
&& !(inValue instanceof java.sql.Date
|| inValue instanceof java.sql.Time || inValue instanceof java.sql.Timestamp)) {
cs.setTimestamp(i, new java.sql.Timestamp(
((java.util.Date) inValue)
.getTime()));
} else if (inValue instanceof Calendar) {
Calendar cal = (Calendar) inValue;
cs.setTimestamp(i, new java.sql.Timestamp(
cal.getTime().getTime()));
} else {
// Fall back to generic setObject call
// without SQL type specified.
cs.setObject(i, inValue);
}
}
}
boolean retVal = cs.execute();
int updateCount = cs.getUpdateCount();
if (retVal || updateCount != -1) {
// Map returnedResults = new HashMap();
// returnedResults.putAll(extractReturnedResultSets(cs,
// declaredParameters, updateCount));
}
if (outTypes == null || outTypes.length <= 0) {
return null;
} else if (outTypes.length == 1) {
return cs.getObject(1);
} else {
List results = new ArrayList();
// 依次返回结果
for (int index = 0; index < outTypes.length; index++) {
results.add(cs.getObject(index + 1));
}
return results;
}
}
});
}
/**
* Prepare the given Query object, applying cache settings and/or a
* transaction timeout.
*
* @param queryObject
* the Query object to prepare
* @see #setCacheQueries
* @see #setQueryCacheRegion
* @see SessionFactoryUtils#applyTransactionTimeout
*/
protected void prepareQuery(Query queryObject) {
if (getHibernateTemplate().isCacheQueries()) {
queryObject.setCacheable(true);
if (getHibernateTemplate().getQueryCacheRegion() != null) {
queryObject.setCacheRegion(getHibernateTemplate()
.getQueryCacheRegion());
}
}
SessionFactoryUtils.applyTransactionTimeout(queryObject,
getHibernateTemplate().getSessionFactory());
}
/**
* @return Returns the jdbcTemplate.
*/
public JdbcTemplate getJdbcTemplate() {
if (jdbcTemplate == null) {
throw new NoSuchBeanDefinitionException("jdbcTemplate",
"没有为数据访问对象配置Spring JDBC Template Bean.");
}
return jdbcTemplate;
}
/**
* @param jdbcTemplate
* The jdbcTemplate to set.
*/
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
protected int getJDBCBatchSize() {
String batchProp = getHibernateEnvirProp(Environment.STATEMENT_BATCH_SIZE);
if (batchProp == null || batchProp.length() == 0) {
return 30;
} else {
return new Integer(batchProp).intValue();
}
}
private String getHibernateEnvirProp(String key) {
return Environment.getProperties().getProperty(key);
}
/**
* 通用方法:更新某表的若干指定字段(字段名与更新及条件值须一一对应)
*
* @author liubo
* @param table
* 表名
* @param properties
* 字段名数组
* @param conditions
* 条件字段名数组
* @param values
* 更新及条件值列表
* @throws SystemRuleException
*/
public void updateProperty(String table, String[] properties,
String[] conditions, List values) throws SystemRuleException {
if (COMMON.isEmpty(table) || properties == null || values == null
|| conditions == null || properties.length < 1
|| values.size() != (properties.length + conditions.length)) {
throw new SystemRuleException("传入了空的表名,或空的字段名/值,或属性名和属性值的数量不相匹配!");
}
StringBuffer command = new StringBuffer(" update ");
command.append(table);
command.append(" set ");
for (int i = 0; i < properties.length; i++) {
if (i == properties.length - 1) {
command.append(properties[i]);
command.append(" = ? ");
break;
} else {
command.append(properties[i]);
command.append(" = ? ");
command.append(" , ");
}
}
command.append(" where ");
for (int i = 0; i < conditions.length; i++) {
if (i == conditions.length - 1) {
command.append(conditions[i]);
command.append(" = ? ");
break;
} else {
command.append(conditions[i]);
command.append(" = ? ");
command.append(" and ");
}
}
this.update(command.toString(), values.toArray());
}
/**
* 执行指定HQL语句返回的记录条数
*
* @param fromAndWhere
* hql语句:from .....
* @param params
* @return 记录条数
* @author yehailong
*/
public int getRowCount(String fromAndWhere, Object[] paramValues) {
Object countObj = this.getHibernateTemplate().find(
"select count(*) " + fromAndWhere, paramValues).get(0);
return ((Integer) countObj).intValue();
}
// 由于HibernateTemplate没有提供分页查询(从某条开始,并只返回指定条数),所以写了几个公共函数,以供分页使用 add by
// yehailong
/**
* @param queryString
* hql语句
* @param start:
* the first result, numbered from 0
* @param maxResults:the
* maximum number of results
* @return 结果集合List
*/
public List find(String queryString, int start, int maxResults) {
return this.getHibernateTemplate().executeFind(
new InnerHibernateCallback(queryString, start, maxResults) {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(getQueryString());
prepareQuery(query);
query.setFirstResult(this.getStart());
query.setMaxResults(this.getMaxResults());
return query.list();
}
});
}
public List find(String queryString, Object paramValue, int start,
int maxResults) {
Object[] paramValues = { paramValue };
return find(queryString, paramValues, start, maxResults);
}
public List find(String queryString, Object[] paramValues, int start,
int maxResults) {
return this.getHibernateTemplate().executeFind(
new InnerHibernateCallback(queryString, paramValues, start,
maxResults) {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(getQueryString());
prepareQuery(query);
Object[] paramValues = getParamValues();
for (int i = 0; paramValues != null
&& i < paramValues.length; i++) {
if (paramValues[i] instanceof Date) {
query.setDate(i, (Date) paramValues[i]);
} else if (paramValues[i] instanceof Calendar) {
query.setCalendar(i, (Calendar) paramValues[i]);
} else {
query.setParameter(i, paramValues[i]);
}
}
query.setFirstResult(this.getStart());
query.setMaxResults(this.getMaxResults());
return query.list();
}
});
}
public List findByNamedParam(String queryString, String[] paramNames,
Object[] paramValues, int start, int maxResults) {
return this.getHibernateTemplate().executeFind(
new InnerHibernateCallback(queryString, paramNames,
paramValues, start, maxResults) {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(getQueryString());
prepareQuery(query);
Object[] paramValues = getParamValues();
String[] paramNames = getParamNames();
for (int i = 0; paramNames != null
&& i < paramNames.length; i++) {
if (paramValues[i] instanceof Date) {
query.setDate(paramNames[i],
(Date) paramValues[i]);
} else if (paramValues[i] instanceof Calendar) {
query.setCalendar(paramNames[i],
(Calendar) paramValues[i]);
} else {
query.setParameter(paramNames[i],
paramValues[i]);
}
}
query.setFirstResult(this.getStart());
query.setMaxResults(this.getMaxResults());
return query.list();
}
});
}
// ------------------------------
abstract class InnerHibernateCallback implements HibernateCallback {
private int start;
private int maxResults;
private String queryString;
private String[] paramNames;
private Object[] paramValues;
public InnerHibernateCallback(String queryString, int start,
int maxResults) {
this.queryString = queryString;
this.start = start;
this.maxResults = maxResults;
}
public InnerHibernateCallback(String queryString, Object[] paramValues,
int start, int maxResults) {
this.queryString = queryString;
this.paramValues = paramValues;
this.start = start;
this.maxResults = maxResults;
}
public InnerHibernateCallback(String queryString, String[] paramNames,
Object[] paramValues, int start, int maxResults) {
this.queryString = queryString;
this.paramNames = paramNames;
this.paramValues = paramValues;
this.start = start;
this.maxResults = maxResults;
}
public int getStart() {
return this.start;
}
public int getMaxResults() {
return this.maxResults;
}
public String getQueryString() {
return this.queryString;
}
public Object[] getParamValues() {
return this.paramValues;
}
public String[] getParamNames() {
return this.paramNames;
}
public abstract Object doInHibernate(Session session)
throws HibernateException, SQLException;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -