zoukankan      html  css  js  c++  java
  • 系统管理模块_岗位管理_实现CRUD功能的具体步骤并设计Role实体

    系统管理模块_岗位管理_实现CRUD功能的具体步骤并设计Role实体

    1,设计实体/表

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

    设计Role实体

     1 public class Role {
     2     private Long id;
     3     private String name;
     4     private String description;
     5     public Long getId() {
     6         return id;
     7     }
     8     public void setId(Long id) {
     9         this.id = id;
    10     }
    11     public String getName() {
    12         return name;
    13     }
    14     public void setName(String name) {
    15         this.name = name;
    16     }
    17     public String getDescription() {
    18         return description;
    19     }
    20     public void setDescription(String description) {
    21         this.description = description;
    22     }        
    23 }

    映射文件

    <hibernate-mapping package="cn.itcast.oa.domain">
        <class name="Role" table="itcast_role">
            <id name="id">
                <generator class="native" />
            </id>
            <property name="name"></property>
            <property name="description"></property>
        </class>
    </hibernate-mapping>

    加到hibernate.cfg.xml配置中,让它知道有这个映射文件才能建表 

    <mapping resource="cn/itcast/oa/domain/Role.hbm.xml" />

    运行测试类,创建SessionFactory时就会创建itcast_role表

        //测试SessionFactory
        @Test
        public void testSessionFactory() throws Exception {
            SessionFactory sessionFactory = (SessionFactory)ac.getBean("sessionFactory");
            System.out.println(sessionFactory);
        } 

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

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

    列表与删除功能都是只有一个请求

    添加与修改功能都是有两个请求

    增删改查共4个功能,6个请求,需要在Action中有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”

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

    ===================================================================

        请求数量  地址栏

    转发    1    不变在一个功能之间的来回跳转

    重定向    2    变化在多个功能之间的跳转

     role_*  --->  {1}代表第一个方法

    *号代表

    role_list      list

    role_addUI     addUI

    role_delete    delete

    3,实现功能:

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

    先完成列表和删除功能

     1 @Controller
     2 @Scope("prototype")
     3 public class RoleAction extends ActionSupport{
     4     //在Action里面要用到Service,用注解@Resource,另外在RoleServiceImpl类上要添加注解@Service
     5     @Resource
     6     private RoleService roleService;
     7     
     8     private Long id;
     9     /**
    10      * 列表
    11      */
    12     public String list() {
    13         List<Role> roleList = roleService.findAll();
    14         ActionContext.getContext().put("roleList", roleList);//用ognl里的#号来获取map的东西
    15         return "list";
    16     }
    17     /**
    18      * 删除
    19      */
    20     public String delete() {
    21         roleService.delete(id);
    22         return "toList";
    23     }
    24     /**
    25      * 添加页面
    26      */
    27     public String addUI() {
    28         return "addUI";
    29     }
    30     /**
    31      * 添加
    32      */
    33     public String add() {
    34         return "toList";
    35     }
    36     /**
    37      * 修改页面
    38      */
    39     public String editUI() {
    40         return "editUI";
    41     }
    42     /**
    43      * 修改
    44      */
    45     public String edit() {
    46         return "toList";
    47     }
    48     //--------------
    49     public Long getId() {
    50         return id;
    51     }
    52     public void setId(Long id) {
    53         this.id = id;
    54     }
    55 }

    struts.xml文件中配置

          <!-- 岗位管理 -->
            <action name="role_*" class="roleAction" method="{1}">
                <result name="list">/WEB-INF/jsp/roleAction/list.jsp</result>
                <result name="addUI">/WEB-INF/jsp/roleAction/addUI.jsp</result>
                <result name="editUI">/WEB-INF/jsp/roleAction/editUI.jsp</result>
                <result name="toList" type="redirectAction">role_list</result>
            </action>

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

    先完成列表和删除功能

    RoleService.java

    //接口中只有方法的声明,没有方法的实现
    public interface RoleService {
        //查询所有
        List<Role> findAll();
        //删除
        void delete(Long id);
    }

    RoleServiceImpl.java

    //在Action中要调用Service,要写下面两个注解
    @Service
    @Transactional    //业务层要管理实务,控制开关事务
    public class RoleServiceImpl implements RoleService{
        //Service里要调用Dao,得到它通过注入
        @Resource
        private RoleDao roleDao;
    
        public List<Role> findAll() {
            return roleDao.findAll();
        }
        public void delete(Long id) {
            roleDao.delete(id);
        }
    }

    3,写Dao方法。

    RoleDao.java

    public interface RoleDao extends BaseDao<Role>{
    }

    列表与删除等公共方法都在继承的BaseDao里有

    RoleDaoImpl.java

    //放到容器里面,以供Service使用Dao的接口与实现类
    @Repository
    public class RoleDaoImpl extends BaseDaoImpl<Role> implements RoleDao{
    }

    4,写JSP

    list.jsp

    <%@ taglib prefix="s" uri="/struts-tags" %><!-- 引入struts标签 -->
    <body>
          <s:iterator value="#roleList"><!-- 得到里面的集合 -->
              <s:property value="id"/>,
              <s:property value="name"/>,
              <s:property value="description"/>,
              <s:a action="role_delete?id=%{id}">删除</s:a>
          </s:iterator>
      </body>

    访问:http://localhost:8080/ItcastOA/role_list.action即可看到列表

    实现添加和修改功能

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

    RoleAction.java

     1 @Controller
     2 @Scope("prototype")
     3 public class RoleAction extends ActionSupport{
     4     //在Action里面要用到Service,用注解@Resource,另外在RoleServiceImpl类上要添加注解@Service
     5     @Resource
     6     private RoleService roleService;
     7     
     8     private Long id;
     9     private String name;
    10     private String description;
    11     /**
    12      * 列表
    13      */
    14     public String list() {
    15         List<Role> roleList = roleService.findAll();
    16         ActionContext.getContext().put("roleList", roleList);//用ognl里的#号来获取map的东西
    17         return "list";
    18     }
    19     
    20     /**
    21      * 删除
    22      */
    23     public String delete() {
    24         roleService.delete(id);
    25         return "toList";
    26     }
    27     /**
    28      * 添加页面
    29      */
    30     public String addUI() {
    31         return "addUI";
    32     }
    33     /**
    34      * 添加
    35      */
    36     public String add() {
    37         //封装到对象中
    38         Role role = new Role();
    39         role.setName(name);//名称和说明怎么得到,跟id一样声明字段,setget方法
    40         role.setDescription(description);
    41         
    42         //保存到数据库中
    43         roleService.save(role);
    44         return "toList";
    45     }
    46     /**
    47      * 修改页面
    48      */
    49     public String editUI() {
    50         //准备回显的数据
    51         Role role =roleService.getById(id);
    52         //ActionContext.getContext().getValueStack().push(role);//放到栈顶
    53         this.name=role.getName();
    54         this.description =role.getDescription();
    55         return "editUI";
    56     }
    57     /**
    58      * 修改
    59      */
    60     public String edit() {
    61         //1.从数据库中获取原对象
    62         Role role = roleService.getById(id);//role是根据id来的
    63         
    64         //2.设置要修改的属性
    65         role.setName(name);
    66         role.setDescription(description);
    67         //3.更新到数据库
    68         roleService.update(role);
    69         
    70         return "toList";
    71     }
    72     //--------------
    73     public Long getId() {
    74         return id;
    75     }
    76     public void setId(Long id) {
    77         this.id = id;
    78     }
    79     public String getName() {
    80         return name;
    81     }
    82     public void setName(String name) {
    83         this.name = name;
    84     }
    85     public String getDescription() {
    86         return description;
    87     }
    88     public void setDescription(String description) {
    89         this.description = description;
    90     }    
    91 }

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

    RoleService.java

    //接口中只有方法的声明,没有方法的实现
    public interface RoleService {
        //查询所有
        List<Role> findAll();
        //删除
        void delete(Long id);
        //保存
        void save(Role role);
        Role getById(Long id);
        //更新
        void update(Role role);
    }

    3,写Dao方法。

    RoleDao.java

    public interface RoleDao extends BaseDao<Role>{
    }

    4,写JSP

    addUI.jsp

      <body>
          <s:form action="role_add"><!-- 提交的地址 -->
              <s:textfield name="name"></s:textfield>
            <s:textarea name="description"></s:textarea>
            <s:submit value="提交"></s:submit>
          </s:form>
      </body>

    editUI.jsp

         <s:form action="role_edit"><!-- 提交的地址 -->
               <s:hidden name="id"></s:hidden><!-- 修改要给出隐藏的id -->
              <s:textfield name="name"></s:textfield>
            <s:textarea name="description"></s:textarea>
            <s:submit value="提交"></s:submit>
          </s:form>

    访问:http://localhost:8080/ItcastOA/role_list.action验证即可

     

     

  • 相关阅读:
    Javascript校验密码复杂度的正则表达式
    Git合并指定文件到另一个分支
    WebForm SignalR 实时消息推送
    基于SignalR的消息推送与二维码描登录实现
    yarn 错误There appears to be trouble with your network connection. Retrying...
    使用SqlConnectionStringBuilder构造数据库连接字符串
    邮箱核心业务领域建模
    C# NuGet常用命令
    阿里P7架构师是如何解决跨域问题的!你有遇到吗?
    WinForm 之 自定义标题栏的窗体移动
  • 原文地址:https://www.cnblogs.com/justdoitba/p/7772201.html
Copyright © 2011-2022 走看看