【Saas-export项目】8--(角色role管理)增删改查

48 篇文章 1 订阅
37 篇文章 3 订阅



页面显示

主页面

在这里插入图片描述

添加界面

在这里插入图片描述

编辑界面

  • 他跟添加界面不同的是,进入界面之后数据会显示

在这里插入图片描述

后台代码

查看页面RoleController

  • 先把基本框架写出来
@Controller
@RequestMapping("/system/role")
public class RoleController {
    private static  final Logger l = LoggerFactory.getLogger(RoleController.class);

    @RequestMapping(path="/toList",method ={ RequestMethod.GET, RequestMethod.POST})
    public String toList(){
        return "system/role/role-list";
    }
    @RequestMapping(path="/toAdd",method ={ RequestMethod.GET, RequestMethod.POST})
    public String toAdd(){
        return "system/role/role-add";
    }
    @RequestMapping(path="/toUpdate",method ={ RequestMethod.GET, RequestMethod.POST})
    public String toUpdate(){
        return "system/role/role-update";
    }
    @RequestMapping(path="/toRoleModule",method ={ RequestMethod.GET, RequestMethod.POST})
    public String toRoleModule(){
        return "system/role/role-module";
    }
}

(1)Role.java实体类

  • 定义实体类
public class Role {
    private String roleId;
    private String name;
    private String remark;
    private long orderNo;
    private String createBy;
    private String createDept;
    private Date createTime;
    private String updateBy;
    private Date updateTime;
    private String companyId;//不同公司使用不同角色
    private String companyName;
全参、空参(必有)、toString、getter AND setter()

(2)TestRoleService.java测试

  • 可写增删改查
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath*:spring/applicationContext-*.xml")  //3:创建 spring/applicationContext-tx.xml
public class TestRoleService {
    private static final Logger l= LoggerFactory.getLogger(TestRoleService.class);
    
    @Autowired
    IRoleService iRoleService;
    /*增删改查*/
    @Test
    public void test01(){
        //分页列表
        //页面上显示分页列表
        PageInfo<Role> pi=iRoleService.findByPage(1,3,"1");
        l.info("pi="+pi);
    }
    @Test
    public void test02(){
        //将一个表单数据保存在javaBean中,再存到数据库
       Role role=new Role();
       role.setName("角色1");
       role.setRemark("角色1的备注");
       role.setCompanyId("1");
       role.setCompanyName("吉首大学");
       iRoleService.saveRole(role);
       //l.info("pi="+pi);
    }
    @Test
    public void test03(){
    //更新业务,先根据id查找对应记录,编辑值,在保存到数据库
        String roleId ="";
        Role role=iRoleService.findById(roleId);
        l.info("role="+role);
        //修改
        role.setName("角色2");
        role.setRemark("角色2备注");
        //保存
        iRoleService.updateRole(role);
    }
    @Test
    public void test04(){
        //删除业务,根据指定id
        String roleId="33";
        //删除
        iRoleService.deleteRole(roleId);
    }

}

(3-1)IRoleService.java

public interface IRoleService {
    PageInfo<Role> findByPage(int curr, int pageSize, String companyId);

    
    void saveRole(Role role);

    Role findById(String roleId);

    void updateRole(Role role);

    void deleteRole(String roleId);
}

(3-2)RoleServiceImpl.java

@Service
public class RoleServiceImpl implements IRoleService {
    @Autowired
    IRoleDao iRoleDao;
    @Override
    public PageInfo<Role> findByPage(int curr, int pageSize, String companyId) {
        //设置参数
        PageHelper.startPage(curr,pageSize);
        //调用全查
        List<Role> list=iRoleDao.findAll(companyId);
        //包装成pageInfo
        PageInfo<Role> pi= new PageInfo<>(list);
        return pi;
    }

    @Override
    public void saveRole(Role role) {
        String uuid= UUID.randomUUID().toString();
        role.setRoleId(uuid);
        iRoleDao.save(role);
    }

