控制层【部分展示】
package com.peng.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.peng.pojo.Dept;
import com.peng.service.DeptService;
@Controller(value = "deptServlet")
@RequestMapping(value = "/sysadmin/dept")
public class DeptController {
@Autowired
@Qualifier(value = "deptService")
private DeptService deptService;
@RequestMapping("/list")
public String findAllAction(Model model) {
List deptList = deptService.findAll();
model.addAttribute("deptList", deptList);
return "/sysadmin/dept/jDeptList";
}
// 点击“启用”按钮,实现禁用按钮--批量操作--非必传参数
@RequestMapping("/stop")
public String stop(@RequestParam(name = "deptId", required = false) String[] deptIds) {
if (deptIds != null) {
deptService.changeState(0, deptIds);
}
return "redirect:/sysadmin/dept/list";
}
// 点击“禁用”按钮,实现启用按钮--批量操作--非必传参数
@RequestMapping("/start")
public String start(@RequestParam(name = "deptId", required = false) String[] deptIds) {
if (deptIds != null) {
deptService.changeState(1, deptIds);
}
return "redirect:/sysadmin/dept/list";
}
// 点击“删除”按钮--批量操作--非必传参数
@RequestMapping("/delete")
public String delete(@RequestParam(name = "deptId", required = false) String[] deptIds) {
if (deptIds != null) {
deptService.deleteDeptByIds(deptIds);
}
return "redirect:/sysadmin/dept/list";
}
// 点击“新增”按钮--增加部门
@RequestMapping("/tocreate")
public String tocreate(Model model) {
List deptList = deptService.findAll();
model.addAttribute("deptList", deptList);
return "/sysadmin/dept/jDeptCreate";
}
// 点击“增加”按钮--保存部门
@RequestMapping("/save")
public String save(Model model, Dept dept) {
try {
deptService.saveDept(dept);
return "redirect:/sysadmin/dept/list";
} catch (Exception e) {
List deptList = deptService.findAll();
model.addAttribute("deptList", deptList);
model.addAttribute("save_error", "新增失败,可能是部门编号重复了!");
return "/sysadmin/dept/jDeptCreate";
}
}
// 点击“修改”按钮
@RequestMapping("/toupdate")
public String toUpdate(Model model, @RequestParam(name = "deptId", required = false) String[] deptIds) {
String deptId = deptIds != null ? deptIds[0] : null;// 只获取第一个
if (deptId == null || "".equals(deptId)) {
model.addAttribute("error_up", "请选择一个部门");
List deptList = deptService.findAll();
model.addAttribute("deptList", deptList);
return "/sysadmin/dept/jDeptList";
}
Dept dept = deptService.findOneById(deptId);
List deptList = deptService.findAll();
model.addAttribute("deptList", deptList);
model.addAttribute("dept", dept);
return "/sysadmin/dept/jDeptUpdate";
}
// 点击“修改”按钮--保存部门
@RequestMapping("/update")
public String update(Model model, Dept dept) {
try {
System.out.println(dept);
deptService.updateDept(dept);
return "redirect:/sysadmin/dept/list";
} catch (Exception e) {
List deptList = deptService.findAll();
model.addAttribute("deptList", deptList);
model.addAttribute("update_error", "更新失败,走神了!");
model.addAttribute("deptList", deptList);
model.addAttribute("dept", dept);
return "/sysadmin/dept/jDeptUpdate";
}
}
// 点击“查看”按钮
@RequestMapping("/toview")
public String showView(Model model, @RequestParam(name = "deptId", required = false) String[] deptIds) {
String deptId = deptIds != null ? deptIds[0] : null;// 只获取第一个
if (deptId == null || "".equals(deptId)) {
model.addAttribute("error_up", "请选择一个部门");
List deptList = deptService.findAll();
model.addAttribute("deptList", deptList);
return "/sysadmin/dept/jDeptList";
}
Dept dept = deptService.findOneById(deptId);
List deptList = deptService.findAll();
model.addAttribute("deptList", deptList);
model.addAttribute("dept", dept);
return "/sysadmin/dept/jDeptView";
}
}
service层【部分展示】
package com.peng.service;
import java.util.List;
import com.peng.pojo.Dept;
public interface DeptService {
/**
* 查询所有的部门
*
* @return
*/
List findAll();
/**
* 批量删除dept
*
* @param deptIds
*/
void deleteDeptByIds(String[] deptIds);
/**
* 批量更改状态
*
* @param state
* @param deptIds
*/
void changeState(int state, String[] deptIds);
/**
* 保存dept
*
* @param dept
*/
void saveDept(Dept dept);
/**
* 通过id查询dept
*
* @param deptId
* @return
*/
Dept findOneById(String deptId);
/**
* 更新部门
*
* @param dept
*/
void updateDept(Dept dept);
}
package com.peng.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import com.peng.mapper.DeptMapper;
import com.peng.pojo.Dept;
@Service(value = "deptService")
public class DeptServiceImpl implements DeptService {
// 注入dao层对象
@Autowired
@Qualifier(value = "deptMapper")
private DeptMapper deptMapper;
public List findAll() {
return deptMapper.findAll();
}
@Override
public void deleteDeptByIds(String[] deptIds) {
deptMapper.deleteDeptByIds(deptIds);
}
@Override
public void changeState(int state, String[] deptIds) {
deptMapper.changeState(state, deptIds);
}
@Override
public void saveDept(Dept dept) {
deptMapper.saveDept(dept);
}
@Override
public Dept findOneById(String deptId) {
return deptMapper.findOneById(deptId);
}
@Override
public void updateDept(Dept dept) {
deptMapper.updateDept(dept);
}
}
dao层
package com.peng.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.peng.pojo.Dept;
public interface DeptMapper {
/**
* 查询所有部门
*/
public List findAll();
/**
* 通过id更改状态
*
* @param state
* @param id
*/
public void changeState(@Param("state") int state, @Param("deptIds") String[] deptIds);
/**
* 批量删除dept
*
* @param deptIds
*/
public void deleteDeptByIds(String[] deptIds);
/**
* 保存dept
*
* @param dept
*/
public void saveDept(Dept dept);
/**
* 通过dept_id查询dept
*
* @param deptId
* @return
*/
public Dept findOneById(String deptId);
/**
* 更新dept
*
* @param dept
*/
public void updateDept(Dept dept);
}
dao层对应的mapper文件
update dept_p set state=#{state} where dept_id in
#{id}
delete from dept_p where dept_id in
#{id}
insert into dept_p
(dept_id,dept_name,parent_id,state)
values(#{deptId},#{deptName},#{parentDept.deptId},#{state});
update dept_p
dept_name=#{deptName},
dept_id=#{deptId},
state=#{state}
where dept_id=#{deptId}
pojo
package com.peng.pojo;
public class Dept extends BaseEntity {
private String deptId;
private Dept parentDept;// 上级部门
private String deptName;
private Integer state;
public String getDeptId() {
return deptId;
}
public void setDeptId(String deptId) {
this.deptId = deptId;
}
public Dept getParentDept() {
return parentDept;
}
public void setParentDept(Dept parentDept) {
this.parentDept = parentDept;
}
public String getdeptName() {
return deptName;
}
public void setdeptName(String deptName) {
this.deptName = deptName;
}
public Integer getState() {
return state;
}
public void setState(Integer state) {
this.state = state;
}
@Override
public String toString() {
return "Dept [deptId=" + deptId + ", parentDept=" + parentDept + ", deptName=" + deptName + ", state=" + state
+ "]";
}
}