040905

角色的增删改查



nsfw.role.dao


RoleDao extends BaseDao<Role>




dao.impl


RoleDaoImpl extends BaseDaoImpl<Role>



nsfw.role.service


RoleService 


实现基础方法


增删改查5方法


service.impl


@Service(“roleService”)

public classRoleServiceImpl implements



@Resource

private RoleDao roleDao;


下面写action

role.action


RoleAction extends BaseAction

实现七个方法

保存新增 编辑两个 删除两个 查找一个


UserAction全部复制过来,修改



配置文件



role.conf


role-spring.xml

role-struts.xml



user替换成Role




struts.xml 

包含角色管理的struts的配置文件

<include file=“cn/itcast/nsfw/role/conf/role-struts.xml”>


引入前台页面




/nsfw/role_addUI.action



复选框标签

checkBoxList标签


<s:checkboxlist list=“#privilegeMap” name=“privilegeIds”></>


后台定义一个数组来接收




public String addUI(){

ActionContext.getContext().getContextMap().put(“privilegeMap”,Constant.PIRVILEGE_MAP);


Constant.PIRVILEGE_MAP 常量类




}



ActionContext级别是request级别



后台接收权限




@Resource

…..

private String[] privilegeIds;


setget




<set name=“rolePrivileges”inverse=“true” lazy=“true” cascade=”save-update,delete”>




public String add(){

。。。。


if(role!=null)

{

//处理权限保存

if(privilegeIds!=null){

HashSet<RolePrivilege> set=new HashSet<RolePrivilege>();

for(int i=0;i<privilegeIds.length;i++){

set.add(new RolePrivilege(new RolePrivilegeId(role,privilegeIds[i])));

}

role.setRolePrivileges(set);

}

roleService.save(role);

}

。。。。

}


以上要重点看,要学会!!!!


编辑方法也类似




editUI .jsp


没有回显



public String editUI(){

ActionContext.getCOntext().getContextMap().put(“privilegeMap”,Constant.PRIVILEGE_MAP);

if(role!=null && role.getRoleId()!=null){

role=roleService.findObjectById(role.getRoleId());

//处理权限回显

if(role.getRolePrivileges()!=null){

privilegeIds=new String[role.getRolePrivileges().size()];

int i=0;

for(RolePrivilege rp:role.getRolePrivileges()){

privilegeIds[i++]=rp.getId().getCode();

}

}

}

}



lazy=“false”



public void update(Role role){

//删除该角色对于所有的权限


roleDao.deleteRolePrivilegeByRoleId(role.getRoleId());

//更新角色及其权限

roleDao.update(role);

}



RoleDaoImpl

public void deleteRolePrivilegeByRoleId(String roleId){

getSession().createQuery(“Delete from RolePrivilege where id.role.roleId=?”)

query.setParameter(0,roleId);

query.executeUpdate();

}




代码如下:


Constant.java

public class Constant {

//系统权限集合

public static String PRIVILEGE_XZGL="xzgl";

public static String PRIVILEGE_HQFW="hqfw";

public static String PRIVILEGE_ZXXX="zxxx";

public static String PRIVILEGE_NSFW="nsfw";

public static String PRIVILEGE_SPACE="spaces";

public static Map<String,String> PRIVILEGE_MAP;

static{

PRIVILEGE_MAP=new HashMap<String,String>();

PRIVILEGE_MAP.put(PRIVILEGE_XZGL, "行政管理");

PRIVILEGE_MAP.put(PRIVILEGE_HQFW, "后勤服务");

PRIVILEGE_MAP.put(PRIVILEGE_ZXXX, "在线学习");

PRIVILEGE_MAP.put(PRIVILEGE_NSFW, "纳税服务");

PRIVILEGE_MAP.put(PRIVILEGE_SPACE, "我的空间");

}

}

Role.java

