本文主要是介绍在项目中,控制权限保存时,如果多次修改权限,该如何写?,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在项目中,控制权限保存时,如果多次修改权限,该如何写?
错误代码:
package cn.itcast.crm.service.impl;import java.util.List;import javax.annotation.Resource;import org.apache.commons.lang.xwork.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;import cn.itcast.crm.dao.ISysPopedomPrivilegeDao;
import cn.itcast.crm.domain.SysPopedomPrivilege;
import cn.itcast.crm.domain.SysPopedomPrivilegeId;
import cn.itcast.crm.service.ISysPopedomPrivilegeService;@Transactional(readOnly=true)
@Service(ISysPopedomPrivilegeService.SERVICE_NAME)
public class SysPopedomPrivilegeServiceImpl implements ISysPopedomPrivilegeService {@Resource(name=ISysPopedomPrivilegeDao.SERVICE_NAME)private ISysPopedomPrivilegeDao sysPopedomPrivilegeDao;@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false)public void updatePopedom(String roleId, String[] popedomModules) {//保存该权限组对应的所有的权限if(StringUtils.isNotBlank(roleId)&&popedomModules!=null&&popedomModules.length>0){for(int i=0;i<popedomModules.length;i++){if(StringUtils.isNotBlank(popedomModules[i])){String[] str=popedomModules[i].split(",");SysPopedomPrivilege sysPopedomPrivilege=new SysPopedomPrivilege();SysPopedomPrivilegeId id = new SysPopedomPrivilegeId();id.setRoleId(roleId);id.setPopedomModule(str[0]);id.setPopedomPrivilege(str[1]);sysPopedomPrivilege.setId(id);sysPopedomPrivilegeDao.save(sysPopedomPrivilege);}}}}}<span style="color:#ff0000;">
</span>
错误提示:
正确代码:
package cn.itcast.crm.service.impl;import java.util.List;import javax.annotation.Resource;import org.apache.commons.lang.xwork.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;import cn.itcast.crm.dao.ISysPopedomPrivilegeDao;
import cn.itcast.crm.domain.SysPopedomPrivilege;
import cn.itcast.crm.domain.SysPopedomPrivilegeId;
import cn.itcast.crm.service.ISysPopedomPrivilegeService;@Transactional(readOnly=true)
@Service(ISysPopedomPrivilegeService.SERVICE_NAME)
public class SysPopedomPrivilegeServiceImpl implements ISysPopedomPrivilegeService {@Resource(name=ISysPopedomPrivilegeDao.SERVICE_NAME)private ISysPopedomPrivilegeDao sysPopedomPrivilegeDao;@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false)public void updatePopedom(String roleId, String[] popedomModules) {//删除该权限组对应的所有权限if(StringUtils.isNotBlank(roleId)&&popedomModules!=null&&popedomModules.length>0){String whereHql=" and o.id.roleId=?";Object[] params={roleId};List<SysPopedomPrivilege> list=sysPopedomPrivilegeDao.findObjectByConditionWithNoPage(whereHql, params);sysPopedomPrivilegeDao.deleteAllObjects(list);}//保存该权限组对应的所有的权限if(StringUtils.isNotBlank(roleId)&&popedomModules!=null&&popedomModules.length>0){for(int i=0;i<popedomModules.length;i++){if(StringUtils.isNotBlank(popedomModules[i])){String[] str=popedomModules[i].split(",");SysPopedomPrivilege sysPopedomPrivilege=new SysPopedomPrivilege();SysPopedomPrivilegeId id = new SysPopedomPrivilegeId();id.setRoleId(roleId);id.setPopedomModule(str[0]);id.setPopedomPrivilege(str[1]);sysPopedomPrivilege.setId(id);sysPopedomPrivilegeDao.save(sysPopedomPrivilege);}}}}}
应该先
删除该权限组对应的所有权限,然后再保存该权限组对应的所有的权限。
这篇关于在项目中,控制权限保存时,如果多次修改权限,该如何写?的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!