我将dao层中弄成一个公用类
查询代码如下:
public List findObject(String hql) {
Query q =sessionFactory.getCurrentSession().createQuery(hql);
List list=q.list();
return list;
}
ServiceImpl中代码如下:
public HashMap findTeachers(String teachername, String tnum,int pageIndex, int pageSize, String sortField, String sortOrder) {
//查询表
String hql="from Teacher where 1=1";
//利用工具类判断 如果关键字不为空 则将字段添加到hql语句中
if(com.zw.util.StringUtil.isNullOrEmpty(teachername)==false)
hql+="and teachername like '%"+teachername+"%'";
if(com.zw.util.StringUtil.isNullOrEmpty(tnum)==false)
hql+="and tnum like '%"+tnum+"%'";
//查询条件 以怎样的顺序排列
if(com.zw.util.StringUtil.isNullOrEmpty(sortField)==false){
if("desc".equals(sortOrder)==false) sortOrder="asc";
hql+="order by "+sortField+" "+sortOrder;
}else{
hql+="order by called desc";
}
ArrayList arr=new ArrayList();
List list = baseDao.findObject(hql);
//计算页面中显示的数据从第几位开始,结束第几位
int start = pageSize*pageIndex , end = pageSize+start;
for(int i=0;i<list.size();i++){
//将从数据库获取的教师集合依次取出,transBean2Map()方法将对象的数据以键值对形式储存在map中
Teacher t = (Teacher) list.get(i);
HashMap map = (HashMap) StringUtil.transBean2Map(t);
if(map==null) continue;
if(start<=i&&i<end){
//将map放在一个集合中
arr.add(map);
}
// map.put("createtime", new Timestamp(100, 10, 10, 1, 1, 1, 1));
}
HashMap result = new HashMap();
result.put("data", arr);
result.put("total", list.size());
return result;
}
Action中代码如下:
public void findTeachers(){
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response=ServletActionContext.getResponse();
try {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
//获取关键字key
String teachername=request.getParameter("teachername");
String tnum =request.getParameter("tnum");
//获取当前页和页面显示数据大小
Int pageIndex=Integer.parseInt(request.getParameter("pageIndex"));
int pageSize =Integer.parseInt(request.getParameter("pageSize"));
//字段排序
String sortField = request.getParameter("sortField");
String sortOrder = request.getParameter("sortOrder");
String json="";
HashMap map=teacherService.findTeachers(teachername, tnum, pageIndex, pageSize,sortField,sortOrder);
//将map中的数据转换成JSON格式
json=JSON.Encode(map);
try {
response.getWriter().write(json);
} catch (IOException e) {
e.printStackTrace();
}
}
工具类中:StringUtil:
public static Map<String, Object> transBean2Map(Object obj) {
if(obj == null){
return null;
}
Map<String, Object> map = new HashMap<String, Object>();
try {
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
if (!key.equals("class")) {
Method getter = property.getReadMethod();
Object value = getter.invoke(obj);
map.put(key, value);
}
}
} catch (Exception e) {
System.out.println("transBean2Map Error " + e);
}
return map;
}
JSON类中:
public static String Encode(Object obj) {
if(obj == null || obj.toString().equals("null")) return null;
if(obj != null && obj.getClass() == String.class){
return obj.toString();
}
JSONSerializer serializer = new JSONSerializer();
serializer.transform(new DateTransformer("yyyy-MM-dd'T'HH:mm:ss"), Date.class);
serializer.transform(new DateTransformer("yyyy-MM-dd'T'HH:mm:ss"), Timestamp.class);
return serializer.deepSerialize(obj);
}
效果图:
注意查询时,最重要的是json数据格式要对!不然做的都是无用功。