    @Override
    public Role findById(String roleId) {
        return iRoleDao.findById(roleId);
    }

    @Override
    public void updateRole(Role role) {
        iRoleDao.update(role);
    }

    @Override
    public void deleteRole(String roleId) {
        iRoleDao.deleteById(roleId);
    }

}

(4-1)IRoleDao.java

public interface IRoleDao {

    List<Role> findAll(String companyId);

    void save(Role role);

    Role findById(String roleId);

    void update(Role role);

    void deleteById(String roleId);
}

(4-2)IRoleDao.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<!--namespace: 需要映射的Dao接口类型-->
<mapper namespace="com.smp.dao.system.role.IRoleDao">

    <!--设置字段与变量名的映射-->
    <resultMap id="roleMap" type="role">
        <result column="role_id" property="roleId"/>
        <result column="order_no" property="orderNo"/>
        <result column="create_by" property="createBy"/>
        <result column="create_dept" property="createDept"/>
        <result column="create_time" property="createTime"/>
        <result column="update_by" property="updateBy"/>
        <result column="update_time" property="updateTime"/>
        <result column="company_id" property="companyId"/>
        <result column="company_name" property="companyName"/>
    </resultMap>


    <select id="findAll" parameterType="string" resultMap="roleMap">
       select * from pe_role where company_id=#{companyId} order by order_no
   </select>


    <insert id="save" parameterType="role">
        insert into pe_role
            (
            role_id       ,
            name          ,
            remark        ,
            order_no      ,
            create_by     ,
            create_dept   ,
            create_time   ,
            update_by     ,
            update_time   ,
            company_id    ,
            company_name
            )
            values
            (
            #{ roleId      },
            #{ name         },
            #{ remark      	},
            #{ orderNo    	},
            #{ createBy   	},
            #{ createDept 	},
            #{ createTime  },
            #{ updateBy    },
            #{ updateTime  },
            #{ companyId   },
            #{ companyName }
            )
    </insert>

    <select id="findById" parameterType="string" resultMap="roleMap">
        select * from pe_role where role_id=#{roleId}
    </select>

    <update id="update" parameterType="role">
            update pe_role set

            name         = #{name       },
            remark       = #{remark     },
            order_no     = #{orderNo    },
            create_by    = #{createBy   },
            create_dept  = #{createDept },
            create_time  = #{createTime },
            update_by    = #{updateBy   },
            update_time  = #{updateTime },
            company_id   = #{companyId  },
            company_name = #{companyName}

            where role_id = #{roleId}
    </update>

    <delete id="deleteById" parameterType="string">
        delete from pe_role where role_id=#{roleId}
    </delete>
</mapper>

前台代码

(1)RoleController.java

  • 这里继承了一个父类 —>getLoginCompanyId()
  • @RequestParam(defaultValue = “1”)int curr,@RequestParam(defaultValue = “10”)int pageSize【参考:controller设置每页数据有几条
@Controller
@RequestMapping("system/role")
public class RoleController extends BaseController {

    private static final Logger l= LoggerFactory.getLogger(RoleController.class);

    @Autowired
    IRoleService iRoleService;

    @RequestMapping(path = "/toList",method = {RequestMethod.GET,RequestMethod.POST})
    public  String toList(@RequestParam(defaultValue = "1")int curr,@RequestParam(defaultValue = "10")int pageSize){
        //调查询分页列表的方法
        PageInfo<Role> pi=iRoleService.findByPage(curr,pageSize,getLoginCompanyId());
        //将pi添加到页面
        request.setAttribute("pi",pi);
        return "system/role/role-list";
    }

    @RequestMapping(path = "/toAdd",method = {RequestMethod.GET,RequestMethod.POST})
    public  String toAdd(){
        return "system/role/role-add";
    }

