jquery flexigrid for java增删查改完整示例--后台

刘焱
2023-12-01
 

新增,修改,删除的就不贴了,那个原来是怎么写就怎么写

DAO层的实现类:

/**
* 根据主键ID查找一条数据
*/
public BlPaper findById(Long id) {
   try {
    BlPaper instance = (BlPaper) getHibernateTemplate().get(
      "net.bolue.drps.vo.BlPaper", id);
    return instance;
   } catch (RuntimeException re) {
    throw re;
   }
}

/**
* 根据条件统计所有数据总数
*/
public Long count(String sql) {
   try {
    String countString = "select count(*) from BlPaper as b where 1=1 "
      + sql;
    List list = getHibernateTemplate().find(countString);
    Iterator iter = list.iterator();
    Long count = 0l;
    while (iter.hasNext()) {
     count = (Long) iter.next();
    }
    return count;
   } catch (RuntimeException re) {
    throw re;
   }
}

/**
* 根据条件查询出数据
*/
public List findAll(String sql, Integer firstResult, Integer maxResults) {
   Session session = getHibernateTemplate().getSessionFactory()
     .getCurrentSession();
   try {
    String queryString = "from BlPaper as b where 1=1 " + sql;
    Query q = session.createQuery(queryString);
    q.setFirstResult(firstResult);
    q.setMaxResults(maxResults);
    return q.list();
   } catch (RuntimeException re) {
    throw re;
   }
}

这没啥说的,和普通的没啥区别

Service层的实现类:

/**
* 根据主键ID查找一条数据
*/
public PrintWriter findById(HttpServletResponse response, Long id)
    throws IOException {
   BlPaper vo = blPaperDAO.findById(id);
   Map map = new HashMap();
   map.put("papId", vo.getPapId());
   map.put("papTitle", vo.getPapTitle());
   map.put("papTitleS", vo.getPapTitleS());
   map.put("papAuthor", vo.getPapAuthor());
   map.put("papKeyword", vo.getPapKeyword());
   map.put("papAbstract", vo.getPapAbstract());
   map.put("papAuthorF", vo.getPapAuthorF());
   map.put("papDepart", vo.getPapDepart());
   map.put("papJourney", vo.getPapJourney());
   map.put("papPubtime", DateUtil.formatDate(vo.getPapPubtime()));
   map.put("papVolum", vo.getPapVolum());
   map.put("papPages", vo.getPapPages());
   map.put("papSupport", vo.getPapSupport());
   map.put("papText", vo.getPapText());
   map.put("papTop", vo.getPapTop());
   map.put("papPublish", vo.getPapPublish());
   map.put("papDownload", vo.getPapDownload());
   map.put("papAddman", vo.getPapAddman());
   map.put("papAddtime", DateUtil.formatDateTime(vo.getPapAddtime()));
  
   JSONObject jsonObject = new JSONObject(map);
   String jsonString = jsonObject.toString();
   return WriterUtil.getJSONString(response, jsonString);
}

/**
* 根据条件查询出数据
*/
public PrintWriter findAll(HttpServletResponse response, String sql,
    Integer page, Integer firstResult, Integer maxResults)
    throws IOException {
   Map pageMap = new HashMap();
   pageMap.put("page", page); //当前页数设置
   pageMap.put("total", blPaperDAO.count(sql)); //总数据量设置
   List list = blPaperDAO.findAll(sql, firstResult, maxResults);
   List mapList = new ArrayList();
   for (int i = 0; i < list.size(); i++) {
    BlPaper vo = (BlPaper) list.get(i);
    StringBuffer ahSb=new StringBuffer();
    ahSb.append("<a href=\"#\" onClick=\"openDetail('");
    ahSb.append(vo.getPapId());
    ahSb.append("')\">");
    ahSb.append(vo.getPapTitle());
    ahSb.append("</a>");
    Map cellMap = new HashMap();
    cellMap.put("id", vo.getPapId()); //注意,这个map的name一定要是id,这个就是flexigrid的行的ID
    cellMap.put("cell", new Object[] { i + 1, ahSb.toString(), vo.getPapAuthor(),
      vo.getPapJourney(), DateUtil.formatDate(vo.getPapPubtime()) });
    mapList.add(cellMap);
   }
   pageMap.put("rows", mapList);
   JSONObject jsonObject = new JSONObject(pageMap);
   String jsonString = jsonObject.toString();
   return WriterUtil.getJSONString(response, jsonString);
}

说明:

1.JSONObject这个是在json.org上面的开源代码,可以下的到,主要是产生JSON格式的数据

2.WriterUtil这个的代码是:

package net.bolue.util;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class WriterUtil {

public static PrintWriter getJSONString(HttpServletResponse response,
    String jsonString) throws IOException {
   response.setContentType("text/html");
   response.setCharacterEncoding("utf-8");
   response.setHeader("Pragma", "no-cache");
   response.setHeader("Cache-Control", "no-cache, must-revalidate");
   response.setHeader("Pragma", "no-cache");
   PrintWriter out = response.getWriter();
   out.write(jsonString);
   out.flush();
   out.close();
   return out;
}

}

Action中的代码,就更容易了

public ActionForward findById(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws IOException {
   Long id = ParserUtil.parseLong(request.getParameter("id"));
   blPaperService.findById(response, id);
   return null;
}

public ActionForward findByAll(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws IOException {
   String papTitle = request.getParameter("papTitle");
   String papTitleS = request.getParameter("papTitleS");
   String papAuthor = request.getParameter("papAuthor");
   String papKeyword = request.getParameter("papKeyword");
   String papAbstract = request.getParameter("papAbstract");
   String papDepart = request.getParameter("papDepart");
   String sortname = request.getParameter("sortname");// 排序字段,flexigrid的参数
   String sortorder = request.getParameter("sortorder");// 排序方式,flexigrid的参数
   Integer page = ParserUtil.parseInt(request.getParameter("page"));// 得到当前页数,flexigrid的参数
   Integer maxResults = ParserUtil.parseInt(request.getParameter("rp"));// 得到每页显示行数,flexigrid的参数
   Integer firstResult = (page - 1) * maxResults;// 计算查询开始数据下标

   StringBuffer sb = new StringBuffer();
   if (papTitle != null && !"".equals(papTitle)) {
    sb.append(" and b.papTitle like '%" + papTitle + "%' ");
   }
   if (papTitleS != null && !"".equals(papTitleS)) {
    sb.append(" and b.papTitleS like '%" + papTitleS + "%' ");
   }
   if (papAuthor != null && !"".equals(papAuthor)) {
    sb.append(" and b.papAuthor like '%" + papAuthor + "%' ");
   }
   if (papKeyword != null && !"".equals(papKeyword)) {
    sb.append(" and b.papKeyword like '%" + papKeyword + "%' ");
   }
   if (papAbstract != null && !"".equals(papAbstract)) {
    sb.append(" and b.papAbstract like '%" + papAbstract + "%' ");
   }
   if (papDepart != null && !"".equals(papDepart)) {
    sb.append(" and b.papDepart like '%" + papDepart + "%' ");
   }
   if (sortname != null && !"".equals(sortname)) {
    sb.append(" order by b." + sortname + " " + sortorder);
   }

   blPaperService.findAll(response, sb.toString(), page, firstResult,
     maxResults);
   return null;
}

这些代码不用解释了,一看就知道了


 类似资料: