納稅服務系統【角色與用戶】

用戶與角色之間的關係

咱們在作用戶模塊的時候,漏掉了最後一個功能。在新增功能中是能夠選擇角色的。java

這裏寫圖片描述

用戶與角色之間的關係也是多對多數組

  • 一個用戶對應多個角色
  • 一個角色能夠被多個用戶使用。

這裏寫圖片描述

如今呢,咱們的用戶表已是寫的了。咱們最好就不要修改原有的用戶表數據。那咱們在不修改用戶表代碼的狀況下,又怎麼來實現多對多呢??服務器

跟角色與權限是同樣的。使用中間表來維護它們的關係就好了。markdown

用戶:user
    用戶id,名稱...
    1      用戶1
    2      用戶2

    用戶角色:user_role
    用戶id,角色id
    1       1
    1       2
    2       2


    角色:role
    角色Id,名稱
    1      管理員
    2      通常用戶

設計中間表

public class UserRole implements Serializable {

    private UserRoleId userRoleId;

    public UserRoleId getUserRoleId() {
        return userRoleId;
    }

    public void setUserRoleId(UserRoleId userRoleId) {
        this.userRoleId = userRoleId;
    }
}

主鍵表

public class UserRoleId implements Serializable {

    private String user_id;

    //在使用的時候,Role相關的數據會用得特別多。爲了方便使用了Role對象。而user就不須要使用User對象了。
    private Role role;


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        UserRoleId that = (UserRoleId) o;

        if (user_id != null ? !user_id.equals(that.user_id) : that.user_id != null) return false;
        return role != null ? role.equals(that.role) : that.role == null;

    }

    @Override
    public int hashCode() {
        int result = user_id != null ? user_id.hashCode() : 0;
        result = 31 * result + (role != null ? role.hashCode() : 0);
        return result;
    }

    public String getUser_id() {
        return user_id;
    }

    public void setUser_id(String user_id) {
        this.user_id = user_id;
    }

    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }
}

映射文件

<?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

    <hibernate-mapping>
        <class name="zhongfucheng.user.entity.UserRole" table="user_role">
            <composite-id name="userRoleId" class="zhongfucheng.user.entity.UserRoleId">

                <!--manytoone能夠生成外鍵字段。-->
                <key-many-to-one name="role" class="zhongfucheng.role.entity.Role" column="role_id" lazy="false"/>

                <key-property name="user_id" column="user_id" type="java.lang.String"/>
            </composite-id>
        </class>

    </hibernate-mapping>

增長模塊

在跳轉到JSP頁面的前,把全部的角色找出來。放到request域對象中,讓JSP頁面顯示出來。app

public String addUI() {

        //把全部的角色查詢出來,帶過去給JSP頁面顯示
        ActionContext.getContext().getContextMap().put("roleList", roleServiceImpl.findObjects());

        return "addUI";
    }
 <%-- list是集合對象 name是要帶給服務器端的字符串數組。 listkey 是集合元素對象的id listValue 是集合元素對象的名字 --%> <s:checkboxlist list="#roleList" name="userRoleIds" listKey="roleId" listValue="name"/> 

這裏寫圖片描述


編輯模塊

編輯回顯數據

在編輯模塊中,須要將該用戶所擁有的角色查詢出來。而後把查詢出來的id值放到數組中dom

public String editUI() {

        //把全部的角色查詢出來,帶過去給JSP頁面顯示
        ActionContext.getContext().getContextMap().put("roleList", roleServiceImpl.findObjects());

        //外邊已經傳了id過來了,咱們要找到id對應的User
        if (user != null &&user.getId() != null  ) {
            //直接獲取出來,後面JSP會根據User有getter就能讀取對應的信息!
            user = userServiceImpl.findObjectById(user.getId());

            //經過用戶的id獲得所擁有UserRole
            List<UserRole> roles = userServiceImpl.findRoleById(user.getId());
            //把用戶擁有角色的id填充到數組中,數組最後回顯到JSP頁面
            int i=0;
            userRoleIds =  new String[roles.size()];
            for (UserRole role : roles) {
                userRoleIds[i++] = role.getUserRoleId().getRole().getRoleId();
            }

        }
        return "editUI";
    }

JSP經過checkboxlist進行回顯,指定了name值就可以自動斷定咱們的用戶擁有的角色是什麼了。ide

<s:checkboxlist list="#roleList" name="userRoleIds" listKey="roleId" listValue="name"></s:checkboxlist>

這裏寫圖片描述


處理編輯操做

在更新以前,首先刪除用戶與角色之間的關係【歷史遺留問題】,若是不刪除,那麼用戶所擁有的角色就一直保留着。不管你在JSP頁面有沒有勾選。this

public String edit() throws IOException {
        //Struts2會自動把JSP帶過來的數據封裝到User對象上
        if (user.getId() != null && user != null) {
            if (headImg != null) {

                //獲得要把頭像上傳到服務器的路徑
                javax.servlet.ServletContext servletContext = ServletActionContext.getServletContext();
                String realPath = servletContext.getRealPath("upload/user");
                //因爲用戶上傳的名字可能會相同,若是相同就被覆蓋掉,所以咱們要修改上傳文件的名字【獨一無二】
                headImgFileName = UUID.randomUUID().toString() + headImgFileName.substring(headImgFileName.lastIndexOf("."));
                FileUtils.copyFile(headImg, new File(realPath, headImgFileName));

                //設置圖片與用戶的關係
                user.setHeadImg(headImgFileName);
            }
            if (userRoleIds != null) {

                //刪除用戶與角色之間的關係【歷史遺留問題】
                userServiceImpl.deleteUserRoleById(userRoleIds);
                //保存用戶與角色。
                userServiceImpl.saveUserAndRole(user,userRoleIds);
            }
        }
        return "list";
    }

調用保存用戶與角色的關係。若是id不是爲空的,那麼就執行更新,若是id爲空,就執行保存。spa

@Override
    public void saveUserAndRole(User user, String... userRoleIds) {

        //保存或更新用戶
        if (user.getId() != null) {
            userDaoImpl.update(user);
        } else {
            userDaoImpl.save(user);
        }
        //判斷有沒有把id帶過來
        if (userRoleIds != null) {
            for (String userRoleId : userRoleIds) {
                System.out.println(userRoleId);
                userDaoImpl.saveUserRole(new UserRole(new UserRoleId(user.getId(), new Role(userRoleId))));
            }

        }
    }
相關文章
相關標籤/搜索