UML class diagrams

Dao layer

BaseDao interface

package cn.zzuli.oa.base; import java.util.List; Public interface BaseDao<T> {public interface BaseDao<T> {public interface BaseDao<T> {public interface BaseDao<T> @param entity */ void save(T entity); /** * delete entity * @param id */ void delete(Long id); /** * update entity * @param entity */ void update(T entity); /** * * @param id * @return */ T getById(Long id); /** * query entity * @param ids set * @return */ List<T> listByIds(Long[] ids); @return */ List<T> listAll(); }Copy the code

BaseDao implementation class BaseDaoImpl

package cn.zzuli.oa.base.impl; import java.lang.reflect.ParameterizedType; import java.util.Collections; import java.util.List; import javax.annotation.Resource; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.transaction.annotation.Transactional; import cn.zzuli.oa.base.BaseDao; /** * Dao layer implementation class ** @author LZH * @date 2月25, 2017 * @param <T> * Generics used to get entity classes */ @transactional // @SuppressWarnings("unchecked") public Abstract Class BaseDaoImpl<T> implements BaseDao<T> { @Resource private SessionFactory sessionFactory; protected Class<T> clazz; Public BaseDaoImpl() {ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass(); this.clazz = (Class<T>) pt.getActualTypeArguments()[0]; } @override public void save(T entity) {getSession().save(entity); } @Override public void delete(Long id) { Object obj = getSession().get(clazz, id); getSession().delete(obj); } @Override public void update(T entity) { getSession().update(entity); } @Override public T getById(Long id) { return (T) getSession().get(clazz, id); } @Override public List<T> listByIds(Long[] ids) { if (ids == null || ids.length == 0) { return Collections.EMPTY_LIST; } return getSession().createQuery("FROM " + clazz.getSimpleName() + " WHERE id IN(:ids)") .setParameterList("ids", ids).list(); } @Override public List<T> listAll() { return getSession().createQuery("FROM " + clazz.getSimpleName()).list(); } /** * get the current available Session ** @return Current available Session */ protected Session getSession() {return sessionFactory.getCurrentSession(); }}Copy the code

Service business layer design

Each module interface, within each interface, can write methods specific to each module to be invoked for implementation. BaseDaoImpl calls Session directly to perform operation data, so that you can write some unique methods in each module.

RoleService

package cn.zzuli.oa.service; import cn.zzuli.oa.base.BaseDao; import cn.zzuli.oa.domain.Role; /** * Public interface RoleService extends BaseDao<Role>{}Copy the code

UserService

package cn.zzuli.oa.service;

import cn.zzuli.oa.base.BaseDao;
import cn.zzuli.oa.domain.User;

public interface UserService extends BaseDao<User>{

}Copy the code

RoleService implements the RoleServiceImpl class

package cn.zzuli.oa.service.impl; import org.springframework.stereotype.Service; import cn.zzuli.oa.base.impl.BaseDaoImpl; import cn.zzuli.oa.domain.Role; import cn.zzuli.oa.service.RoleService; /** * @author LZH * @date 2017年2月25日 */ @service public class RoleServiceImpl extends BaseDaoImpl<Role> implements RoleService { }Copy the code

UserService implementation class UserServiceImpl

package cn.zzuli.oa.service.impl; import org.springframework.stereotype.Service; import cn.zzuli.oa.base.impl.BaseDaoImpl; import cn.zzuli.oa.domain.User; import cn.zzuli.oa.service.UserService; /** * @author LZH * @date 2017年2月26日 */ @service public class extends BaseDaoImpl<User> implements UserService { }Copy the code

The Action layer design

BaseAction base class

package cn.zzuli.oa.base; import java.lang.reflect.ParameterizedType; import javax.annotation.Resource; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; import cn.zzuli.oa.service.DepartmentService; import cn.zzuli.oa.service.RoleService; import cn.zzuli.oa.service.UserService; public class BaseAction<T> extends ActionSupport implements ModelDriven<T> { private static final long serialVersionUID = 1L; @resource protected RoleService; @resource protected RoleService; @Resource protected DepartmentService departmentService; @Resource protected UserService userSercice; / /... protected T model; @SuppressWarnings({ "unchecked", "Rawtypes"}) public BaseAction() {ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass(); Class clazz = (Class) pt.getActualTypeArguments()[0]; // Generate an instance of model by reflecting instance model = (T) clazz.newinstance (); } catch (Exception e) { e.printStackTrace(); } } @Override public T getModel() { return model; }}Copy the code

RoleAction

package cn.zzuli.oa.view.action; import java.util.List; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import com.opensymphony.xwork2.ActionContext; import cn.zzuli.oa.base.BaseAction; import cn.zzuli.oa.domain.Role; /** * @controller@scope ("prototype"); * @author * @date 2017年2月25日 */ @controller @Scope("prototype") public Class RoleAction extends BaseAction<Role> { private static final long serialVersionUID = 1L; /** * list ** @return * @throws Exception */ public String list() throws Exception {list <Role> roleList = roleService.listAll(); ActionContext.getContext().put("roleList", roleList); return "list"; } /** * add ** @return * @throws Exception */ public String add() throws Exception {roleService. Save (model); return "toList"; } /** * delete ** @return * @throws Exception */ public String delete() throws Exception { roleService.delete(model.getId()); return "toList"; } /** * modify ** @return * @throws Exception */ public String Edit () throws Exception {Role Role = roleService.getById(model.getId()); role.setName(model.getName()); role.setDescription(model.getDescription()); roleService.update(role); return "toList"; } /** * add page ** @return * @throws Exception */ public String addUI() throws Exception {return "addUI"; } /** * modify the page ** @return * @throws Exception */ public String editUI() throws Exception {Role Role = roleService.getById(model.getId()); model.setName(role.getName()); model.setDescription(role.getDescription()); // ActionContext.getContext().getValueStack().push(role); Return "editUI"; }}Copy the code

UserAction

package cn.zzuli.oa.view.action; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import cn.zzuli.oa.base.BaseAction; import cn.zzuli.oa.domain.User; /** * @author LZH * @date 2017年2月26日 */ @controller @scope ("prototype") public class UserAction extends BaseAction<User> { private static final long serialVersionUID = 1L; /** * list ** @return * @throws Exception */ public String list() throws Exception {return "list"; } /** * add ** @return * @throws Exception */ public String add() throws Exception {return "toList"; } /** * add page ** @return * @throws Exception */ public String addUI() throws Exception {return "addUI"; } /** * delete ** @return * @throws Exception */ public String delete() throws Exception {return "toList"; Throws Exception */ public String edit() throws Exception {return "toList"; } /** * modify the page ** @return * @throws Exception */ public String editUI() throws Exception {return "editUI"; } /** * The initial password is 1234 ** @return * @throws Exception */ public String initPassword() throws Exception {return "toList"; }}Copy the code