BaseDao:
package cn.itcast.oa.base; import java.util.List; /** * baseDao 公共借口 * @author Administrator * * @param <T> */ public interface BaseDao<T> { /** * 保存实体 * @param entity */ void save(T entity); /** * 删除实体 * @param id */ void delete(Long id); /** * 更新实体 * @param entity */ void update(T entity); /** * 根据id查找 * @param id * @return */ T getById(Long id); /** * 根据ID查询 * @param ids * @return */ List<T> findByIds(Long[] ids); /** * 查询所有的集合 * @return */ List<T> findAll(); } |
BaseDaoImpl
package cn.itcast.oa.base; import java.lang.reflect.ParameterizedType; import java.util.List; import javax.annotation.Resource; import org.hibernate.*; /** * DAO公告实现类 * * @author Administrator * * @param <E> */ @SuppressWarnings("unchecked") public class BaseDaoImpl<E> implements BaseDao<E> { @Resource private SessionFactory sessionFactory; private Class<E> clazz ; //构造方法 public BaseDaoImpl(){ //使用反射得到类型 ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();//获取当前new对象的泛型对象父类 this.clazz =(Class<E>) pt.getActualTypeArguments()[0] ;//获取第一个类型的真实类型 } /** * 获得当前的可用session * @return */ protected Session getSession() { return sessionFactory.getCurrentSession(); } @Override public void save(E entity) { getSession().save(entity); } @Override public void delete(Long id) { Object obj = getById(id); if (obj != null) { getSession().delete(obj); } } @Override public void update(E entity) { getSession().update(entity); } @Override public E getById(Long id) { if (id ==null) { return null; }else { return (E) getSession().get(clazz, id); } } @Override public List<E> findByIds(Long[] ids) { return getSession().createQuery(// "FROM " + clazz.getSimpleName()+"WHERE id IN(:ids)")// .setParameterList("ids", ids) .list(); } @Override public List<E> findAll() { return getSession().createQuery(// "FROM " + clazz.getSimpleName())// .list(); } } |
DepartmentAction类
package cn.itcast.oa.view; import java.util.List; import javax.annotation.Resource; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import cn.itcast.oa.domain.Department; import cn.itcast.oa.domain.PageBean; import cn.itcast.oa.domain.Role; import cn.itcast.oa.service.DepartmentService; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; @Controller @Scope("prototype") public class DepartmentAction extends ActionSupport implements ModelDriven<Department> { @Resource private DepartmentService departmentService; private Long parentId; private int pageNum = 1;// 当前页 private int pageSize = 2;// 每页显示多少条数据 private Department department = new Department(); @Override public Department getModel() { return department; } /** * 列表 * * @return * @throws Exception */ public String list() throws Exception { List<Department> departmentList = null; if (parentId == null) { departmentList = departmentService.findTopList(); } else { departmentList = departmentService.findChildList(parentId); Department parent = departmentService.findById(parentId); } // List<Department> departmentList = departmentService.findAll(); ActionContext.getContext().put("departmentList", departmentList); return "list"; } /** * 删除 * * @return * @throws Exception */ public String delete() throws Exception { departmentService.delete(department.getId()); return "toList"; } /** * 增加页面 * * @return * @throws Exception */ public String addUI() throws Exception { // 显示所有部门(准备数据,department列表) List<Department> departmentList = departmentService.findAll(); // 放到Map中 ActionContext.getContext().put("departmentList", departmentList); return "saveDepartmentUI"; } /** * 添加 * * @return * @throws Exception */ public String add() throws Exception { // 根据ID查询部门信息 Department parent = departmentService.findById(parentId); // 设置上级部门信息 department.setParent(parent); // 添加部门信息 departmentService.add(department); return "toList"; } /** * 修改页面 * * @return * @throws Exception */ public String editorUI() throws Exception { // 显示所有部门(准备数据,department列表) List<Department> departmentList = departmentService.findAll(); // 放到Map中 ActionContext.getContext().put("departmentList", departmentList); // 准备回显数据 Department departmentResult = departmentService.findById(department.getId()); ActionContext.getContext().getValueStack().push(departmentResult); // 给所属部门设值回显(相对应的属性有值) if (departmentResult.getParent() != null) { parentId = departmentResult.getParent().getId(); } return "saveDepartmentUI"; } /** * 修改 * * @return * @throws Exception */ public String editor() throws Exception { // 取出原对象 Department departmentResult = departmentService.findById(department.getId()); // 更新到原对象信息 departmentResult.setName(department.getName()); departmentResult.setDescription(department.getDescription()); departmentResult.setParent(departmentService.findById(parentId));// 设置上级部门 // 修改 departmentService.update(departmentResult); return "toList"; } /** * 分页 * * @return */ public String showBypage() throws Exception { //List<Department> departmentList =departmentService.findAll(); Department department =null; PageBean pageBean = departmentService.getPageBeanByDepartment(pageNum,pageSize,department); ActionContext.getContext().getValueStack().push(pageBean); return "showBypage"; } public Long getParentId() { return parentId; } public void setParentId(Long parentId) { this.parentId = parentId; } public int getPageNum() { return pageNum; } public void setPageNum(int pageNum) { this.pageNum = pageNum; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } } |
DepartmentService
package cn.itcast.oa.service; import java.util.List; import javax.annotation.Resource; import cn.itcast.oa.dao.DepartmentDao; import cn.itcast.oa.domain.Department; import cn.itcast.oa.domain.PageBean; public interface DepartmentService { /** * 查询所有 * @return */ List<Department> findAll(); /** * 删除 * @param id */ void delete(Long id); /** * 添加 * @param role */ void add(Department department); /** * 根据ID查找 * @param id * @return */ Department findById(Long id); /** * 修改 * @param role */ void update(Department department); /** * 查找顶级部门 * @return */ List<Department> findTopList(); /** * 查找子部门 * @param parentId * @return */ List<Department> findChildList(Long parentId); /** * 根据条件封装pageBean * @param pageNum * @param pageSize * @param departmentResult * @return */ PageBean getPageBeanByDepartment(int pageNum, int pageSize, Department departmentResult); } |
DepartmentServiceImpl
package cn.itcast.oa.service.impl; import java.util.List; import javax.annotation.Resource; import org.hibernate.SessionFactory; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.sun.org.apache.bcel.internal.generic.GETSTATIC; import cn.itcast.oa.dao.DepartmentDao; import cn.itcast.oa.domain.Department; import cn.itcast.oa.domain.PageBean; import cn.itcast.oa.service.DepartmentService; @Service @Transactional @SuppressWarnings("unchecked") public class DepartmentServiceImpl implements DepartmentService{ @Resource private DepartmentDao departmentDao ; @Resource private SessionFactory sessionFactory ; @Override public List<Department> findAll() { // TODO Auto-generated method stub return departmentDao.findAll(); } @Override public void delete(Long id) { departmentDao.delete(id); } @Override public void add(Department department) { departmentDao.save(department); } @Override public Department findById(Long id) { return departmentDao.getById(id); } @Override public void update(Department department) { departmentDao.update(department); } @Override public List<Department> findTopList() { return sessionFactory.getCurrentSession().createQuery("" + "FROM Department d WHERE d.parent IS NULL" + "").list(); } @Override public List<Department> findChildList(Long parentId) { return sessionFactory.getCurrentSession().createQuery("" + "FROM Department d WHERE d.parent.id =?") .setParameter(0, parentId) .list(); } @Override public PageBean getPageBeanByDepartment(int pageNum, int pageSize, Department departmentResult) { List list = sessionFactory.getCurrentSession().createQuery("" + "FROM Department d order BY d.name ASC") .setFirstResult((pageNum-1)*pageSize) .setMaxResults(pageSize) .list(); Long count =(Long)sessionFactory.getCurrentSession().createQuery("" + "SELECT COUNT(*) FROM Department d ").uniqueResult(); return new PageBean(pageNum, pageSize, count.intValue(), list); } } |
DepartmentDao
package cn.itcast.oa.dao; import java.util.List; import cn.itcast.oa.base.BaseDao; import cn.itcast.oa.domain.Department; public interface DepartmentDao extends BaseDao<Department> { }
DepartmentDaoImpl
package cn.itcast.oa.dao.impl; import org.springframework.stereotype.Repository; import cn.itcast.oa.base.BaseDaoImpl; import cn.itcast.oa.dao.DepartmentDao; import cn.itcast.oa.domain.Department; @Repository public class DepartmentDaoImpl extends BaseDaoImpl<Department> implements DepartmentDao { }
Department.java
package cn.itcast.oa.domain; import java.util.HashSet; import java.util.Set; /** * 部门 * * @author tyg * */ public class Department { private Long id; private Set<User> users = new HashSet<User>(); private Department parent; private Set<Department> children = new HashSet<Department>(); private String name; private String description; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public Set<User> getUsers() { return users; } public void setUsers(Set<User> users) { this.users = users; } public Department getParent() { return parent; } public void setParent(Department parent) { this.parent = parent; } public Set<Department> getChildren() { return children; } public void setChildren(Set<Department> children) { this.children = children; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } }
Department.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.itcast.oa.domain"> <class name="Department" table="t_department"> <id name="id"> <generator class="native" /> </id> <property name="name" /> <property name="description" /> <!-- users属性 ,本类与User 一对多 --> <set name="users"> <key column="departmentId"></key> <one-to-many class="User" /> </set> <!-- parent属性 ,本类与Department(上级)多对一 --> <many-to-one name="parent" class="Department" column="parentId" lazy="false"></many-to-one> <!-- children属性 ,本类与Department(下级)一对多 级联删除--> <set name="children" cascade="delete" lazy="false"> <key column="parentId"></key> <one-to-many class="Department" /> </set> </class> </hibernate-mapping>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!-- 配置为开发模式 --> <constant name="struts.devMode" value="true" /> <!-- 把扩展名配置为action --> <constant name="struts.action.extension" value="action" /> <!-- 把主题设置为simple --> <constant name="struts.ui.theme" value="simple" /> <package name="default" namespace="/" extends="struts-default"> <!-- 部门管理 --> <action name="department_*" class="departmentAction" method="{1}"> <result name="list"> /WEB-INF/jsp/departmentAction/list.jsp</result> <result name="showBypage"> /WEB-INF/jsp/departmentAction/list.jsp</result> <result name="saveDepartmentUI"> /WEB-INF/jsp/departmentAction/saveDepartmentUI.jsp</result> <result name="toList" type="redirectAction">department_showBypage?parentId=${parentId}</result> </action> </package> </struts>