public class Role implements Serializable{

private String roleId;

private String name;

private String state;

private Set<RolePrivilege> rolePrivileges;

public String ROLE_STATE_VALID = "1";

public String ROLE_STATE_INVALID = "0";

public Role() {

// TODO Auto-generated constructor stub

}


public Role(String roleId, String name, String state,

Set<RolePrivilege> rolePrivileges, String rOLE_STATE_VALID,

String rOLE_STATE_INVALID) {

this.roleId = roleId;

this.name = name;

this.state = state;

this.rolePrivileges = rolePrivileges;

ROLE_STATE_VALID = rOLE_STATE_VALID;

ROLE_STATE_INVALID = rOLE_STATE_INVALID;

}


public String getRoleId() {

return roleId;

}


public void setRoleId(String roleId) {

this.roleId = roleId;

}


public String getName() {

return name;

}


public void setName(String name) {

this.name = name;

}


public String getState() {

return state;

}


public void setState(String state) {

this.state = state;

}


public Set<RolePrivilege> getRolePrivileges() {

return rolePrivileges;

}


public void setRolePrivileges(Set<RolePrivilege> rolePrivileges) {

this.rolePrivileges = rolePrivileges;

}


}


Role.hbm.xml

<hibernate-mapping>

<class name="cn.itcast.nsfw.role.entity.Role" table="role">

<id name="roleId" type="java.lang.String">

<column name="role_id" length="32" />

<generator class="uuid.hex" />

</id>

<property name="name" type="java.lang.String">

<column name="name" length="20" not-null="true" />

</property>

<property name="state" type="java.lang.String">

<column name="state" length="1" />

</property>

<set name="rolePrivileges" inverse="true" lazy="false" cascade="save-update,delete">

<key>

<column name="role_id"></column>

</key>

<one-to-many class="cn.itcast.nsfw.role.entity.RolePrivilege"/>

</set>

</class>

</hibernate-mapping>

RolePrivilege.java

public class RolePrivilege  implements Serializable{

private RolePrivilegeId id;


public RolePrivilegeId getId() {

return id;

}

public void setId(RolePrivilegeId id) {

this.id = id;

}

public RolePrivilege(RolePrivilegeId id) {

this.id = id;

}

public RolePrivilege() {

}

}

RolePrivilege.hbm.xml

<hibernate-mapping>

<class name="cn.itcast.nsfw.role.entity.RolePrivilege" table="role_privilege">

<composite-id name="id" class="cn.itcast.nsfw.role.entity.RolePrivilegeId">

<key-many-to-one name="role" class="cn.itcast.nsfw.role.entity.Role" lazy="false">

<column name="role_id"></column>

</key-many-to-one>

<key-property name="code" type="java.lang.String">

<column name="code" length="20"></column>

</key-property>

</composite-id>

</class>

</hibernate-mapping>

RolePrivilegeId.java

public class RolePrivilegeId  implements Serializable{

private Role role;

private String code;

@Override

public int hashCode() {

final int prime = 31;

int result = 1;

result = prime * result + ((code == null) ? 0 : code.hashCode());

result = prime * result + ((role == null) ? 0 : role.hashCode());

return result;

}

@Override

public boolean equals(Object obj) {

if (this == obj)

return true;

if (obj == null)

return false;

if (getClass() != obj.getClass())

return false;

RolePrivilegeId other = (RolePrivilegeId) obj;

if (code == null) {

if (other.code != null)

return false;

} else if (!code.equals(other.code))

return false;

if (role == null) {

if (other.role != null)

return false;

} else if (!role.equals(other.role))

return false;

return true;

}

public RolePrivilegeId() {

}

public RolePrivilegeId(Role role, String code) {

this.role = role;

this.code = code;

}

public Role getRole() {

return role;

}

public void setRole(Role role) {

this.role = role;

}

public String getCode() {

return code;

}

public void setCode(String code) {

this.code = code;

}

}

RoleDao.java

public interface RoleDao extends BaseDao<Role> {

void deleteRolePrivilegeByRoleId(String roleId);

}

RoleDaoImpl.java

public class RoleDaoImpl extends BaseDaoImpl<Role> implements RoleDao {

@Override

public void deleteRolePrivilegeByRoleId(String roleId) {

// TODO Auto-generated method stub

Query query=getSession().createQuery("DELETE FROM RolePrivilege where id.role.roleId=?")

.setParameter(0, roleId);

query.executeUpdate();

}

}

RoleService.java

public interface RoleService {

/*

* 新增 

*/

public void save(Role role);

/*

* 更新 

*/

public void update(Role role);

/*

* 删除 

*/

public void delete(Serializable id);

/*

* 根据id查找

*/

public Role findObjectById(Serializable id);

/*

* 查找列表

*/

public List<Role> findObjects();

}

RoleServiceImpl.java

@Service("roleService")

