📄 abstractdaohibernate.java
字号:
buf.append(" ");
}
//从排序列表读取排序字段,检查排序字段是否属于类中的属性值,检查排序方式是否支持,构造HQL排序语句
buf.append(" order by ");
queryCondition.beginFindOrder();
QueryOrder order;
while ((order = queryCondition.findNextOrder()) != null) {
String name = order.getName();
String dir = order.getDir();
// try {
// Method method = DaoHelper.getMethod(name, clazz);
// } catch (SecurityException e) {
// logger.warn("给定的排序字段[" + name + "]在[" + clazz.getName() + "]类中不允许操作!");
// continue;
// } catch (NoSuchMethodException e) {
// logger.warn("给定的排序字段[" + name + "]在[" + clazz.getName() + "]类中不存在!");
// continue;
// }
buf.append(name).append(" ").append(dir).append(", ");
}
if (", ".equals(buf.substring(buf.length() - 2)))
buf = buf.delete(buf.length() - 2, buf.length());
else
buf = buf.delete(buf.length() - 10, buf.length());
return buf.toString();
}
public List executeFind(final String hql) {
return executeFind(hql, null, null, 0, 0);
}
public List executeFind(final String hql, final Object[] args, final Type[] types) {
return executeFind(hql, args, types, 0, 0);
}
public List executeFind(final String hql, final int pageNo, final int pageSize) {
return executeFind(hql, null, null, pageNo, pageSize);
}
public List executeFind(final String hql, final Object[] args, final Type[] types, final int pageNo, final int pageSize) {
return getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Query query = session.createQuery(hql);
if ( args != null ) {
for ( int i = 0; i < args.length; i++ ) {
Object arg = args[i];
Type type = null;
if ( types != null && i < types.length ) {
type = types[i];
}
if ( type == null ) {
query.setParameter(i, arg);
} else {
query.setParameter(i, arg, type);
}
}
}
if (pageSize > 0) {
int offset = (pageNo - 1) * pageSize;
int limit = pageSize;
query.setFirstResult(offset);
query.setMaxResults(limit);
if ( logger.isDebugEnabled() ) {
logger.debug("查询结果实现翻页,每页" + pageSize + "条记录,翻页到第" + pageNo + "页...");
}
}
List list = query.list();
if (list == null) list = new ArrayList();
session.flush();
session.clear();
return list;
}
});
}
public Object findUniqueResult(final String hql) {
return findUniqueResult(hql, null, null);
}
public Object findUniqueResult(final String hql, final Object[] args, final Type[] types) {
return getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Query query = session.createQuery(hql);
if ( args != null ) {
for ( int i = 0; i < args.length; i++ ) {
Object arg = args[i];
Type type = null;
if ( types != null && i < types.length ) {
type = types[i];
}
if ( type == null ) {
query.setParameter(i, arg);
} else {
query.setParameter(i, arg, type);
}
}
}
query.setMaxResults(1);
return query.uniqueResult();
}
});
}
public void executeSQLUpdate(final String sql) throws Exception {
executeSQLUpdate(sql, null, null);
}
public void executeSQLUpdate(final String sql, final Object[] args, final String[] types) throws Exception {
Session session = getSession();
Connection connection = session.connection();
PreparedStatement pstmt = null;
try {
pstmt = connection.prepareStatement(sql);
if ( args != null ) {
for ( int i = 0; i < args.length; i++ ) {
Object arg = args[i];
String type = null;
if ( types != null && i < types.length ) {
type = types[i];
}
int index = i + 1;
if ("String".equals(type)) {
pstmt.setString(index, (String)arg);
} else if ("long".equals(type) || "Long".equals(type)) {
pstmt.setLong(index, ((Long)arg).longValue());
} else if ("int".equals(type) || "Integer".equals(type)) {
pstmt.setInt(index, ((Integer)arg).intValue());
} else if ("double".equals(type) || "Double".equals(type)) {
pstmt.setDouble(index, ((Double)arg).doubleValue());
} else if ("float".equals(type) || "Float".equals(type)) {
pstmt.setFloat(index, ((Float)arg).floatValue());
} else if ("Date".equals(type)) {
java.sql.Date date = null;
if (arg instanceof java.util.Date) {
date = new java.sql.Date(((java.util.Date)arg).getTime());
} else if (arg instanceof java.sql.Date)
date = (java.sql.Date)arg;
else
date = new java.sql.Date(new SimpleDateFormat("yyyy-MM-dd").parse((String)arg).getTime());
pstmt.setDate(index, date);
} else if ("Time".equals(type)) {
pstmt.setTime(index, (Time)arg);
} else if ("Timestamp".equals(type)) {
pstmt.setTimestamp(index, (Timestamp)arg);
}
}
}
pstmt.executeUpdate();
} catch(Exception e) {
throw e;
} finally {
try {
if (pstmt != null) pstmt.close();
} catch (Exception e) { }
}
}
public ResultSet executeSQLQuery(String sql) {
return executeSQLQuery(sql, 0, 0);
}
public ResultSet executeSQLQuery(String sql, final int pageNo, final int pageSize) {
logger.debug("executeSQLQuery - [" + sql + "]...");
if (pageSize > 0)
logger.debug("每页" + pageSize + "条,翻页到第" + pageNo + "页");
Connection conn = this.getSession().connection();
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY );
if (pageSize > 0) {
int offset = (pageNo - 1) * pageSize;
int maxRows = offset + pageSize;
pstmt.setMaxRows(maxRows);
rs = pstmt.executeQuery();
rs.absolute(offset);
} else
rs = pstmt.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("Execute retrieve sql " + sql + " failed");
}
return rs;
}
public long executeSQLCount(String sql) {
int index = sql.indexOf("order by");
if (index >= 0)
sql = sql.substring(0, index);
sql = "select count(0) from (" + sql + ") mytable";
if ( logger.isDebugEnabled() ) {
logger.debug("executeSQLCount - [" + sql + "]...");
}
Connection conn = this.getSession().connection();
PreparedStatement pstmt = null;
ResultSet rs = null;
long count = 0;
try {
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
if(rs.next())
count = rs.getLong(1);
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("Execute retrieve sql " + sql + " failed");
}
return count;
}
public List ResultSet2List(ResultSet rs, TransitionCallback callback) {
List list = new ArrayList();
try {
while (rs.next()) {
Object obj = callback.transition(rs);
list.add(obj);
}
} catch (SQLException e) {
throw new RuntimeException( "SQLException Exception encountered", e);
}
return list;
}
public List ResultSet2List(ResultSet rs) {
int columnCount = 0;
String[] columnNames = null;
try {
ResultSetMetaData resultSetMetaData = rs.getMetaData();
columnCount = resultSetMetaData.getColumnCount();
columnNames = new String[columnCount+1];
for (int i = 1; i <= columnCount; i++) {
columnNames[i] = resultSetMetaData.getColumnName(i);
}
} catch (SQLException e) {
throw new RuntimeException( "SQLException Exception encountered", e);
}
List list = new ArrayList();
try {
while (rs.next()) {
Map map = new HashMap();
for (int i = 1; i <= columnCount; i++)
map.put(columnNames[i], rs.getObject(i));
list.add(map);
}
} catch (SQLException e2) {
throw new RuntimeException( "SQLException Exception encountered", e2);
}
return list;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -