JavaWeb各层次设计

作者: 键盘瞎 | 来源:发表于2017-02-27 15:29 被阅读258次

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";
    }

}

相关文章

  • JavaWeb各层次设计

    JavaWeb 各个层次简化设计 UML类图 Dao层 BaseDao接口 BaseDao实现类BaseDaoIm...

  • 2019-06-26

    初创企业如何进行顶层设计 顶层设计是指要以理念一致,从全局视角出发,统筹考虑各层次和各要素,...

  • 手把手搭建Java学生信息管理系统【附源码】(毕设)

    基于JavaWeb的学生信息管理系统的设计与实现 一、系统简介 本课程演示的是一套基于JavaWeb实现的学生信息...

  • 目录【JavaWeb教程】

    JavaWeb-001-JavaWeb简介 JavaWeb-002-JavaWeb开发的目录结构 JavaWeb-...

  • 【顶层设计架构】是什么?

    顶层设计架构是什么? 顶层设计架构是运用系统方法,从全局角度对某项任务或项目的各方面、各层次、各要素统筹规划,以集...

  • 校友录 - 我的毕业设计

    校友录网站。作为毕业设计,为母校设计校友交流平台。基于javaWeb,由Maven构建管理,采用Spring+Sp...

  • 校友录设计

    校友录网站。作为毕业设计,为母校设计校友交流平台。基于javaWeb,由Maven构建管理,采用Spring+Sp...

  • 七、JavaWeb设计模式

    一、jsp设计模式 SUN公司推出JSP技术后,同时也推荐了两种web应用程序的开发模式,一种是JSP+JavaB...

  • JavaWeb权限设计原理

    每个系统都有权限设计,本篇主要将初始的权限设计的原理,不依赖任何框架,以直观的角度剖析web的权限设计。 权限设计...

  • JavaWeb MVC设计模式

    开发模式 1.C/S 客户端/服务器,需要安装客户端软件,称为胖客户端 2.B/S 浏览器/服务器,只要有浏览器即...

网友评论

    本文标题:JavaWeb各层次设计

    本文链接:https://www.haomeiwen.com/subject/ksyzwttx.html