1. 程式人生 > >SSH案例----員工管理系統

SSH案例----員工管理系統

上一次說道SSH框架整合,這次來談談剛學的一個小專案,就是實現一個對員工和部門的管理系統。

1. 實現步驟

(1). 準備工作

  • 需求分析
  • 建立專案的目錄結構
  • 建立實體類和對映
  • 將提供的html檔案轉成jsp檔案,並引入到專案
    這裡寫圖片描述

(2). 實現登陸功能

  • 在Spring,struts2的核心配置檔案中配置相應的Action,service,Dao層資訊
  • 實現登陸功能,並新增校驗

(3). 實現部門管理功能

  • 在Spring,struts2的核心配置檔案中配置相應的Action,service,Dao層資訊
  • 編寫Action層呼叫Service層,Service層呼叫Dao層的方法,並回顯到jsp頁面。通過將值儲存到Session中ActionContext.getContext().getSession().put("existEmployee", existEmployee);
    或者儲存到值棧中ActionContext.getContext().getValueStack().push(pageBean); ,對應地再在jsp中通過<s:property value="#session.existEmployee.ename"/> 或者
<s:iterator value="list" var="d">
    <tr>
    <td align="center"><s:property value="#d.dname"/></td>
    <td align="center">
<a href="${pageContext.request.contextPath}/department_edit.action?did=<s:property value="#d.did"/>">編輯</a></td> <td align="center"><a href="${pageContext.request.contextPath}/department_delete.action?did=<s:property value="#d.did"/>">刪除</a></td> </tr
>
</s:iterator>

把一個物件新增到值棧中,在jsp頁面,可以直接訪問到其屬性,如上述的list就是pageBean的一個屬性。

(4). 實現員工管理功能

步驟與(3)相似,但此時因為員工與管理部門通過外來鍵關聯了,所以增刪改查時要注意:

  • 刪除時:要實現級聯刪除,一種方法是用Hibernate的delete方法刪除,這時需要設定cascade屬性,並且在刪除部門時要先查詢獲得該部門的完整資訊
/*
     * delete
     */
    public String delete() {
        department = departmentService.findById(department.getDid());

        //departmentService.delete(department.getDid());
        /*
         * 傳遞一個物件的方式刪除可以級聯刪除
         */
        departmentService.delete(department);

        return "delete";
    }

並設定cascade屬性

<!-- 關聯關係對映 -->
        <set name="employees" cascade="all" inverse="false">
            <!-- 這個外來鍵名稱是自己指定的,Hibernate預設會為我們建立 -->
            <key column="dno"></key>
            <one-to-many class="entity.Employee"/>
        </set>

另外一種方法可以通過HQL語句刪除

int did = department.getDid();
        String hql = "delete from Department d where d.did = ?";
        Query q = this.getSessionFactory().openSession().createQuery(hql);
        q.setInteger(0, did);
        q.executeUpdate();

這時會遇到一些報錯資訊,可參見 Hibernate刪除時的級聯問題
- 修改部門後,員工部分沒有了部門資訊:原因可能是Hibernate設定了雙向關聯,可以在一段設定inverse屬性為true即可。
- 查詢不到外來鍵關聯的內容,即查詢員工時查不到部門,這時應該設定lazy屬性為false

2. 原始碼

