【Saas-export项目】6--(部门管理)增删改查

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



页面显示

主页面

在这里插入图片描述
每页10条数据,共14条,第二页4条
在这里插入图片描述

添加页面

在这里插入图片描述

编辑页面

编辑页面分两步

  • 编辑回显数据
  • 编辑传值

在这里插入图片描述

后台代码

Dept.java

  • Dept.java(export_parent\export_domain\src\main\java\com\smp\domain\system\dept)
package com.smp.domain.system.dept;

public class Dept {
    //成员变量是小驼峰 第二个单词起首字母大写
    //deptId

    //数据库  每个单词小写 使用_隔开
    //dept_id

    private String deptId;
    private String deptName;
    //private String parentId; //上级部门
    //必须获取上级部门,再获取它的deptId
    private Dept parent; //上级部门
    private Integer state;
    private String companyId;//企业id
    private String companyName;

    public Dept() {
    }

    public Dept(String deptId, String deptName, Dept parent, Integer state, String companyId, String companyName) {
        this.deptId = deptId;
        this.deptName = deptName;
        this.parent = parent;
        this.state = state;
        this.companyId = companyId;
        this.companyName = companyName;
    }

    @Override
    public String toString() {
        return "Dept{" +
                "deptId='" + deptId + '\'' +
                ", deptName='" + deptName + '\'' +
                ", state=" + state +
                ", companyId='" + companyId + '\'' +
                ", companyName='" + companyName + '\'' +
                '}';
    }

    public String getDeptId() {
        return deptId;
    }

    public void setDeptId(String deptId) {
        this.deptId = deptId;
    }

    public String getDeptName() {
        return deptName;
    }

    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }

    public Dept getParent() {
        return parent;
    }

    public void setParent(Dept parent) {
        this.parent = parent;
    }

    public Integer getState() {
        return state;
    }

    public void setState(Integer state) {
        this.state = state;
    }

    public String getCompanyId() {
        return companyId;
    }

    public void setCompanyId(String companyId) {
        this.companyId = companyId;
    }

    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }
}

TestDeptService.java测试

  • src\test\java\com\smp\service\company
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath*:spring/applicationContext-*.xml")  //3:创建 spring/applicationContext-tx.xml
public class TestDeptService {
    private static final Logger l= LoggerFactory.getLogger(TestDeptService.class);

    //1、注入业务部门类
    //2、定义接口
    //3、定义实现类
    //4、在实现类上加@service
    @Autowired
    IDeptService iDeptService;

    //列表显示
    @Test
    public void test01(){
        //部门分页显示
        int curr=1;
        int pageSize=3;
        String companyId="1";
        //2、调用分页查找方法
        PageInfo<Dept> pi=iDeptService.findByPage(curr,pageSize,companyId);
        //3、打印
        l.info("test01 pi="+pi);
    }
    //查询company_id为1的部门
    @Test
    public void test02(){
        //给页面的下拉菜单按公司查找所有的部门
        String  companyId="1";
        //2 调用分页查找方法
        List<Dept> list = iDeptService.findAll(companyId);//
        //打印
        l.info("test02 list="+list);
    }
    //添加一个部门
    @Test
    public void test03(){
        //模拟表单
        Dept dept=new Dept();
        dept.setCompanyId("1");
        dept.setDeptName("java部门");
        dept.setState(1);

        Dept parent =new Dept();
        parent.setDeptId("100101101");

        dept.setParent(parent);
        iDeptService.saveDept(dept);
    }
    //查询指定dept_id的部门
    @Test
    public void test04(){
        String deptId="100101";
        Dept dept=iDeptService.findById(deptId);
        l.info("test04 dept="+dept);
    }
    //修改
    @Test
    public void test05(){
        String deptId="8a7e82be61400c000161400c05810000";
        Dept dept = iDeptService.findById(deptId);
        //1 模拟页面的修改
        dept.setDeptName("BATJM299");
        dept.setCompanyName("吉首大学");
        dept.setCompanyId("2");
        Dept parent = new Dept();//下拉菜单
        parent.setDeptId("100");
        dept.setParent(parent);
        dept.setState(0);//停用
        //2 保存到数据库
        iDeptService.updateDept(dept);
        l.info("test05  dept="+dept);
    }
    //删除
    @Test
    public void test06(){
        //给定id
        //String deptId=" ";
        String deptId="c2a4fadf-4efa-4e17-a4a4-3f93d84825d9";//有给其他部门作上级
        //根据id删除
        boolean result=iDeptService.deleteDeptById(deptId);
        l.info("test06 result="+result);
    }

}