    @RequestMapping(path = "/add",method = {RequestMethod.GET,RequestMethod.POST})
    public  String add(Role role){
        //接收页面提交过来的表单
        l.info("add role="+role);
        role.setCompanyId(getLoginCompanyId());
        role.setCompanyName(getLoginCompanyName());
        iRoleService.saveRole(role);
        return "redirect:/system/role/toList.do";
    }

    @RequestMapping(path = "/toUpdate",method = {RequestMethod.GET,RequestMethod.POST})
    public  String toUpdate(String roleId){
        //需要使用参数接收提交的roleId
        Role role=iRoleService.findById(roleId);
        l.info("toUpdate role="+role);
        //回显到页面
        request.setAttribute("role",role);
        return "system/role/role-update";
    }

    @RequestMapping(path = "/update",method = {RequestMethod.GET,RequestMethod.POST})
    public  String update(Role role){
        l.info("update role="+role);

        role.setCompanyId(getLoginCompanyId());
        role.setCompanyName(getLoginCompanyName());
        iRoleService.updateRole(role);
        return "redirect:/system/role/toList.do";
    }
    @RequestMapping(path = "/delete",method = {RequestMethod.GET,RequestMethod.POST})
    public  @ResponseBody
    Object delete(String roleId){
        try {
            iRoleService.deleteRole(roleId);
            //成功
            return Result.init(200,"删除成功",null);
        } catch (Exception e) {
            e.printStackTrace();
            //失败
            return Result.init(-200,"删除失败",null);
        }

    }

    @RequestMapping(path = "/toRoleModule",method = {RequestMethod.GET,RequestMethod.POST})
    public  String toRoleModule(){
        return "system/role/role-module";
    }

}

(2)菜单栏界面地址left_menu.jsp

<li id="sys-role">
                     <a href="${path}/system/role/toList.do" onclick="setSidebarActive(this)"  target="iframe">
                         <i class="fa fa-circle-o"></i>角色管理
                     </a>
                 </li>

(3)role-list.jsp列表显示界面

<script>
    function deleteById() {
        var id = getCheckId()
        if(id) {
            if(confirm("你确认要删除此条记录吗?")) {
                //location.href="/system/role/delete.do?id="+id;
                //使用ajax请求
                var url='${path}/system/role/delete.do?roleId='+id
                var fn=function (result) {
                    //弹出提示
                    alert(result.msg)
                    window.location.reload()
                }
                $.get(url,fn,'json')
            }
        }else{
            alert("请勾选待处理的记录,且每次只能勾选一个")
        }
    }

    function findModuleByRoleId(){
        var id = getCheckId();
        if(id) {
            location.href="/system/role/roleModule.do?roleid="+id;
        }else{
            alert("请勾选待处理的记录,且每次只能勾选一个")
        }
    }
</script>
<body>
<div id="frameContent" class="content-wrapper" style="margin-left:0px;">
<section class="content-header">
    <h1>
        系统管理
        <small>角色管理</small>
    </h1>
    <ol class="breadcrumb">
        <li><a href="all-admin-index.html"><i class="fa fa-dashboard"></i> 首页</a></li>
    </ol>
</section>
<!-- 内容头部 /-->

<!-- 正文区域 -->
<section class="content">

    <!-- .box-body -->
    <div class="box box-primary">
        <div class="box-header with-border">
            <h3 class="box-title">角色列表</h3>
        </div>

        <div class="box-body">

            <!-- 数据表格 -->
            <div class="table-box">

                <!--工具栏-->
                <div class="pull-left">
                    <div class="form-group form-inline">
                        <div class="btn-group">
                            <button type="button" class="btn btn-default" title="新建" onclick='location.href="${path}/system/role/toAdd.do"'><i class="fa fa-file-o"></i> 新建</button>
                            <button type="button" class="btn btn-default" title="删除" onclick='deleteById()'><i class="fa fa-trash-o"></i> 删除</button>
                            <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                            <button type="button" class="btn btn-default" title="权限" onclick="findModuleByRoleId()"><i class="fa fa-users"></i> 权限</button>
                        </div>
                    </div>
                </div>
                <div class="box-tools pull-right">
                    <div class="has-feedback">
                        <input type="text" class="form-control input-sm" placeholder="搜索">
                        <span class="glyphicon glyphicon-search form-control-feedback"></span>
                    </div>
                </div>
                <!--工具栏/-->