applicationContext.xml:

    <!-- 引入外部的屬性檔案 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!-- 配置連線池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="jdbcUrl" value="${jdbc.url}" />
        <property name="user" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>

    <!-- 配置Hibernate的相關的屬性 -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <!-- 注入連線池 -->
        <property name="dataSource" ref="dataSource"></property>

        <!-- 配置Hibernate的一些屬性 -->
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>

        <!-- 載入Hibernate中的對映 -->
        <property name="mappingResources">
            <list>
                <value>entity/Department.hbm.xml</value>
                <value>entity/Employee.hbm.xml</value>
            </list>
        </property>

    </bean>


    <!-- 配置Action類 -->
    <bean id="employeeAction" class="action.EmployeeAction" scope="prototype">
        <property name="employeeService" ref="employeeService"></property>
        <property name="departmentService" ref="departmentService"></property>
    </bean>

    <bean id="departmentAction" class="action.DepartmentAction" scope="prototype">
        <property name="departmentService" ref="departmentService"></property>
    </bean>


    <!-- 配置業務層的類 -->
    <bean id="employeeService" class="service.impl.EmployeeServiceImpl">
        <property name="employeeDao" ref="employeeDao"></property>
    </bean>

    <bean id="departmentService" class="service.impl.DepartmentServiceImpl">
        <property name="departmentDao" ref="departmentDao"></property>
    </bean>


    <!-- 配置DAO的類 -->
    <bean id="departmentDao" class="dao.impl.DepartmentDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <bean id="employeeDao" class="dao.impl.EmployeeDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>



    <!-- 配置事務管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <!-- 開啟註解事務 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>


</beans>

Struts2.xml:

<struts>        

    <package name="ssh" namespace="/" extends="struts-default">
        <action name="employee_*" class="employeeAction" method="{1}">
            <result name="input">/index.jsp</result>
            <result name="success" type="redirect">/frame.jsp</result>
            <result name="findAll">/jsp2/list.jsp</result>
            <result name="add">/jsp2/add.jsp</result>
            <result name="save" type="redirect">employee_findAll.action</result>
            <result name="edit">/jsp2/edit.jsp</result>
            <result name="update" type="redirect">employee_findAll.action</result>
            <result name="delete" type="redirect">employee_findAll.action</result>
        </action>

        <action name="department_*" class="departmentAction" method="{1}">
            <result name="findAll">/jsp/list.jsp</result>
            <result name="add">/jsp/add.jsp</result>
            <result name="save" type="redirect">department_findAll.action</result>
            <result name="edit">/jsp/edit.jsp</result>
            <result name="update" type="redirect">department_findAll.action</result>
            <result name="delete" type="redirect">department_findAll.action</result>
        </action>

    </package>  

</struts>

查詢所有內容的顯示頁面例項代表:

<table cellspacing="0" border="1" class="table1">
<thead>
   <tr><th width="450">部門名稱</th><th  width="450">編輯</th><th  width="450">刪除</th></tr>
</thead>

<tbody>
<s:iterator value="list" var="d">
    <tr>
    <td align="center"><s:property value="#d.dname"/></td>
    <td align="center"><a href="${pageContext.request.contextPath}/department_edit.action?did=<s:property value="#d.did"/>">編輯</a></td>
    <td align="center"><a href="${pageContext.request.contextPath}/department_delete.action?did=<s:property value="#d.did"/>">刪除</a></td>
    </tr>
</s:iterator>
</tbody>

</table>
<br/>


<table border="0" cellspacing="0" cellpadding="0"  width="900px">
<tr>
<td align="right">
   <span><s:property value="currPage"/>/<s:property value="totalPage"/></span>
   <span>
       <s:if test="currPage != 1">
           <a href="${pageContext.request.contextPath}/department_findAll.action?currPage=1">[首頁]</a>&nbsp;&nbsp;
           <a href="${pageContext.request.contextPath}/department_findAll.action?currPage=<s:property value="currPage-1"/>">[上一頁]</a>&nbsp;&nbsp;
       </s:if>
       <s:if test="currPage != totalPage">
           <a href="${pageContext.request.contextPath}/department_findAll.action?currPage=<s:property value="currPage+1"/>">[下一頁]</a>&nbsp;&nbsp;
           <a href="${pageContext.request.contextPath}/department_findAll.action?currPage=<s:property value="totalPage"/>">[尾頁]</a>&nbsp;&nbsp;
       </s:if>
   </span>
   <span><s:property value="myTest"/></span>
</td>
</tr>
</table>

等有時間了傳到GitHub上