IDeptService.java

  • src\main\java\com\smp\service\system\dept
public interface IDeptService {
    //查询指定公司id的第几个部门分页
    PageInfo<Dept> findByPage(int curr, int pageSize, String companyId);

    List<Dept> findAll(String companyId);
    //新建一个部门
    void saveDept(Dept dept);
    //查找指定id的部门
    Dept findById(String deptId);
    //保存编辑页面的部门数据
    void updateDept(Dept dept);
    //根据指定的deptId删除部门数据 当前部门有没有给其他部门作上级
    // 》1可以直接删除 》2删除报错
    boolean deleteDeptById(String deptId);
}

DeptServiceImpl.java

  • src\main\java\com\smp\service\system\dept\impl
@Service
public class DeptServiceImpl implements IDeptService {
    @Autowired
    IDeptDao iDeptDao;
    @Override
    public PageInfo<Dept> findByPage(int curr, int pageSize, String companyId) {
        //调用dao查看所有的记录
        PageHelper.startPage(curr,pageSize);
        List<Dept> list=iDeptDao.findAll(companyId);
        return new PageInfo<>(list);
    }

    @Override
    public List<Dept> findAll(String companyId) {
        List<Dept> list=iDeptDao.findAll(companyId);
        return list;
    }
    //新建一个部门
    @Override
    public void saveDept(Dept dept) {
        //获取随机id
        String id= UUID.randomUUID().toString();
        dept.setDeptId(id);

        iDeptDao.save(dept);
    }
    //查找指定id的部门
    @Override
    public Dept findById(String deptId) {
        return iDeptDao.findById(deptId);
    }
    //与保存的区别 》1:前者insert 后者是update  》2:前者需要产生id,后者有id
    @Override
    public void updateDept(Dept dept) {
        iDeptDao.update(dept);
    }

    @Override
    public boolean deleteDeptById(String deptId) {
        //先查询count
        int count = iDeptDao.findParentCount(deptId);
        //再根据count判断
        if(count==0){//没有给其他部门作上级
            iDeptDao.deleteById(deptId);
            return true;
        }else{
            return false;
        }
    }

}

IDeptDao.java

  • src\main\java\com\smp\dao\system\dept
public interface IDeptDao {

    List<Dept> findAll(String companyId);

    Dept findById(String deptId);
    //新建一个部门
    void save(Dept dept);
    //保存编辑页面的部门
    void update(Dept dept);

    int findParentCount(String deptId);
    void deleteById(String deptId);
}

IDeptDao.xml

  • src\main\resources\com\smp\dao\system\dept
<?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.dept.IDeptDao">

    <!--//select * from pe_dept
    List<Dept> findAll(String companyId);-->
    <!--<select id="findAll" resultMap="findOneMap">
        select * from pe_dept
    </select>-->
    <select id="findAll" parameterType="string" resultMap="findOneMap">
            select * from pe_dept where company_id = #{companyId}
    </select>
    <!--将查询结果映射到成员变量-->
    <resultMap id="findOneMap" type="dept">
        <id column="dept_id" property="deptId"/>
        <result column="dept_name" property="deptName"/>
        <result column="company_id" property="companyId"/>
        <result column="company_name" property="companyName"/>
        <!-- 将parent_id 映射Dept类型
        private  Dept parent
        -->
        <association property="parent" column="parent_id" javaType="dept" select="findById">
        </association>
    </resultMap>

    <select id="findById" parameterType="string" resultMap="findOneMap">
        select  * from  pe_dept where dept_id = #{dept_id}
    </select>

    <insert id="save" parameterType="dept">
    insert into pe_dept
        (
        dept_id      ,
        dept_name    ,
        parent_id    ,
        state        ,
        company_id   ,
        company_name
        )
        values
        (
        #{deptId     },
        #{deptName   },
        #{parent.deptId  },
        #{state       },
        #{companyId  },
        #{companyName}
        )
    </insert>

    <!--保存编辑页面的部门-->
    <!--<update id="update" parameterType="dept">
        update pe_dept set
            dept_name=  #{deptName},
            parent_id=  #{parent.deptId},
            state=      #{state},
            company_id=  #{companyId},
            company_name=#{companyName}

        where dept_id=#{deptId}
    </update>-->
    <!--方法1:-->
   <!-- <update id="update" parameterType="dept">
        update pe_dept set

        dept_name= #{deptName},
        <if test="parent.deptId  == null or parent.deptId == '' ">
            parent_id= NULL ,
        </if>
        <if test="parent.deptId !=null and parent.deptId != '' ">
            parent_id= ${parent.deptId},
        </if>
        state= #{state},
        company_id= #{companyId},
        company_name= #{companyName}

        where dept_id= #{deptId}
    </update>-->


    <!--方法2:-->
    <update id="update" parameterType="dept">
        update pe_dept set

            dept_name=  #{deptName},
            <choose>
                <when test="parent.deptId==null or parent.deptId==''">
                    parent_id= null ,
                </when>
                <otherwise>
                    parent_id=  #{parent.deptId},
                </otherwise>
            </choose>

            state=      #{state},
            company_id=  #{companyId},
            company_name=#{companyName}

        where dept_id=#{deptId}
    </update>

    <!-- 统计当前部门作为其他部门的上级的数量-->
    <select id="findParentCount" parameterType="string" resultType="int">
        select count(*) from pe_dept where parent_id=#{deptId}
    </select>
    <!--    删除指定deptId的部门-->
    <delete id="deleteById" parameterType="string">
        delete from pe_dept where dept_id=#{deptId}
    </delete>