                <!--数据列表-->
                <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
                    <thead>
                    <tr>
                        <th class="" style="padding-right:0px;">
                            <input type="checkbox" name="selid" onclick="checkAll('id',this)">
                        </th>
                        <th class="sorting">序号</th>
                        <th class="sorting">编号</th>
                        <th class="sorting">名称</th>
                        <th class="sorting">说明</th>
                        <th class="text-center">操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <c:forEach items="${pi.list}" var="o" varStatus="status">
                    <tr class="odd" onmouseover="this.className='highlight'" onmouseout="this.className='odd'" >
                        <td><input type="checkbox" name="id" value="${o.roleId}"/></td>
                        <td>${status.index+1}</td>
                        <td>${o.roleId}</td>
                        <td><a href="${path}/system/role/toUpdate.do?roleId=${o.roleId}">${o.name}</a></td>
                        <td>${o.remark}</td>
                        <th class="text-center"><button type="button" class="btn bg-olive btn-xs" onclick='location.href="${path}/system/role/toUpdate.do?roleId=${o.roleId}"'>编辑</button></th>
                    </tr>
                    </c:forEach>
                    </tbody>
                </table>
            </div>
        </div>
        <div class="box-footer">
            <jsp:include page="../../common/page.jsp">
                <jsp:param value="${path}/system/role/toList.do" name="pageUrl"/>
            </jsp:include>
        </div>
    </div>

</section>
</div>
</body>

(4)role-add.jsp添加界面

<body>
    <div id="frameContent" class="content-wrapper" style="margin-left:0px;">
        <section class="content-header">
            <h1>
                系统管理
                <small>角色管理</small>
            </h1>
            <ol class="breadcrumb">
                <li><a href="all-admin-index.html"><i class="fa fa-dashboard"></i> 首页</a></li>
            </ol>
        </section>
        <section class="content">
            <div class="box-body">
                <div class="nav-tabs-custom">
                    <ul class="nav nav-tabs">
                        <li class="active">
                            <a href="#tab-form" data-toggle="tab">编辑角色</a>
                        </li>
                    </ul>
                    <div class="tab-content">
                        <form id="editForm" action="${path}/system/role/add.do" method="post">
                            <%--<input type="hidden" name="roleId" value="${role.roleId}">--%>
                            <div class="tab-pane active" id="tab-form">
                                <div class="row data-type">
                                    <div class="col-md-2 title">角色名</div>
                                    <div class="col-md-10 data">
                                        <input type="text" class="form-control" placeholder="角色名" name="name" value="${role.name}">
                                    </div>
                                    <div class="col-md-2 title">备注</div>
                                    <div class="col-md-10 data line-height36">
                                        <input type="text" class="form-control" placeholder="备注" name="remark" value="${role.remark}">
                                    </div>
                                    <div class="col-md-2 title"></div>
                                    <div class="col-md-10 data text-center">
                                        <button type="button" onclick='document.getElementById("editForm").submit()'  class="btn bg-maroon">保存</button>
                                        <button type="button" class="btn bg-default" onclick="history.back(-1);">返回</button>
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>