public class RoleServiceImpl implements RoleService{

@Resource

private RoleDao roleDao;

@Override

public void save(Role role) {

// TODO Auto-generated method stub

roleDao.save(role);

}

@Override

public void update(Role role) {

// TODO Auto-generated method stub

//1.删除该角色所有对象权限

roleDao.deleteRolePrivilegeByRoleId(role.getRoleId());

//2.更新角色及权限

roleDao.update(role);

}

@Override

public void delete(Serializable id) {

// TODO Auto-generated method stub

roleDao.delete(id);

}

@Override

public Role findObjectById(Serializable id) {

// TODO Auto-generated method stub

return roleDao.findObjectById(id);

}

@Override

public List<Role> findObjects() {

// TODO Auto-generated method stub

return roleDao.findObjects();

}

}

RoleAction.java

public class RoleAction extends BaseAction {

// 调用roleService(注入名称,不能随便改)

@Resource

private RoleService roleService;

private List<Role> roleList;

// 用户信息接收(普通变量,可以随便改)

private Role role;

//用于接收addUI的权限数组

private String[] privilegeIds;


// 调用增删改查方法

// 一个CRUD中包含:

// 列表页面

public String listUI() {

//加载权限集合

ActionContext.getContext().getContextMap().put("privilegeMap", Constant.PRIVILEGE_MAP);

roleList = roleService.findObjects();


return "listUI";

}


// 跳转到新增页面

public String addUI() {

//加载权限集合

ActionContext.getContext().getContextMap().put("privilegeMap", Constant.PRIVILEGE_MAP);

return "addUI";

}


// 保存新增

public String add() {

if (role != null) {

if(privilegeIds!=null){

System.out.println("不为空");

HashSet<RolePrivilege> set=new HashSet<RolePrivilege>();

for(int i=0;i<privilegeIds.length;i++){

//???????

set.add(new RolePrivilege(new RolePrivilegeId(role, privilegeIds[i])));

}

role.setRolePrivileges(set);

}

roleService.save(role);

}

return "list";

}


/**

* 跳转到编辑页面

* */

public String editUI() {

//加载权限集合

ActionContext.getContext().getContextMap().put("privilegeMap", Constant.PRIVILEGE_MAP);

if (role != null && role.getRoleId() != null) {

role = roleService.findObjectById(role.getRoleId());

//如果set集合为空,初始化数组,长度为set集合的长度

if(role.getRolePrivileges()!=null){

privilegeIds=new String[role.getRolePrivileges().size()];

int i=0;

for(RolePrivilege rp:role.getRolePrivileges()){

privilegeIds[i++]=rp.getId().getCode();

}

}

}

return "editUI";

}


// 保存编辑

public String edit() {

if (role != null) {

if(privilegeIds!=null){

System.out.println("不为空");

HashSet<RolePrivilege> set=new HashSet<RolePrivilege>();

for(int i=0;i<privilegeIds.length;i++){

//???????

set.add(new RolePrivilege(new RolePrivilegeId(role, privilegeIds[i])));

}

role.setRolePrivileges(set);

}

roleService.update(role);

}

return "list";

}


// 删除

public String delete() {

if (role != null && role.getRoleId() != null) {

System.out.println(role.getRoleId());

roleService.delete(role.getRoleId());

}

return "list";

}


// 批量删除

public String deleteSelected() {

if (selectedRow != null) {

for (String id : selectedRow) {

roleService.delete(id);

}

}

return "list";

}


public List<Role> getRoleList() {

return roleList;

}


public void setRoleList(List<Role> roleList) {

this.roleList = roleList;

}


public void setRole(Role role) {

this.role = role;

}


public Role getRole() {

return role;

}

public String[] getPrivilegeIds() {

return privilegeIds;

}


public void setPrivilegeIds(String[] privilegeIds) {

this.privilegeIds = privilegeIds;

}

}

role-spring.xml

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    

    <bean id="roleDao" class="cn.itcast.nsfw.role.dao.impl.RoleDaoImpl" parent="baseDao"></bean>

    

    <!-- 扫描service -->

    <context:component-scan base-package="cn.itcast.nsfw.role.service.impl"></context:component-scan>

</beans>

role-struts.xml

<struts>

<package name="role-action" namespace="/nsfw" extends="base-default">

<action name="role_*" class="cn.itcast.nsfw.role.action.RoleAction" method="{1}">

<result name="{1}">/WEB-INF/jsp/nsfw/role/{1}.jsp</result>

<result name="list" type="redirectAction">

<param name="actionName">role_listUI</param>

</result>

</action>

</package>


</struts>


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值