</mapper>

前台代码

DeptController.java

  • src\main\java\com\smp\web\controller\system\dept
    @Controller
@RequestMapping("system/dept")
public class DeptController extends BaseController{
    @Autowired
    IDeptService iDeptService;

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

    @RequestMapping(path = "/toList",method = {RequestMethod.GET,RequestMethod.POST})
    public  String toList(Model model,@RequestParam(defaultValue = "1") Integer curr,
                          @RequestParam(defaultValue = "10")  Integer pageSize){
        l.info("toList curr"+curr);//当前页数
        l.info("toList pageSize"+pageSize);//每页记录数
        l.info("toList companyId"+super.getLoginCompanyId());//指定公司id
        //查询一个分页
        PageInfo<Dept> pi=iDeptService.findByPage(curr,pageSize,super.getLoginCompanyId());

        l.info("toList pi="+pi);
        model.addAttribute("pi",pi);
        return "system/dept/dept-list";
    }
    @RequestMapping(path="/toAdd",method ={ RequestMethod.GET, RequestMethod.POST})
    public String toAdd(Model model){
        //需要为下拉菜单查询出所有的部门,一个部门对应一个选项
        //根据companyId查询出部门,不做分页
        l.info("toAdd companyId="+super.getLoginCompanyId());
        List<Dept> list=iDeptService.findAll(super.getLoginCompanyId());
        l.info("toAdd list="+list);
        model.addAttribute("list",list);
        return "system/dept/dept-add";
    }
    @RequestMapping(path = "/add",method = RequestMethod.POST)
    public String add(Dept dept,String parentId){
        l.info("add dept="+dept);
        l.info("add parentId="+parentId);
        //默认公司id为1,后面可修改

        dept.setCompanyId(super.getLoginCompanyId());
        dept.setCompanyName(super.getLoginCompanyName());

        Dept parent =new Dept();
        parent.setDeptId(parentId);

        dept.setParent(parent);
        //保存到数据库
        iDeptService.saveDept(dept);
        return "redirect:/system/dept/toList.do";
    }
    //修改编辑
    //${path}/system/dept/toUpdate.do?deptId=${dept.deptId}
    @RequestMapping(path="/toUpdate",method ={ RequestMethod.GET, RequestMethod.POST})
    public String toUpdate(Model model, String deptId){

        String companyId = super.getLoginCompanyId();
        l.info("toUpdate deptId="+deptId);

        //查询部门
        Dept dept = iDeptService.findById(deptId);
        l.info("toUpdate dept="+dept);

        List<Dept> list = iDeptService.findAll(companyId);

        model.addAttribute("dept",dept);
        model.addAttribute("list",list);

        return "system/dept/dept-update";
    }
    //修改回显
    //action="${path}/system/dept/update.do"
    @RequestMapping(path="/update",method ={ RequestMethod.GET, RequestMethod.POST})
    public String update(Dept dept,String parentId){

        l.info("update dept="+dept);
        l.info("update parentId="+parentId);

        //当前写死companyId与companyName以后再修改
        dept.setCompanyName(super.getLoginCompanyName());
        dept.setCompanyId(super.getLoginCompanyId());

        Dept parent = new Dept();//下拉菜单
        parent.setDeptId(parentId);
        dept.setParent(parent);

        l.info("update dept="+dept);
        //2 保存到数据库
        iDeptService.updateDept(dept);
        //修改完成之后跳到列表页面
        return "redirect:/system/dept/toList.do";
    }

