java web 层次_JavaWeb各层次设计

JavaWeb 各个层次简化设计

UML类图

51be275287bc0c97d95421c99ceb03f4.png UML类图

Dao层

BaseDao接口

package cn.zzuli.oa.base;

import java.util.List;

/**

* Dao层的基类

* @author LZH

* @date 2017年2月25日

* @param 泛型 获取实体类

*/

public interface BaseDao {

/**

* 保存实体

* @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 listByIds(Long[] ids);

/**

* 查询所有

* @return

*/

List 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

* 泛型 用于获取实体类

*/

@Transactional // @Transactional可以被继承,即对子类也有效

@SuppressWarnings("unchecked")

public abstract class BaseDaoImpl implements BaseDao {

@Resource

private SessionFactory sessionFactory;

protected Class clazz;

public BaseDaoImpl() {

// 通过反射得到T的真实类型

ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();

this.clazz = (Class) 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 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 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{

}

UserService

package cn.zzuli.oa.service;

import cn.zzuli.oa.base.BaseDao;

import cn.zzuli.oa.domain.User;

public interface UserService extends BaseDao{

}

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 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 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 extends ActionSupport implements ModelDriven {

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 {

private static final long serialVersionUID = 1L;

/**

* 列表

*

* @return

* @throws Exception

*/

public String list() throws Exception {

List 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 {

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

}

}

作者:键盘瞎

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java高手真经 - Java Web系统设计与架构 (二) UML: (1)UML样例源文件(8个样例文件) uml/uml.mdl //01.UML快速入门 uml/usecase.mdl //02.用例图 uml/class.mdl //03.静态图——类图、对象图、包图 uml/component.mdl //04.实现图——组件图与部署图 uml/statechart.mdl //05.行为图——状态图 uml/activity.mdl //06.行为图——活动图 uml/sequence.mdl //07.交互图——序列图 uml/collaboration.mdl //08.交互图——协作图 (2)UML上机演练源文件(16个实例文件) uml/usecase_product.mdl //02.用例图——企业产品生产销售管理系统 uml/class_login.mdl //03.类图和包图——企业信息管理系统用户登录和注册模块 uml/component_emis.mdl //04.组件图与部署图——企业信息管理系统 uml/statechart_bug.mdl //05.状态图——Bug管理系统 uml/statechart_atm.mdl //05.状态图——ATM机存取款 uml/statechart_thread.mdl //05.状态图——Java线程类Thread uml/activity_bug.mdl //06.活动图——Bug管理系统 uml/activity_atm.mdl //06.活动图——ATM机存取款 uml/activity_thread.mdl //06.活动图——Java线程类Thread uml/sequence_bug.mdl //07.序列图——Bug管理系统 uml/sequence_atm.mdl //07.序列图——ATM机存取款 uml/sequence_tel.mdl //07.序列图——打电话 uml/collaboration_bug.mdl //08.协作图——Bug管理系统 uml/collaboration_atm.mdl //08.协作图——ATM机存取款 uml/collaboration_tel.mdl //08.协作图——打电话 uml/bug.mdl //09.综合实例——Bug管理系统

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值