通用后台管理系统(5)-编写角色接口、实现、控制器、

1.角色接口

package com.sundablog.service.backend.system.upms.role;

import java.util.List;

import com.sundablog.pojo.AdminRole;
import com.sundablog.pojo.AdminRolePermission;
import com.sundablog.result.LayUiResult;

/**
 * 角色接口类
 * @ClassName:  RoleService   
 * @Description:角色接口类   
 * @author: 哒哒 
 * @date:   2018年3月14日 下午6:55:15   
 *     
 * @Copyright: 2018 www.sundablog.com Inc. All rights reserved.
 */
public interface RoleService {
    
    /**
     * 添加一条角色信息
     * @Title: addRole   
     * @Description: 添加一条角色   
     * @param: @param adminRole
     * @param: @return      
     * @return: int      
     * @throws
     */
    int addRole(AdminRole adminRole);
    
    /**
     * 修改一条角色信息
     * @Title: updataRole   
     * @Description: 修改一条角色信息   
     * @param: @param adminRole
     * @param: @return      
     * @return: int      
     * @throws
     */
    int updataRole(AdminRole adminRole);
    
    /**
     * 删除一条角色信息
     * @Title: delectRoleByRoleId   
     * @Description: 删除一条角色信息   
     * @param: @param roleId
     * @param: @return      
     * @return: int      
     * @throws
     */
    int delectRoleByRoleId(int roleId);
    
    /**
     * 根据RoleId查询一条数据
     * @Title: selectRoleByRoleId   
     * @Description: 根据RoleId查询一条数据   
     * @param: @param roleId
     * @param: @return      
     * @return: AdminRole      
     * @throws
     */
    AdminRole selectRoleByRoleId(int roleId);
    /**
     * 分页查询
     * @Title: findPagingQuery   
     * @Description: 分页查询   
     * @param: @param page
     * @param: @param limit
     * @param: @return      
     * @return: LayUiResult      
     * @throws
     */
    LayUiResult findPagingQuery(int page,int limit);
    
    /**
     * 查询全部角色
     * @Title: selectRole   
     * @Description: TODO(这里用一句话描述这个方法的作用)   
     * @param: @return      
     * @return: List      
     * @throws
     */
    List selectRole();
    
    /**
     * 根据角色id查询角色权限关联表
     * @Title: selectRolePermissionByRoleId   
     * @Description: 根据角色id查询角色权限关联表
     * @param: @param roleId
     * @param: @return      
     * @return: List      
     * @throws
     */
    List selectRolePermissionByRoleId(int roleId);
    
    /**
     * 根据角色ID 向权限关联表中添加权限id
     * @Title: addRolePermission   
     * @Description: 根据角色ID 向权限关联表中添加权限id
     * @param: @param roleId
     * @param: @param permissionId
     * @param: @return      
     * @return: int      
     * @throws
     */
    int addRolePermission(int roleId,String permissionId);
    
}

2.角色接口实现

package com.sundablog.service.impl.backend.system.upms.role;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sundablog.mapper.AdminRoleMapper;
import com.sundablog.mapper.AdminRolePermissionMapper;
import com.sundablog.pojo.AdminRole;
import com.sundablog.pojo.AdminRoleExample;
import com.sundablog.pojo.AdminRolePermission;
import com.sundablog.pojo.AdminRolePermissionExample;
import com.sundablog.pojo.AdminRolePermissionExample.Criteria;
import com.sundablog.result.LayUiResult;
import com.sundablog.service.backend.system.upms.role.RoleService;

import cn.hutool.core.util.StrUtil;

