下面的是java类代码,用法与 上上篇的代码一样。
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Set;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
/**
* hibernate分页
* @author Administrator
*
*/
public class YCFHPage extends HibernateDaoSupport{
/**
* 返回JSON数据
* 第一条为页面对象
* @param hql
* 如from table
* @param currpag
* 当前页
* @param page
* 每页显示数目
* @return
* 返回字符串
*/
public String executeQuery(String hql,int currpag,int page){
JSONArray jsonEmployeeArray = new JSONArray();
SessionFactory sessionFactory = this.getHibernateTemplate().getSessionFactory();
Session session = sessionFactory.openSession();
if(page<=0){
page=10;
}
try {
Query query = session.createQuery(hql);
int counts= query.list().size();
int pages=(counts-1)/page+1;
try {
pages = (counts-1)/page+1;
} catch (Exception ex) {
pages = 0;
}
if(currpag>pages)
currpag=pages;
else if(currpag<=0)
currpag=1;
JSONObject jsObject = new JSONObject();
jsObject.put("page", page);
jsObject.put("sql", hql);
jsObject.put("currpag", currpag);
jsObject.put("counts", counts);
jsObject.put("pages", pages);
jsonEmployeeArray.put(jsObject);
query.setFirstResult((currpag-1)*page);
query.setMaxResults(page);
query.getNamedParameters();
List tmp = query.list();
for (Object o : tmp) {
JSONObject json=object2JSONObject(o);
jsonEmployeeArray.put(json);
}
} catch (Exception e) {
// TODO: handle exception
}
return jsonEmployeeArray.toString();
}
/**
* 将PO转成JSONObject对象
* @param o
* @return
*/
private JSONObject object2JSONObject(Object o){
JSONObject jsObject = new JSONObject();
Method[] methods = o.getClass().getMethods();
for (int i = 0; i < methods.length; i++) {
Method method = (Method) methods[i];
if (method.getName().startsWith("get")) {
String pName=getMethodName(method.getName());
Object obj = null;
try {
obj=method.invoke(o, null);
if (obj instanceof Set) {
jsObject.put(pName, object2JSONArray(obj));
}
else
jsObject.put(pName, obj);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return jsObject;
}
/**
* 将set转成JSONArray对象
* @param o
* @return
*/
private JSONArray object2JSONArray(Object o){
Set set = (Set) o;
JSONArray jsonEmployeeArray = new JSONArray();
for (Object object : set) {
JSONObject json=object2JSONObject(object);
jsonEmployeeArray.put(json);
}
return jsonEmployeeArray;
}
/**
* 获得PO属性名
* @param mName
* @return
*/
private String getMethodName(String mName){
String s = "";
s = mName.substring(3, 4).toLowerCase() + mName.substring(4);
return s;
}
/**
* 返回第一页数据
* @param sql
* 如 from table
* @param page
* 每页记录数
* @return
* JSON数据
*/
public String firstExecute(String hql,int page){
return executeQuery(hql, 1, page);
}
}