JavaWeb各层次设计

JavaWeb 各个层次简化设计

UML类图

UML类图

Dao层

BaseDao接口

package cn.zzuli.oa.base;

import java.util.List;

/**
 * Dao层的基类
 * @author LZH
 * @date 2017年2月25日
 * @param <T> 泛型 获取实体类
 */
public interface BaseDao<T> {
    
    /**
     * 保存实体
     * @param entity
     */
    void save(T entity);
    
    /**
     * 删除实体
     * @param id
     */
    void delete(Long id);
    
    /**
     * 更新实体
     * @param entity
     */
    void update(T entity);
    
    /**
     * 
     * @param id
     * @return
     */
    T getById(Long id);
    
    /**
     * 查询实体
     * @param ids id的集合
     * @return
     */
    List<T> listByIds(Long[] ids);
    
    /**
     * 查询所有
     * @return
     */
    List<T> listAll();
    
}

BaseDao实现类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层的实现类
 * 
 * @author LZH
 * @date 2017年2月25日
 * @param <T>
 *            泛型 用于获取实体类
 */
@Transactional // @Transactional可以被继承,即对子类也有效
@SuppressWarnings("unchecked")
public abstract class BaseDaoImpl<T> implements BaseDao<T> {

    @Resource
    private SessionFactory sessionFactory;

    protected Class<T> clazz;

    public BaseDaoImpl() {
        // 通过反射得到T的真实类型
        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();
    }

    /**
     * 获取当前可用的Session
     * 
     * @return 当前可用的Session
     */
    protected Session getSession() {
        return sessionFactory.getCurrentSession();
    }

}

Service业务层设计

各个模块接口,在每个接口中,可以写每个模块所特有的方法,以供调用实现。这里面由于继承了BaseDaoImpl可是直接调用Session来执行操作数据,这样就可以写一些每个模块里面所独有的方法。

RoleService

package cn.zzuli.oa.service;

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

/**
 * 岗位业务层接口
 *   可以写自己模块所特有的方法
 * @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实现类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;

/**
 * 岗位业务层实现类
 * 
 * @author LZH
 * @date 2017年2月25日
 */
@Service
public class RoleServiceImpl extends BaseDaoImpl<Role> implements RoleService {

}

UserService实现类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 UserServiceImpl extends BaseDaoImpl<User> implements UserService {
    
}

Action层设计

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;

    //填写各个Service的实现类,方便在控制层中调用各个不同之间的使用
    @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的实例 通过反射实例
            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") 这两个注解不能写到父类中,否则就失效了
 * @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);//放到对象栈的栈顶
        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";
    }

}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,373评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,732评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 107,163评论 0 238
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,700评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,036评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,425评论 1 211
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,737评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,421评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,141评论 1 239
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,398评论 2 243
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,908评论 1 257
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,276评论 2 251
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,907评论 3 233
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,018评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,772评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,448评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,325评论 2 261

推荐阅读更多精彩内容