跟着汤阳光同志做一个OA系统(四):第一个增删改查(岗位管理)

79 篇文章 1 订阅
35 篇文章 0 订阅

设计实体—》JavaBean—》hbm.xml—》建表,当然还可以反过来做。

在生成sessionFactory的时候就会自动建表。

1,设计实体/表

         设计实体 -->JavaBean --> hbm.xml --> 建表

2,分析有几个功能,对应几个请求。

方式                   请求数量         地址栏

转发                   1                  不变

重定向              2                  变化

•    添加、修改、删除成功后 要重定向到列表功能,这样在刷新页面时才不会出现“又做一次增、删、改”的操作。

•    列表与删除功能都是是一个请求,一个方法

•    添加与修改功能都有两个请求(UI和真正做的),两个方法

•    从一个功能转到另外一个功能都应该用重定向

由上面的分析,增删改查一共6个请求,应该对应6个方法。

作用

方法名

返回值

对应的JSP页面

列表

list()

list

list.jsp

删除

delete()

toList

 

添加页面

addUI()

addUI

addUI.jsp

添加

add()

toList

 

修改页面

editUI()

editUI

editUI.jsp

修改

edit()

toList

 

toList的配置为:type="redirectAction"actionName=“xxAction_list

增删改查所用的6Action方法的模板

/** 列表 */

public Stringlist() throws Exception { return "list"; }

/** 删除 */

public Stringdelete() throws Exception { return "toList"; }

/** 添加页面 */

public StringaddUI() throws Exception { return "saveUI"; }

/** 添加 */

public Stringadd() throws Exception { return "toList"; }

/** 修改页面 */

public StringeditUI() throws Exception { return "saveUI"; }

/** 修改 */

public Stringedit() throws Exception { return "toList"; }

 

请求------------action(控制权限,准备数据)-------------------jspWEB-INF中保护起来)

 

3,实现功能:

         1,写Action类,写Action中的方法,确定Service中的方法。

         2,写Service方法,确定Dao中的方法。

         3,写Dao方法。

         4,写JSP

 

@Action模板

@Controller

@Scope("prototype")

public class RoleActionextends ActionSupport {

    @Resource

    private RoleService roleService;

   

   

    private Long id;

    private String name;

    private String description;

   

    /** 列表 */

    public String list() throws Exception {

       List<Role> roleList = roleService.findAll();

       ActionContext.getContext().put("roleList",roleList);

       return "list";

    }

 

    /** 删除 */

    public String delete() throws Exception {

       roleService.delete(id);

       return "toList";

    }

 

    /** 添加页面 */

    public String addUI() throws Exception {

       return "addUI";

    }

 

    /** 添加 */

    public String add() throws Exception {

       //将数据装入对象中

       Role role = new Role();

       role.setName(name);

       role.setDescription(description);

       //保存

       roleService.save(role);

       return "toList";

    }

 

    /** 修改页面 */

    public String editUI() throws Exception {

       //找到数据库中的对象,准备回显的数据

       Role role= roleService.getById(id);

       //放入值栈

       ActionContext.getContext().getValueStack().push(role);

//     this.name=role.getName();

//     this.description=role.getDescription();

       return "editUI";

    }

 

    /** 修改 */

    public String edit() throws Exception {

       //找到数据库中的对象

       Role role= roleService.getById(id);

       //跟新数据

       role.setName(name);

       role.setDescription(description);

       //保存到数据库

       roleService.update(role);

       return "toList";

    }

 

   

    //------------------------------

    public Long getId() {

       return id;

    }

 

    public void setId(Long id) {

       this.id = id;

    }

 

    public String getName() {

       return name;

    }

 

    public void setName(String name) {

       this.name = name;

    }

 

    public String getDescription() {

       return description;

    }

 

    public void setDescription(String description) {

       this.description = description;

    }

   

}@Struts.xml中配置模板

<action name="role_*"class="roleAction" method="{1}">

           <result name="list">/WEB-INF/jsp/roleAction/list.jsp</result>

           <result name="toList"type="redirectAction">role_list</result>

           <result name="addUI">/WEB-INF/jsp/roleAction/addUI.jsp</result>

           <result name="editUI">/WEB-INF/jsp/roleAction/editUI.jsp</result>

       </action>

@Service类中的模板

@Service

@Transactional

publicclass RoleServiceImpl implements RoleService {

 

    @Resource

    private RoleDao roleDao;

   

    public List<Role> findAll() {

       returnroleDao.findAll();

    }

 

    publicvoid delete(Long id) {

       roleDao.delete(id);

    }

 

    publicvoid save(Role role) {

       roleDao.save(role);

    }

 

    public Role getById(Long id) {

       returnroleDao.getById(id);

    }

 

    publicvoid update(Role role) {

       roleDao.update(role);

    }

 

}

Alt+←/→在窗口间切换

Ctrl+Shift+T打开类型查看器


 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值