ssm项目部分分享2,后台代码解析----------------------做一个mis

Controller

package cn.offcn.controller;

import cn.offcn.entity.Role;
import cn.offcn.service.RoleService;
import cn.offcn.utils.OAResult;
import cn.offcn.utils.TableVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private  RoleService roleService;
    @RequestMapping("/{page}")
    public String forwardPage(@PathVariable("page") String page, Integer roleid, Model model){
        //将roleid传到model域中
        model.addAttribute("roleid",roleid);
        return "role/"+page;
    }
    //角色的增加
    @ResponseBody
    @RequestMapping("/saveRole")
    //此处的ids是什么出处
    public OAResult saveRole(Role role,String ids){

        return roleService.saveRole(role,ids);
    }
    //角色列表
    @ResponseBody
    @RequestMapping("/getAllRoles")
    public TableVo<Role> getAllRoles(int page,int limit){

        return roleService.getAllRoles(page,limit);
    }
    //角色的删除
    @ResponseBody
    @RequestMapping("/deleteRoleById")
    public OAResult deleteRoleById(int roleid){

       return roleService.deleteRoleById(roleid);
    }
    //角色的修改

    //查询时不光用role数据,还要角色所拥有的资源

    @ResponseBody
    @RequestMapping("/getRoleByRoleid")
    public Map<String,Object> getRoleByRoleid(int roleid){

        return roleService.getRoleByRoleid(roleid);
    }

    @ResponseBody
    @RequestMapping("/updateRole")
    public OAResult updateRole(Role role,String ids){

        return roleService.updateRole(role,ids);
    }
}

Service

package cn.offcn.service;

import cn.offcn.entity.Role;
import cn.offcn.utils.OAResult;
import cn.offcn.utils.TableVo;

import java.util.Map;

public interface RoleService {


    public OAResult saveRole(Role role, String ids);

    public TableVo<Role> getAllRoles(int page,int limit);

    public OAResult deleteRoleById(int roleid);

    public Map<String, Object> getRoleByRoleid(int roleid);

    public  OAResult updateRole(Role role, String ids);
}

ServiceImpl

package cn.offcn.service.impl;

import cn.offcn.entity.*;
import cn.offcn.mapper.EmpRoleMapper;
import cn.offcn.mapper.RoleMapper;
import cn.offcn.mapper.RoleSourcesMapper;
import cn.offcn.service.RoleService;
import cn.offcn.utils.OAResult;
import cn.offcn.utils.TableVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RoleSourcesMapper roleSourcesMapper;
    @Autowired
    private EmpRoleMapper empRoleMapper;
    //添加角色
    @Override
    public OAResult saveRole(Role role, String ids) {

        //保存角色
       int rows=  roleMapper.insert(role);
       String[] sourcesIds=ids.split(",");
       int count=0;
       for(String sourcesId : sourcesIds){
           RoleSources roleSources=new RoleSources();
           roleSources.setRoleFk(role.getRoleid());
           roleSources.setResourcesFk(Integer.parseInt(sourcesId));
           roleSourcesMapper.insert(roleSources);
           count++;
       }
       if(rows==1 && count==sourcesIds.length){
           return OAResult.ok(200,"操作成功");
       }

        return OAResult.ok(400,"操作失败");
    }
    //角色列表
    public TableVo<Role> getAllRoles(int page,int limit){

        PageHelper.startPage(page,limit);
        RoleExample roleExample=new RoleExample();
        List<Role> roleList=roleMapper.selectByExample(roleExample);
        PageInfo<Role> pageInfo=new PageInfo<>(roleList);
        TableVo<Role> tableVo=new TableVo<>();
        tableVo.setData(pageInfo.getList());
        tableVo.setCode(0);
        tableVo.setCount(pageInfo.getTotal());
        tableVo.setMsg("");
        return tableVo;
    }

    public OAResult deleteRoleById(int roleid){

        //删除role_resours中间表中的数据
        RoleSourcesExample roleSourcesExample=new RoleSourcesExample();
        RoleSourcesExample.Criteria roleSourcesCriteria = roleSourcesExample.createCriteria();
        roleSourcesCriteria.andRoleFkEqualTo(roleid);
        roleSourcesMapper.deleteByExample(roleSourcesExample);

        //删除emp_role中间表中的数据
        EmpRoleExample empRoleExample=new EmpRoleExample();
        EmpRoleExample.Criteria empRoleExampleCriteria = empRoleExample.createCriteria();
        empRoleExampleCriteria.andRoleFkEqualTo(roleid);
        empRoleMapper.deleteByExample(empRoleExample);

        //删除role表中的数据
        int rows= roleMapper.deleteByPrimaryKey(roleid);
        if(rows==1){
            return OAResult.ok(200,"操作成功");
        }
        return OAResult.ok(400,"操作失败");
    }

    public Map<String, Object> getRoleByRoleid(int roleid){
        //查询role对象
        Role role= roleMapper.selectByPrimaryKey(roleid);
        //查询role所拥有的资源
        RoleSourcesExample roleSourcesExample=new RoleSourcesExample();
        RoleSourcesExample.Criteria criteria = roleSourcesExample.createCriteria();
        criteria.andRoleFkEqualTo(roleid);
        List<RoleSources> roleSourcesList = roleSourcesMapper.selectByExample(roleSourcesExample);

        Map<String,Object> map=new HashMap<String,Object>();
        map.put("role",role);
        map.put("roleSourcesList",roleSourcesList);
        return map;
    }

    public  OAResult updateRole(Role role, String ids){

        //更新角色Role
        int rows=roleMapper.updateByPrimaryKey(role);
        //删除当前role的role_sources中间表数据
        RoleSourcesExample roleSourcesExample=new RoleSourcesExample();
        RoleSourcesExample.Criteria roleSourcesExampleCriteria = roleSourcesExample.createCriteria();
        roleSourcesExampleCriteria.andRoleFkEqualTo(role.getRoleid());
        roleSourcesMapper.deleteByExample(roleSourcesExample);

        //重新把当前角色所拥用的资源添加到中间表中
        String[] sourcesIds=ids.split(",");
        int count=0;
        for(String sourcesId : sourcesIds){
            RoleSources roleSources=new RoleSources();
            roleSources.setRoleFk(role.getRoleid());
            roleSources.setResourcesFk(Integer.parseInt(sourcesId));
            roleSourcesMapper.insert(roleSources);
            count++;
        }
        if(rows==1 && count==sourcesIds.length){
            return  OAResult.ok(200,"操作成功");
        }

        return OAResult.ok(400,"操作失败");
    }
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值