    // location.href="${path}/system/dept/delete.do?depId="+deptId;
    @RequestMapping(path="/delete",method ={ RequestMethod.GET})
    public String delete(String deptId){
        l.info("delete deptId="+deptId);

        iDeptService.deleteDeptById(deptId);
        //修改完成之后跳到列表页面
        return "redirect:/system/dept/toList.do";
    }
}

dept-list.jsp

  • src\main\webapp\WEB-INF\pages\system\dept
<script>
    function deleteById() {
        var id = getCheckId()
        if(id) {
            if(confirm("你确认要删除此条记录吗?")) {
                location.href="${path}/system/dept/delete.do?deptId="+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/dept/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>
                        </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="dept"  varStatus="st">
                        <tr>
                            <td><input type="checkbox" name="id" value="${dept.deptId}"/></td>
                            <td>${st.count }</td>
                            <td>${dept.deptId}</td>
                            <td>${dept.parent.deptName }</td>
                            <td><a href="/system/dept/toUpdate.do?id=${dept.deptId}">${dept.deptName }</a></td>
                            <th class="text-center"><button type="button" class="btn bg-olive btn-xs" onclick='location.href="${path}/system/dept/toUpdate.do?deptId=${dept.deptId}"'>编辑</button></th>
                        </tr>
                    </c:forEach>
                    </tbody>
                </table>
            </div>
        </div>
        <div class="box-footer">
            <jsp:include page="../../common/page.jsp">
                <jsp:param value="${path}/system/dept/toList.do" name="pageUrl"/>
            </jsp:include>
        </div>
    </div>
</section>
</div>
</body>

dept-add.jsp

  • src\main\webapp\WEB-INF\pages\system\dept
<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/dept/add.do" method="post">
                        <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="deptName" value="${dept.deptName}">
                                </div>
                                <div class="col-md-2 title">上级部门</div>
                                <div class="col-md-10 data line-height36">
                                    <select class="form-control" name="parentId">
                                        <c:forEach items="${list}" var="item">
                                            <option value="${item.deptId}">${item.deptName}</option>
                                        </c:forEach>
                                    </select>
                                </div>
                                <div class="col-md-2 title">状态</div>
                                <div class="col-md-10 data">
                                    <div class="form-group form-inline">
                                        <div class="radio"><label><input type="radio"  checked="checked" name="state" value="0">停用</label></div>
                                        <div class="radio"><label><input type="radio"  name="state" value="1">启用</label></div>
                                    </div>
                                </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>

dept-update.jsp

  • src\main\webapp\WEB-INF\pages\system\dept
<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/dept/update.do" method="post">
                        <input type="hidden" name="deptId" value="${dept.deptId}">
                        <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="deptName" value="${dept.deptName}">
                                </div>
                                <div class="col-md-2 title">上级部门</div>
                                <div class="col-md-10 data line-height36">
                                    <select class="form-control" name="parentId">
                                        <option value="">董事会</option>
                                        <c:forEach items="${list}" var="item">
                                            <%-- dept表示正在编辑的部门数据,不能选自己作为上级部门--%>
                                            <c:if test="${dept.deptId != item.deptId}">
                                                <option ${dept.parent.deptId == item.deptId ?'selected':''}  value="${item.deptId}">${item.deptName}</option>
                                            </c:if>

                                        </c:forEach>
                                    </select>
                                </div>
                                <div class="col-md-2 title">状态</div>
                                <div class="col-md-10 data">
                                    <div class="form-group form-inline">
                                        <div class="radio"><label><input type="radio" ${dept.state==0?'checked':''} name="state" value="0">停用</label></div>
                                        <div class="radio"><label><input type="radio" ${dept.state==1?'checked':''} name="state" value="1">启用</label></div>
                                    </div>
                                </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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值