        </section>
    </div>
</body>

(5)role-update.jsp编辑界面

<body>
    <div id="frameContent" class="content-wrapper" style="margin-left:0px;">
        <section class="content-header">
            <h1>
                系统管理
                <small>角色管理</small>
            </h1>
            <ol class="breadcrumb">
                <li><a href="all-admin-index.html"><i class="fa fa-dashboard"></i> 首页</a></li>
            </ol>
        </section>
        <section class="content">
            <div class="box-body">
                <div class="nav-tabs-custom">
                    <ul class="nav nav-tabs">
                        <li class="active">
                            <a href="#tab-form" data-toggle="tab">编辑角色</a>
                        </li>
                    </ul>
                    <div class="tab-content">
                        <form id="editForm" action="${path}/system/role/update.do" method="post">
                            <input type="hidden" name="roleId" value="${role.roleId}">
                            <div class="tab-pane active" id="tab-form">
                                <div class="row data-type">
                                    <div class="col-md-2 title">角色名</div>
                                    <div class="col-md-10 data">
                                        <input type="text" class="form-control" placeholder="角色名" name="name" value="${role.name}">
                                    </div>
                                    <div class="col-md-2 title">备注</div>
                                    <div class="col-md-10 data line-height36">
                                        <input type="text" class="form-control" placeholder="备注" name="remark" value="${role.remark}">
                                    </div>
                                    <div class="col-md-2 title"></div>
                                    <div class="col-md-10 data text-center">
                                        <button type="button" onclick='document.getElementById("editForm").submit()'  class="btn bg-maroon">保存</button>
                                        <button type="button" class="btn bg-default" onclick="history.back(-1);">返回</button>
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>

        </section>
    </div>
</body>

(6)role-module.jsp权限界面

<head>
    <base href="${path}/">
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>数据 - AdminLTE2定制版</title>
    <meta name="description" content="AdminLTE2定制版">
    <meta name="keywords" content="AdminLTE2定制版">
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 页面meta /-->
    <link rel="stylesheet" href="plugins/ztree/css/zTreeStyle/zTreeStyle.css" type="text/css">
    <script type="text/javascript" src="plugins/ztree/js/jquery-1.4.4.min.js"></script>
    <script type="text/javascript" src="plugins/ztree/js/jquery.ztree.core-3.5.js"></script>
    <script type="text/javascript" src="plugins/ztree/js/jquery.ztree.excheck-3.5.js"></script>

    <SCRIPT type="text/javascript">

    </SCRIPT>
</head>

<body style="overflow: visible;">
<div id="frameContent" class="content-wrapper" style="margin-left:0px;height: 1200px" >
    <section class="content-header">
        <h1>
            菜单管理
            <small>菜单列表</small>
        </h1>
        <ol class="breadcrumb">
            <li><a href="all-admin-index.html"><i class="fa fa-dashboard"></i> 首页</a></li>
        </ol>
    </section>
    <!-- 内容头部 /-->

    <!-- 正文区域 -->
    <section class="content">

        <!-- .box-body -->
        <div class="box box-primary">
            <div class="box-header with-border">
                <h3 class="box-title">角色 [${role.name}] 权限列表</h3>
            </div>

            <div class="box-body">

                <!-- 数据表格 -->
                <div class="table-box">
                    <!--工具栏-->
                    <div class="box-tools text-left">
                        <button type="button" class="btn bg-maroon" onclick="submitCheckedNodes();">保存</button>
                        <button type="button" class="btn bg-default" onclick="history.back(-1);">返回</button>
                    </div>
                    <!--工具栏/-->
                    <!-- 树菜单 -->
                    <form name="icform" method="post" action="${path}/system/role/toRoleModule.do">
                        <input type="hidden" name="roleId" value="${role.roleId}"/>
                        <input type="hidden" id="moduleIds" name="moduleIds" value=""/>
                        <div class="content_wrap">
                            <div class="zTreeDemoBackground left" style="overflow: visible">
                                <ul id="treeDemo" class="ztree"></ul>
                            </div>
                        </div>

                    </form>
                    <!-- 树菜单 /-->

                </div>
                <!-- /数据表格 -->

            </div>
            <!-- /.box-body -->
        </div>
    </section>
</div>
</body>
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值