JavaWeb 各個(gè)層次簡(jiǎn)化設(shè)計(jì)
UML類圖
UML類圖
Dao層
BaseDao接口
package cn.zzuli.oa.base;
import java.util.List;
/**
* Dao層的基類
* @author LZH
* @date 2017年2月25日
* @param <T> 泛型 獲取實(shí)體類
*/
public interface BaseDao<T> {
/**
* 保存實(shí)體
* @param entity
*/
void save(T entity);
/**
* 刪除實(shí)體
* @param id
*/
void delete(Long id);
/**
* 更新實(shí)體
* @param entity
*/
void update(T entity);
/**
*
* @param id
* @return
*/
T getById(Long id);
/**
* 查詢實(shí)體
* @param ids id的集合
* @return
*/
List<T> listByIds(Long[] ids);
/**
* 查詢所有
* @return
*/
List<T> listAll();
}
BaseDao實(shí)現(xiàn)類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層的實(shí)現(xiàn)類
*
* @author LZH
* @date 2017年2月25日
* @param <T>
* 泛型 用于獲取實(shí)體類
*/
@Transactional // @Transactional可以被繼承,即對(duì)子類也有效
@SuppressWarnings("unchecked")
public abstract class BaseDaoImpl<T> implements BaseDao<T> {
@Resource
private SessionFactory sessionFactory;
protected Class<T> clazz;
public BaseDaoImpl() {
// 通過反射得到T的真實(shí)類型
ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();
this.clazz = (Class<T>) pt.getActualTypeArguments()[0]; // 獲取T的類型
}
@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();
}
/**
* 獲取當(dāng)前可用的Session
*
* @return 當(dāng)前可用的Session
*/
protected Session getSession() {
return sessionFactory.getCurrentSession();
}
}
Service業(yè)務(wù)層設(shè)計(jì)
各個(gè)模塊接口延赌,在每個(gè)接口中背零,可以寫每個(gè)模塊所特有的方法党瓮,以供調(diào)用實(shí)現(xiàn)丢间。這里面由于繼承了BaseDaoImpl可是直接調(diào)用Session來執(zhí)行操作數(shù)據(jù),這樣就可以寫一些每個(gè)模塊里面所獨(dú)有的方法倡勇。
RoleService
package cn.zzuli.oa.service;
import cn.zzuli.oa.base.BaseDao;
import cn.zzuli.oa.domain.Role;
/**
* 崗位業(yè)務(wù)層接口
* 可以寫自己模塊所特有的方法
* @author LZH
* @date 2017年2月25日
*/
public interface RoleService extends BaseDao<Role>{
}
UserService
package cn.zzuli.oa.service;
import cn.zzuli.oa.base.BaseDao;
import cn.zzuli.oa.domain.User;
public interface UserService extends BaseDao<User>{
}
RoleService實(shí)現(xiàn)類RoleServiceImpl
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;
/**
* 崗位業(yè)務(wù)層實(shí)現(xiàn)類
*
* @author LZH
* @date 2017年2月25日
*/
@Service
public class RoleServiceImpl extends BaseDaoImpl<Role> implements RoleService {
}
UserService實(shí)現(xiàn)類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;
/**
* 用戶業(yè)務(wù)層
*
* @author LZH
* @date 2017年2月26日
*/
@Service
public class UserServiceImpl extends BaseDaoImpl<User> implements UserService {
}
Action層設(shè)計(jì)
BaseAction基類
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;
//填寫各個(gè)Service的實(shí)現(xiàn)類著觉,方便在控制層中調(diào)用各個(gè)不同之間的使用
@Resource
protected RoleService roleService;
@Resource
protected DepartmentService departmentService;
@Resource
protected UserService userSercice;
//.....
protected T model;
@SuppressWarnings({ "unchecked", "rawtypes" })
public BaseAction() {
try {
// 得到model的類型信息
ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();
Class clazz = (Class) pt.getActualTypeArguments()[0];
// 生成model的實(shí)例 通過反射實(shí)例
model = (T) clazz.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public T getModel() {
return model;
}
}
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") 這兩個(gè)注解不能寫到父類中村生,否則就失效了
* @author LZH
* @date 2017年2月25日
*/
@Controller
@Scope("prototype")
public class RoleAction extends BaseAction<Role> {
private static final long serialVersionUID = 1L;
/**
* 列表
*
* @return
* @throws Exception
*/
public String list() throws Exception {
List<Role> roleList = roleService.listAll();
ActionContext.getContext().put("roleList", roleList);
return "list";
}
/**
* 添加
*
* @return
* @throws Exception
*/
public String add() throws Exception {
roleService.save(model);
return "toList";
}
/**
* 刪除
*
* @return
* @throws Exception
*/
public String delete() throws Exception {
roleService.delete(model.getId());
return "toList";
}
/**
* 修改
*
* @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";
}
/**
* 添加頁面
*
* @return
* @throws Exception
*/
public String addUI() throws Exception {
return "addUI";
}
/**
* 修改頁面
*
* @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);//放到對(duì)象棧的棧頂
return "editUI";
}
}
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;
/**
* 列表
*
* @return
* @throws Exception
*/
public String list() throws Exception {
return "list";
}
/**
* 添加
*
* @return
* @throws Exception
*/
public String add() throws Exception {
return "toList";
}
/**
* 添加頁面
*
* @return
* @throws Exception
*/
public String addUI() throws Exception {
return "addUI";
}
/**
* 刪除
*
* @return
* @throws Exception
*/
public String delete() throws Exception {
return "toList";
}
/**
* 修改
*
* @return
* @throws Exception
*/
public String edit() throws Exception {
return "toList";
}
/**
* 修改頁面
*
* @return
* @throws Exception
*/
public String editUI() throws Exception {
return "editUI";
}
/**
* 初始化密碼為1234
*
* @return
* @throws Exception
*/
public String initPassword() throws Exception {
return "toList";
}
}