/**
 * 角色接口实现类
 * @ClassName:  RoleServiceImpl   
 * @Description:TODO(这里用一句话描述这个类的作用)   
 * @author: 哒哒 
 * @date:   2018年3月14日 下午7:02:07   
 *     
 * @Copyright: 2018 www.sundablog.com Inc. All rights reserved.
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private AdminRoleMapper adminRoleMapper;
    
    @Autowired
    private AdminRolePermissionMapper adminRolePermissionMapper;
    
    /**
     * 添加角色信息
     * Title: addRole   
     * Description:    
     * @param adminRole
     * @return   
     * @see com.sundablog.service.backend.system.upms.role.RoleService#addRole(com.sundablog.pojo.AdminRole)
     */
    @Override
    public int addRole(AdminRole adminRole) {
        
        int i = adminRoleMapper.insertSelective(adminRole);
        return i;
    }

    /**
     * 更新角色信息
     * Title: updataRole   
     * Description:    
     * @param adminRole
     * @return   
     * @see com.sundablog.service.backend.system.upms.role.RoleService#updataRole(com.sundablog.pojo.AdminRole)
     */
    @Override
    public int updataRole(AdminRole adminRole) {
        int i = adminRoleMapper.updateByPrimaryKeySelective(adminRole);
        return i;
    }

    /**
     * 删除角色信息
     * Title: delectRoleByRoleId   
     * Description:    
     * @param roleId
     * @return   
     * @see com.sundablog.service.backend.system.upms.role.RoleService#delectRoleByRoleId(int)
     */
    @Override
    public int delectRoleByRoleId(int roleId) {
        int i = adminRoleMapper.deleteByPrimaryKey(roleId);
        if (i > 0) {
            AdminRolePermissionExample example = new AdminRolePermissionExample();
            Criteria criteria = example.createCriteria();
            criteria.andRoleIdEqualTo(roleId);
            int j = adminRolePermissionMapper.deleteByExample(example);
            if (j  > 0) {
                return j;
            } else {
                return 0;
            }
        } else {
            return 0;
        }
    }

    /**
     * 根据角色id查询角色信息
     * Title: selectRoleByRoleId   
     * Description:    
     * @param roleId
     * @return   
     * @see com.sundablog.service.backend.system.upms.role.RoleService#selectRoleByRoleId(int)
     */
    @Override
    public AdminRole selectRoleByRoleId(int roleId) {
        AdminRole adminRole = adminRoleMapper.selectByPrimaryKey(roleId);
        return adminRole;
    }

    /**
     * 分页查询
     * Title: findPagingQuery   
     * Description:    
     * @param page
     * @param limit
     * @return   
     * @see com.sundablog.service.backend.system.upms.role.RoleService#findPagingQuery(int, int)
     */
    @Override
    public LayUiResult findPagingQuery(int page, int limit) {
        PageHelper.startPage(page, limit);
        AdminRoleExample example = new AdminRoleExample();
        List list = adminRoleMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo<>(list);
        LayUiResult layUiResult = new LayUiResult();
        layUiResult.setCode(0);
        layUiResult.setMsg("");
        layUiResult.setCount(pageInfo.getTotal());
        layUiResult.setData(list);
        return layUiResult;
    }

    /**
     * 根据角色id查询角色权限关联表
     * Title: selectRolePermissionByRoleId   
     * Description:    
     * @param roleId
     * @return   
     * @see com.sundablog.service.backend.system.upms.role.RoleService#selectRolePermissionByRoleId(int)
     */
    @Override
    public List selectRolePermissionByRoleId(int roleId) {
        AdminRolePermissionExample example = new AdminRolePermissionExample();
        Criteria criteria = example.createCriteria();
        criteria.andRoleIdEqualTo(roleId);
        List list = adminRolePermissionMapper.selectByExample(example);
        return list;
    }

    /**
     * 根据角色ID 向权限关联表中添加权限id
     * Title: addRolePermission   
     * Description:    
     * @param roleId
     * @param permissionId
     * @return   
     * @see com.sundablog.service.backend.system.upms.role.RoleService#addRolePermission(int, java.lang.String)
     */
    @Override
    public int addRolePermission(int roleId, String permissionId) {
        //删除角色权限关联表中是数据
        AdminRolePermissionExample example = new AdminRolePermissionExample();
        Criteria criteria = example.createCriteria();
        criteria.andRoleIdEqualTo(roleId);
        int i = adminRolePermissionMapper.deleteByExample(example);
        
        //添加
        List list = StrUtil.split(permissionId, ',');
        try {
            for (String string : list) {
                AdminRolePermission adminRolePermission = new AdminRolePermission();
                adminRolePermission.setRoleId(roleId);
                adminRolePermission.setPermissionId(Integer.valueOf(string));
                adminRolePermissionMapper.insert(adminRolePermission);
            }
            return 1;
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * 查询全部角色
     * Title: selectRole   
     * Description:    
     * @return   
     * @see com.sundablog.service.backend.system.upms.role.RoleService#selectRole()
     */
    @Override
    public List selectRole() {
        AdminRoleExample example = new AdminRoleExample();
        List list = adminRoleMapper.selectByExample(example);
        return list;
    }

}

3.角色控制器

package com.sundablog.controller.backend.system.upms.role;

import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.deser.Deserializers.Base;
import com.sundablog.pojo.AdminRole;
import com.sundablog.pojo.AdminRolePermission;
import com.sundablog.result.BaseResult;
import com.sundablog.result.LayUiResult;
import com.sundablog.service.backend.system.upms.role.RoleService;

import cn.hutool.setting.Setting;

/**
 * 角色权限控制器
 * @ClassName:  RoleController   
 * @Description:角色权限控制器   
 * @author: 哒哒 
 * @date:   2018年3月14日 下午7:10:26   
 *     
 * @Copyright: 2018 www.sundablog.com Inc. All rights reserved.
 */
@Controller
@RequestMapping("/backend/system/upms/role")
public class RoleController {
    
    @Autowired
    private RoleService roleService;

    /**
     * 角色界面显示
     * @Title: index   
     * @Description: 添加界面显示  
     * @param: @return      
     * @return: String      
     * @throws
     */
    @RequiresPermissions("backend_system_upms_role_index")
    @RequestMapping("/index")
    public String index() {
        return "backend/system/upms/role/index";
    }
    
    /**
     * 添加角色界面显示
     * @Title: addIndex   
     * @Description: 添加角色界面显示
     * @param: @return      
     * @return: String      
     * @throws
     */
    @RequiresPermissions("backend_system_upms_role_add")
    @RequestMapping("/add")
    public String addIndex() {
        return "backend/system/upms/role/add";
    }
    
    /**
     * 设置权限界面显示
     * @Title: settingIndex   
     * @Description: TODO(这里用一句话描述这个方法的作用)   
     * @param: @return      
     * @return: String      
     * @throws
     */
    @RequiresPermissions("backend_system_upms_role_setting")
    @RequestMapping("/setting")
    public String settingIndex() {
        return "backend/system/upms/role/setting";
    }
    
    
    /**
     * 删除角色
     * @Title: delectRole   
     * @Description:  删除角色
     * @param: @param roleId
     * @param: @return      
     * @return: BaseResult      
     * @throws
     */
    @RequiresPermissions("backend_system_upms_role_delectRole")
    @RequestMapping("/delectRole")
    @ResponseBody
    public BaseResult delectRole(int roleId) {
        int i = roleService.delectRoleByRoleId(roleId);
        if (i > 0) {
            return BaseResult.build(200, BaseResult.delectSuccess);
        } else {
            return BaseResult.build(201, BaseResult.delectFail);
        }
    }
    
    
    /**
     * 分页查询
     * @Title: findPagingQuery   
     * @Description: 分页查询   
     * @param: @param page
     * @param: @param limit
     * @param: @return      
     * @return: LayUiResult      
     * @throws
     */
    @RequestMapping("/findPagingQuery")
    @ResponseBody
    public LayUiResult findPagingQuery(@RequestParam(value="page", defaultValue="1") int page,@RequestParam(value="limit", defaultValue="10") int limit) {
        LayUiResult result = roleService.findPagingQuery(page, limit);
        return result;
    }
    
    /**
     * 查询全部角色
     * @Title: selectRole   
     * @Description:查询全部角色
     * @param: @return      
     * @return: BaseResult      
     * @throws
     */
    @RequestMapping("/selectRole")
    @ResponseBody
    public BaseResult selectRole() {
        List list = roleService.selectRole();
        return BaseResult.build(200, "", list);
    }
    
    /**
     * 添加角色
     * @Title: addRole   
     * @Description: 添加角色
     * @param: @param adminRole
     * @param: @return      
     * @return: BaseResult      
     * @throws
     */
    @RequestMapping("/addRole")
    @ResponseBody
    public BaseResult addRole(AdminRole adminRole) {
        adminRole.setCtime(new Date());
        int i = roleService.addRole(adminRole);
        if (i > 0) {
            return BaseResult.build(200, BaseResult.addSuccess);
        } else {
            return BaseResult.build(209, BaseResult.addFail);
        }
    }
    
    
    
    /**
     * 根据角色id查询角色权限关联表
     * @Title: selectRolePermissionByRoleId   
     * @Description: 根据角色id查询角色权限关联表
     * @param: @param roleId
     * @param: @return      
     * @return: BaseResult      
     * @throws
     */
    @RequestMapping("/selectRolePermissionByRoleId")
    @ResponseBody
    public BaseResult selectRolePermissionByRoleId(int roleId) {
        List list = roleService.selectRolePermissionByRoleId(roleId);
        return BaseResult.build(200, "", list);
    }
    
    /**
     * 根据角色ID 向权限关联表中添加权限id
     * @Title: addRolePermission   
     * @Description: TODO(这里用一句话描述这个方法的作用)   
     * @param: @param roleId
     * @param: @param permissionId
     * @param: @return      
     * @return: BaseResult      
     * @throws
     */
    @RequestMapping("/addRolePermission")
    @ResponseBody
    public BaseResult addRolePermission(int roleId,String permissionId) {
        int i = roleService.addRolePermission(roleId, permissionId);
        if (i > 0) {
            return BaseResult.build(200, BaseResult.addSuccess);
        } else {
            return BaseResult.build(209, BaseResult.addFail);
        }
    }
    
    
}

你可能感兴趣的:(通用后台管理系统(5)-编写角色接口、实现、控制器、)