Action代码: package org.niit.sshpopedom.web.action; public interface ISSHPopedomAction { String TO_DEPART_LIST = "toDepartList"; String TO_EMP_LIST = "toEmpList"; String DEPART_LIST = "departList"; String EMP_LIST = "empList"; } package org.niit.sshpopedom.web.action; import java.io.IOException; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import org.apache.struts2.convention.annotation.Result; import org.apache.struts2.convention.annotation.ResultPath; import org.niit.sshpopedom.biz.IDepartBiz; import org.niit.sshpopedom.entities.Depart; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; @Scope("prototype") @Controller("departService") public class DepartAction extends ActionSupport implements ISSHPopedomAction { @Resource(name="departBiz") private IDepartBiz departBiz; public void setDepartBiz(IDepartBiz departBiz) { this.departBiz = departBiz; } public String toList(){ // TODO Auto-generated method stub //查询所有员工 ServletActionContext.getRequest().setAttribute(DEPART_LIST,departBiz.getDeparts()); return TO_DEPART_LIST; } public String toAjajxList() throws IOException{ java.util.List<Depart> dlist = departBiz.getDeparts(); HttpServletResponse response = ServletActionContext.getResponse(); response.setCharacterEncoding("UTF-8"); response.setContentType("text/plain; charset=UTF-8"); response.getWriter().write(getDepartsJson(dlist)); response.getWriter().flush(); response.getWriter().close(); return null; } private String getDepartsJson(java.util.List<Depart> dlist){ StringBuilder departBuilder = new StringBuilder(); if (dlist!=null && dlist.size()==0){ return null; } departBuilder.append("["); for (Depart depart : dlist) { departBuilder.append("{"); departBuilder.append("did:"+depart.getDid()+","); departBuilder.append("dname:'"+depart.getDname()+"',"); departBuilder.append("dcreate:'"+depart.getDcreatetime().toLocaleString()+"'"); departBuilder.append("},"); } //去掉末尾逗号 if (departBuilder.length()>1){ departBuilder.deleteCharAt(departBuilder.length()-1); } departBuilder.append("]"); return departBuilder.toString(); } } package org.niit.sshpopedom.web.action; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import org.niit.sshpopedom.biz.IEmpBiz; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import com.opensymphony.xwork2.ActionSupport; @Scope("prototype") @Controller("empService") public class EmpAction extends ActionSupport implements ISSHPopedomAction { @Resource(name="empBiz") private IEmpBiz empBiz; public void setEmpBiz(IEmpBiz empBiz) { this.empBiz = empBiz; } public String toList(){ // TODO Auto-generated method stub //查询所有员工 ServletActionContext.getRequest().setAttribute(EMP_LIST,empBiz.getEmps()); return TO_EMP_LIST; } } 业务层代码: package org.niit.sshpopedom.biz.impl; import java.util.List; import javax.annotation.Resource; import org.niit.sshpopedom.biz.IDepartBiz; import org.niit.sshpopedom.dao.DepartDao; import org.niit.sshpopedom.entities.Depart; import org.springframework.stereotype.Service; @Service("departBiz") public class DepartBiz implements IDepartBiz { @Resource(name="departDao") private DepartDao departDao; public void setDepartDao(DepartDao departDao) { this.departDao = departDao; } @Override public List<Depart> getDeparts() { // TODO Auto-generated method stub return departDao.getAll(Depart.class); } @Override public void addDepart(Depart... departs) { // TODO Auto-generated method stub if (departs!=null){ for (Depart depart : departs) { departDao.save(depart); } } } } package org.niit.sshpopedom.biz.impl; import java.util.List; import javax.annotation.Resource; import org.niit.sshpopedom.biz.IEmpBiz; import org.niit.sshpopedom.dao.EmpDao; import org.niit.sshpopedom.entities.Emp; import org.springframework.stereotype.Service; @Service("empBiz") public class EmpBiz implements IEmpBiz { @Resource(name="empDao") private EmpDao empDao; public void setEmpDao(EmpDao empDao) { this.empDao = empDao; } @Override public List<Emp> getEmps() { // TODO Auto-generated method stub return empDao.getAll(Emp.class); } @Override public void addEmps(Emp... emps) { if (emps!=null){ for (Emp emp : emps) { empDao.save(emp); } } } } 持久层代码: package org.niit.sshpopedom.dao; import java.io.Serializable; import java.sql.SQLException; import java.util.List; import javax.annotation.Resource; import org.hibernate.Criteria; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.criterion.DetachedCriteria; import org.hibernate.criterion.MatchMode; import org.hibernate.criterion.Order; import org.hibernate.criterion.Projections; import org.hibernate.criterion.Restrictions; import org.niit.sshpopedom.entities.pagination.*; import org.springframework.orm.hibernate3.HibernateCallback; import org.springframework.orm.hibernate3.HibernateTemplate; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import org.springframework.stereotype.Repository; /** * 基础Dao类 * */ public class BaseDao implements IBaseDao { @Resource(name = "hibernateTemplate") private HibernateTemplate hibernateTemplate; public void setHibernateTemplate(HibernateTemplate hibernateTemplate) { this.hibernateTemplate = hibernateTemplate; } public HibernateTemplate getHibernateTemplate() { return hibernateTemplate; } public void save(Object entity){ hibernateTemplate.save(entity); } /* (non-Javadoc) * @see org.niit.sshpopedom.dao.IBaseDao#get(java.lang.Class, java.io.Serializable) */ public Object get(Class clazz, Serializable oid) { return hibernateTemplate.get(clazz, oid); } /* (non-Javadoc) * @see org.niit.sshpopedom.dao.IBaseDao#getAll(java.lang.Class) */ public List getAll(Class clazz) { return hibernateTemplate.find("from " + clazz.getName()); } /* (non-Javadoc) * @see org.niit.sshpopedom.dao.IBaseDao#remove(java.lang.Object) */ public void remove(Object entity) { hibernateTemplate.delete(entity); } /* (non-Javadoc) * @see org.niit.sshpopedom.dao.IBaseDao#update(java.lang.Object) */ public void update(Object entity) { hibernateTemplate.update(entity); } /* (non-Javadoc) * @see org.niit.sshpopedom.dao.IBaseDao#getByCondition(java.lang.Class, org.niit.sshpopedom.entities.pagination.PCondition) */ public List getByCondition(Class clazz, PCondition... pConditions) { DetachedCriteria qbc = DetachedCriteria.forClass(clazz); if (pConditions != null && pConditions.length > 0) { for (PCondition pcd : pConditions) { switch (pcd.getOpt()) { case PEQ: qbc.add(Restrictions.eq(pcd.getPropertyName(), pcd .getPropertyValue())); break; case PGT: qbc.add(Restrictions.gt(pcd.getPropertyName(), pcd .getPropertyValue())); break; case PNE: qbc.add(Restrictions.ne(pcd.getPropertyName(), pcd .getPropertyValue())); break; case PLT: qbc.add(Restrictions.lt(pcd.getPropertyName(), pcd .getPropertyValue())); break; case PLE: qbc.add(Restrictions.le(pcd.getPropertyName(), pcd .getPropertyValue())); break; case PLIKE: qbc .add(Restrictions.like(pcd.getPropertyName(), pcd .getPropertyValue() == null ? "" : pcd .getPropertyValue().toString(), MatchMode.ANYWHERE)); break; default: break; } } } return hibernateTemplate.findByCriteria(qbc); } /** * 预处理条件 * */ private void prepareCondition(Criteria qbc, PageInfo pi) { if (pi.getPconditionList() != null && pi.getPconditionList().size() > 0) { for (PCondition pcd : pi.getPconditionList()) { switch (pcd.getOpt()) { case PEQ: qbc.add(Restrictions.eq(pcd.getPropertyName(), pcd .getPropertyValue())); break; case PGT: qbc.add(Restrictions.gt(pcd.getPropertyName(), pcd .getPropertyValue())); break; case PNE: qbc.add(Restrictions.ne(pcd.getPropertyName(), pcd .getPropertyValue())); break; case PLT: qbc.add(Restrictions.lt(pcd.getPropertyName(), pcd .getPropertyValue())); break; case PLE: qbc.add(Restrictions.le(pcd.getPropertyName(), pcd .getPropertyValue())); break; case PLIKE: qbc .add(Restrictions.like(pcd.getPropertyName(), pcd .getPropertyValue() == null ? "" : pcd .getPropertyValue().toString(), MatchMode.ANYWHERE)); break; default: break; } } } } /** * 预处理排序 * */ private void PrepareOrderField(Criteria qbc, PageInfo pi) { if (qbc == null || pi == null || pi.getPorderLlist() == null || pi.getPorderLlist().size() == 0) { return; } for (POrder orderfield : pi.getPorderLlist()) { switch (orderfield.getPdirect()) { case ASC: qbc.addOrder(Order.asc(orderfield.getPropertyName())); break; case DESC: qbc.addOrder(Order.desc(orderfield.getPropertyName())); break; default: break; } } } /* (non-Javadoc) * @see org.niit.sshpopedom.dao.IBaseDao#pager(org.niit.sshpopedom.entities.pagination.PageInfo) */ public void pager(final PageInfo pi) { getHibernateTemplate().execute(new HibernateCallback() { @Override public Object doInHibernate(Session session) throws HibernateException, SQLException { try { Criteria qbc = session.createCriteria(pi.getClazz()); qbc.setProjection(Projections.rowCount());// 设置投影【查询总条数】 prepareCondition(qbc, pi);// 处理条件 pi.setRecordCount(Integer.parseInt(qbc.uniqueResult() .toString()));// 得到总条数 pi .setPageCount(pi.getRecordCount() % pi.getPageSize() == 0 ? pi .getRecordCount() / pi.getPageSize() : pi.getRecordCount() / pi.getPageSize() + 1);// 计算总页数 qbc.setProjection(null);// 清空投影设置 // 开始分页 PrepareOrderField(qbc, pi);// 处理排序 qbc.setFirstResult((pi.getPageIndex() - 1) * pi.getPageSize());// 设置第一条索引 qbc.setMaxResults(pi.getPageSize());// 设置要显示的条数 pi.setResult(qbc.list()); } catch (Exception e) { e.printStackTrace(); } finally { session.close(); } return null; } }); } } package org.niit.sshpopedom.dao; import javax.annotation.Resource; import org.springframework.stereotype.Repository; /** * 部门Dao * */ @Repository("departDao") public class DepartDao extends BaseDao { } package org.niit.sshpopedom.dao; import org.springframework.stereotype.Repository; /** * 员工Dao * */ @Repository("empDao") public class EmpDao extends BaseDao { }