部门管理pojo
package com. cy. pj. sys. pojo;
import lombok. Data;
import java. io. Serializable;
import java. util. Date;
@Data
public class SysDept implements Serializable {
private static final long serialVersionUID = 8876920804134951849 L;
private Integer id;
private String name;
private Integer parentId;
private Integer sort;
private String note;
private Date createdTime;
private Date modifiedTime;
private String createdUser;
private String modifiedUser;
}
部门管理Dao接口
package com. cy. pj. sys. dao;
import com. cy. pj. common. pojo. Node;
import com. cy. pj. sys. pojo. SysDept;
import org. apache. ibatis. annotations. Delete;
import org. apache. ibatis. annotations. Mapper;
import org. apache. ibatis. annotations. Param;
import org. apache. ibatis. annotations. Select;
import java. util. List;
import java. util. Map;
@Mapper
public interface SysDeptDao {
@Select ( "select c.*,p.name parentName from sys_depts c left join sys_depts p on c.parentId=p.id" )
List< Map< String, Object> > findObjects ( ) ;
@Select ( "select id,name,parentId from sys_depts" )
List< Node> findZTreeNodes ( ) ;
int updateObject ( SysDept entity) ;
int insertObject ( SysDept entity) ;
@Select ( "select count(*) from sys_depts where parentId=#{id}" )
int getChildCount ( Integer id) ;
@Delete ( "delete from sys_depts where id=#{id}" )
int deleteObject ( Integer id) ;
int findById ( @Param ( "id" ) Integer id) ;
}
部门管理Mapper.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">
< mapper namespace = " com.cy.pj.sys.dao.SysDeptDao" >
< select id = " findById"
resultType = " int" >
select *
from sys_depts
where id=#{id}
</ select>
< update id = " updateObject"
parameterType = " com.cy.pj.sys.pojo.SysDept" >
update sys_depts
set
name=#{name},
sort=#{sort},
note=#{note},
parentId=#{parentId},
modifiedUser=#{modifiedUser},
modifiedTime=now()
where id=#{id}
</ update>
< insert id = " insertObject"
parameterType = " com.cy.pj.sys.pojo.SysDept" >
insert into sys_depts
(name,sort,note,parentId,createdTime,modifiedTime,createdUser,modifiedUser)
values
(#{name},#{sort},#{note},#{parentId},now(),now(),#{createdUser},#{modifiedUser})
</ insert>
</ mapper>
部门管理Service接口
package com. cy. pj. sys. service;
import com. cy. pj. common. pojo. Node;
import com. cy. pj. sys. pojo. SysDept;
import java. util. List;
import java. util. Map;
public interface SysDeptService {
List< Map< String, Object> > findObjects ( ) ;
List< Node> findZTreeNodes ( ) ;
int saveObject ( SysDept entity) ;
int updateObject ( SysDept entity) ;
int deleteObject ( Integer id) ;
}
部门管理Service实现类
package com. cy. pj. sys. service. impl;
import com. cy. pj. common. exception. ServiceException;
import com. cy. pj. common. pojo. Node;
import com. cy. pj. sys. dao. SysDeptDao;
import com. cy. pj. sys. pojo. SysDept;
import com. cy. pj. sys. service. SysDeptService;
import org. springframework. beans. factory. annotation. Autowired;
import org. springframework. stereotype. Service;
import org. springframework. util. StringUtils;
import java. util. List;
import java. util. Map;
@Service
public class SysDeptServiceImpl implements SysDeptService {
@Autowired
private SysDeptDao sysDeptDao;
@Override
public List< Map< String, Object> > findObjects ( ) {
List< Map< String, Object> > list=
sysDeptDao. findObjects ( ) ;
if ( list== null|| list. size ( ) == 0 )
throw new ServiceException ( "没有部门信息" ) ;
return list;
}
@Override
public List< Node> findZTreeNodes ( ) {
List< Node> list=
sysDeptDao. findZTreeNodes ( ) ;
if ( list== null|| list. size ( ) == 0 )
throw new ServiceException ( "没有部门信息" ) ;
return list;
}
@Override
public int updateObject ( SysDept entity) {
if ( entity== null)
throw new ServiceException ( "保存对象不能为空" ) ;
if ( StringUtils. isEmpty ( entity. getName ( ) ) )
throw new ServiceException ( "部门不能为空" ) ;
int rows;
try {
rows= sysDeptDao. updateObject ( entity) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
throw new ServiceException ( "更新失败" ) ;
}
return rows;
}
@Override
public int saveObject ( SysDept entity) {
if ( entity== null)
throw new ServiceException ( "保存对象不能为空" ) ;
if ( StringUtils. isEmpty ( entity. getName ( ) ) )
throw new ServiceException ( "部门不能为空" ) ;
int rows= sysDeptDao. insertObject ( entity) ;
return rows;
}
@Override
public int deleteObject ( Integer id) {
if ( id== null|| id<= 0 )
throw new ServiceException ( "数据不合法,id=" + id) ;
int childCount= sysDeptDao. getChildCount ( id) ;
if ( childCount> 0 )
throw new ServiceException ( "此元素有子元素,不允许删除" ) ;
int rows= sysDeptDao. deleteObject ( id) ;
if ( rows== 0 )
throw new ServiceException ( "此信息可能已经不存在" ) ;
return rows;
}
}
部门管理Controller控制层
package com. cy. pj. sys. controller;
import org. springframework. beans. factory. annotation. Autowired;
import org. springframework. web. bind. annotation. RequestMapping;
import org. springframework. web. bind. annotation. ResponseBody;
import org. springframework. web. bind. annotation. RestController;
import com. cy. pj. common. pojo. JsonResult;
import com. cy. pj. sys. pojo. SysDept;
import com. cy. pj. sys. service. SysDeptService;
@RestController
@RequestMapping ( "/dept/" )
public class SysDeptController {
@Autowired
private SysDeptService sysDeptService;
@RequestMapping ( "doFindObjects" )
public JsonResult doFindObjects ( ) {
return new JsonResult ( sysDeptService. findObjects ( ) ) ;
}
@RequestMapping ( "doFindZTreeNodes" )
public JsonResult doFindZTreeNodes ( ) {
return new JsonResult ( sysDeptService. findZTreeNodes ( ) ) ;
}
@RequestMapping ( "doUpdateObject" )
public JsonResult doUpdateObject ( SysDept entity) {
sysDeptService. updateObject ( entity) ;
return new JsonResult ( "update ok" ) ;
}
@RequestMapping ( "doSaveObject" )
public JsonResult doSaveObject ( SysDept entity) {
sysDeptService. saveObject ( entity) ;
return new JsonResult ( "save ok" ) ;
}
@RequestMapping ( "doDeleteObject" )
@ResponseBody
public JsonResult doDeleteObject ( Integer id) {
sysDeptService. deleteObject ( id) ;
return new JsonResult ( "delete ok" ) ;
}
}
部门管理页面html
列表页面
< div class = " row" >
< div class = " col-xs-12" >
< div class = " box" >
< div class = " box-header" >
< h3 class = " box-title" > 部门管理</ h3>
< div class = " box-tools" >
< div class = " input-group input-group-sm" style =" width : 100px; " >
< div class = " input-group-btn" >
< button type = " button"
class = " btn btn-default btn-delete" > 删除</ button>
< button type = " button" class = " btn btn-default btn-add" > 添加</ button>
< button type = " button" class = " btn btn-default btn-update" > 修改</ button>
</ div>
</ div>
</ div>
</ div>
< div class = " box-body table-responsive no-padding" >
< table id = " gridTreeTable" class = " table table-hover" >
< thead>
< tr>
< th data-field = " selectItem" data-checkbox = " true" > </ th>
</ tr>
</ thead>
</ table>
</ div>
</ div>
</ div>
</ div>
< script type = " text/javascript" src = " bower_components/treegrid/jquery.treegrid.extension.js" > </ script>
< script type = " text/javascript" src = " bower_components/treegrid/jquery.treegrid.min.js" > </ script>
< script type = " text/javascript" src = " bower_components/treegrid/tree.table.js" > </ script>
< script type = " text/javascript" >
var columns = [
{
field : 'selectItem' ,
radio : true
} ,
{
title : 'ID' ,
field : 'id' ,
visible : false ,
align : 'center' ,
valign : 'middle' ,
width : '80px'
} ,
{
title : '名称' ,
field : 'name' ,
align : 'center' ,
valign : 'middle' ,
sortable : true ,
width : '130px'
} ,
{
title : '上级机构' ,
field : 'parentName' ,
align : 'center' ,
valign : 'middle' ,
sortable : true ,
width : '100px'
} ,
{
title : '排序号' ,
field : 'sort' ,
align : 'center' ,
valign : 'middle' ,
sortable : true ,
width : '70px'
}
] ;
$ ( function ( ) {
doGetObjects ( ) ;
$ ( ".input-group-btn" )
. on ( "click" , ".btn-delete" , doDeleteObject)
. on ( "click" , ".btn-add,.btn-update" , doLoadEditUI) ;
} )
function doLoadEditUI ( ) {
var title;
if ( $ ( this ) . hasClass ( "btn-add" ) ) {
title= "添加部门"
} else if ( $ ( this ) . hasClass ( "btn-update" ) ) {
title= "修改部门"
var rowData= doGetCheckedItem ( ) ;
if ( ! rowData) {
alert ( "请选择一个" ) ;
return ;
}
$ ( "#mainContentId" ) . data ( "rowData" , rowData) ;
}
var url= "dept/dept_edit" ;
$ ( "#mainContentId" ) . load ( url, function ( ) {
$ ( ".box-title" ) . html ( title) ;
} )
}
function doGetCheckedItem ( ) {
var tr=
$ ( "tbody input[type='radio']:checked" )
. parents ( "tr" ) ;
var rowData= tr. data ( "rowData" ) ;
console. log ( rowData) ;
return rowData;
}
function doDeleteObject ( ) {
var url= "dept/doDeleteObject" ;
var id= doGetCheckedId ( ) ;
if ( ! id) {
alert ( "请先选择一条记录" ) ;
return ;
}
var params= { "id" : id} ;
$. post ( url, params, function ( result) {
if ( result. state== 1 ) {
alert ( result. message) ;
doGetObjects ( ) ;
} else {
alert ( result. message) ;
}
} )
}
function doGetCheckedId ( ) {
var selections= $ ( "#gridTreeTable" )
. bootstrapTreeTable ( "getSelections" ) ;
if ( selections. length> 0 ) {
return selections[ 0 ] . id;
}
}
function doGetObjects ( ) {
var tableId= "gridTreeTable" ;
var url= "dept/doFindObjects" ;
var treeTable=
new TreeTable ( tableId, url, columns)
treeTable. setExpandColumn ( 2 ) ;
treeTable. init ( ) ;
}
</ script>
新增、修改页面
< div class = " box box-info" >
< div class = " box-header with-border" >
< h3 class = " box-title" > 添加部门</ h3>
</ div>
< form class = " form-horizontal" >
< div class = " box-body" >
< div class = " form-group" >
< label for = " nameId" class = " col-sm-2 control-label" > 部门名称</ label>
< div class = " col-sm-10" >
< input type = " text" class = " form-control" id = " nameId" placeholder = " 名称" >
</ div>
</ div>
< div class = " form-group" >
< label for = " parentId" class = " col-sm-2 control-label" > 上级部门</ label>
< div class = " col-sm-10" >
< input type = " text" class = " form-control load-sys-dept" readonly = " readonly" id = " parentId" placeholder = " 上级部门" >
</ div>
</ div>
< div class = " form-group" >
< label for = " sortId" class = " col-sm-2 control-label" > 排序号:</ label>
< div class = " col-sm-10" >
< input type = " text" id = " sortId" placeholder = " 排序号" class = " form-control" >
</ div>
</ div>
< div class = " form-group" >
< label for = " noteId" class = " col-sm-2 control-label" > 备注:</ label>
< div class = " col-sm-10" >
< input type = " text" id = " noteId" placeholder = " 备注" class = " form-control" >
</ div>
</ div>
</ div>
< div class = " box-footer" >
< button type = " button" class = " btn btn-default btn-cancel" > Cancel</ button>
< button type = " button" class = " btn btn-info pull-right btn-save" > Save</ button>
</ div>
</ form>
< div class = " layui-layer layui-layer-page layui-layer-molv layer-anim" id = " treeLayer" type = " page" times = " 2" showtime = " 0" contype = " object"
style =" z-index : 59891016; width : 300px; height : 450px; top : 100px; left : 500px; display : none" >
< div class = " layui-layer-title" style =" cursor : move; " > 选择部门</ div>
< div class = " layui-layer-content" style =" height : 358px; " >
< div style =" padding : 10px; " class = " layui-layer-wrap" >
< ul id = " zTreeId" class = " ztree" > </ ul>
</ div>
</ div>
< span class = " layui-layer-setwin" > < a class = " layui-layer-ico layui-layer-close layui-layer-close1 btn-cancel" > </ a> </ span>
< div class = " layui-layer-btn layui-layer-btn-" >
< a class = " layui-layer-btn0 btn-confirm" > 确定</ a>
< a class = " layui-layer-btn1 btn-cancel" > 取消</ a>
</ div>
</ div>
</ div>
< script type = " text/javascript" src = " bower_components/ztree/jquery.ztree.all.min.js" > </ script>
< script type = " text/javascript" src = " bower_components/layer/layer.js" >
</ script>
< script type = " text/javascript" >
var zTree;
var setting = {
data : {
simpleData : {
enable : true ,
idKey : "id" ,
pIdKey : "parentId" ,
rootPId : null
}
}
}
$ ( function ( ) {
$ ( ".form-horizontal" )
. on ( "click" , ".load-sys-dept" , doLoadZtreeNodes)
$ ( ".box-footer" )
. on ( "click" , ".btn-cancel" , doCancel)
. on ( "click" , ".btn-save" , doSaveOrUpdate) ;
$ ( "#treeLayer" )
. on ( "click" , ".btn-confirm" , doSetSelectNode)
. on ( "click" , ".btn-cancel" , doHideTree)
var data= $ ( "#mainContentId" ) . data ( "rowData" ) ;
if ( data) doInitEditFormData ( data) ;
} ) ;
function doInitEditFormData ( data) {
$ ( "#nameId" ) . val ( data. name) ;
$ ( "#sortId" ) . val ( data. sort) ;
$ ( "#noteId" ) . val ( data. note) ;
$ ( "#parentId" ) . val ( data. parentName) ;
$ ( "#parentId" ) . data ( "parentId" , data. parentId) ;
}
function doCancel ( ) {
$ ( "#mainContentId" ) . load ( "dept/dept_list" , function ( ) {
$ ( "#mainContentId" ) . removeData ( ) ;
$ ( "#parentId" ) . removeData ( ) ;
} )
}
function doSaveOrUpdate ( ) {
var params = doGetEditFormData ( ) ;
var rowData = $ ( "#mainContentId" ) . data ( "rowData" ) ;
if ( rowData) params. id = rowData. id;
var insertUrl = "dept/doSaveObject" ;
var updateUrl = "dept/doUpdateObject" ;
var url = rowData ? updateUrl : insertUrl;
$. post ( url, params, function ( result) {
if ( result. state == 1 ) {
alert ( result. message) ;
doCancel ( ) ;
} else {
alert ( result. message) ;
}
} ) ;
}
function doGetEditFormData ( ) {
var params = {
name : $ ( "#nameId" ) . val ( ) ,
sort : $ ( "#sortId" ) . val ( ) ,
note : $ ( "#noteId" ) . val ( ) ,
parentId : $ ( "#parentId" ) . data ( "parentId" )
}
return params;
}
function doHideTree ( ) {
$ ( "#treeLayer" ) . css ( "display" , "none" ) ;
}
function doSetSelectNode ( ) {
var nodes = zTree. getSelectedNodes ( ) ;
if ( nodes. length == 0 )
return ;
var node = nodes[ 0 ] ;
console. log ( node) ;
$ ( "#parentId" ) . data ( "parentId" , node. id) ;
$ ( "#parentId" ) . val ( node. name) ;
doHideTree ( ) ;
}
function doLoadZtreeNodes ( ) {
$ ( "#treeLayer" ) . css ( "display" , "block" ) ;
var url = "dept/doFindZTreeNodes" ;
$. getJSON ( url, function ( result) {
if ( result. state == 1 ) {
zTree = $. fn. zTree
. init ( $ ( "#zTreeId" ) , setting, result. data) ;
} else {
alert ( result. message) ;
}
} )
}
</ script>