用户子模块
- 1.实体类
package com.itheima.domain.system;
public class Dept {
private String id;
private String deptName;
private String parentId;
private Integer state;
private Dept parent;
public Dept getParent() {
return parent;
}
public void setParent(Dept parent) {
this.parent = parent;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public String getParentId() {
return parentId;
}
public void setParentId(String parentId) {
this.parentId = parentId;
}
public Integer getState() {
return state;
}
public void setState(Integer state) {
this.state = state;
}
}
- 2.dao层
package com.itheima.dao.system;
import com.itheima.domain.system.Dept;
import java.util.List;
public interface DeptDao {
int save(Dept dept);
int delete(Dept dept);
int update(Dept dept);
Dept findById(String id);
List<Dept> findAll();
}
- 3.resources
<?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.itheima.dao.system.DeptDao">
<!--配置实体类属性和数据库表中列的对应关系-->
<resultMap id="BaseResultMap" type="com.itheima.domain.system.Dept">
<id column="dept_id" jdbcType="VARCHAR" property="id"/>
<result column="dept_name" jdbcType="VARCHAR" property="deptName"/>
<result column="parent_id" jdbcType="VARCHAR" property="parentId"/>
<result column="state" jdbcType="DECIMAL" property="state"/>
<!--关联关系-->
<association
property="parent"
javaType="com.itheima.domain.system.Dept"
column="parent_id"
select="com.itheima.dao.system.DeptDao.findById"
/>
</resultMap>
<!--配置查询的列名公共SQL语句-->
<sql id="Base_Column_List">
dept_id, dept_name, parent_id, state
</sql>
<!--配置查询所有,带条件-->
<select id="findAll" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from ss_dept
</select>
<!--配置根据ID查询-->
<select id="findById" parameterType="java.lang.String" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from ss_dept
where dept_id = #{id,jdbcType=VARCHAR}
</select>
<!--配置根据id删除-->
<delete id="delete" parameterType="java.lang.String">
delete from ss_dept where dept_id = #{id,jdbcType=VARCHAR}
</delete>
<!--配置全字段插入,当某个字段没有值时,插入null-->
<insert id="save" parameterType="com.itheima.domain.system.Dept">
insert into ss_dept (dept_id, dept_name, parent_id,state)
values (#{id,jdbcType=VARCHAR}, #{deptName,jdbcType=VARCHAR}, #{parentId,jdbcType=VARCHAR},#{state,jdbcType=DECIMAL})
</insert>
<!--配置全字段更新,当提供的数据为null时,数据库数据会被更新为null-->
<update id="update" parameterType="com.itheima.domain.system.Dept">
update ss_dept
set dept_name = #{deptName,jdbcType=VARCHAR},
parent_id = #{parentId,jdbcType=VARCHAR},
state = #{state,jdbcType=DECIMAL}
where dept_id = #{id,jdbcType=VARCHAR}
</update>
</mapper>
- 4.service层
package com.itheima.service.system;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.system.Dept;
import java.util.List;
public interface DeptService {
/**
* 添加
* @param dept
* @return
*/
void save(Dept dept);
/**
* 删除
* @param dept
* @return
*/
void delete(Dept dept);
/**
* 修改
* @param dept
* @return
*/
void update(Dept dept);
/**
* 查询单个
* @param id 查询的条件(id)
* @return 查询的结果,单个对象
*/
Dept findById(String id);
/**
* 查询全部的数据
* @return 全部数据的列表对象
*/
List<Dept> findAll();
/**
* 分页查询数据
* @param page 页码
* @param size 每页显示的数据总量
* @return
*/
PageInfo findAll(int page, int size);
}
package com.itheima.service.system.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.system.DeptDao;
import com.itheima.domain.system.Dept;
import com.itheima.factory.MapperFactory;
import com.itheima.service.system.DeptService;
import com.itheima.utils.TransactionUtil;
import org.apache.ibatis.session.SqlSession;
import java.util.List;
import java.util.UUID;
public class DeptServiceImpl implements DeptService {
@Override
public void save(Dept dept) {
SqlSession sqlSession = null;
try{
//1.获取SqlSession
sqlSession = MapperFactory.getSqlSession();
//2.获取Dao
DeptDao deptDao = MapperFactory.getMapper(sqlSession,DeptDao.class);
//id使用UUID的生成策略来获取
String id = UUID.randomUUID().toString();
dept.setId(id);
//3.调用Dao层操作
deptDao.save(dept);
//4.提交事务
TransactionUtil.commit(sqlSession);
}catch (Exception e){
TransactionUtil.rollback(sqlSession);
throw new RuntimeException(e);
//记录日志
}finally {
try {
TransactionUtil.close(sqlSession);
}catch (Exception e){
e.printStackTrace();
}
}
}
@Override
public void delete(Dept dept) {
SqlSession sqlSession = null;
try{
//1.获取SqlSession
sqlSession = MapperFactory.getSqlSession();
//2.获取Dao
DeptDao deptDao = MapperFactory.getMapper(sqlSession,DeptDao.class);
//3.调用Dao层操作
deptDao.delete(dept);
//4.提交事务
TransactionUtil.commit(sqlSession);
}catch (Exception e){
TransactionUtil.rollback(sqlSession);
throw new RuntimeException(e);
//记录日志
}finally {
try {
TransactionUtil.close(sqlSession);
}catch (Exception e){
e.printStackTrace();
}
}
}
@Override
public void update(Dept dept) {
SqlSession sqlSession = null;
try{
//1.获取SqlSession
sqlSession = MapperFactory.getSqlSession();
//2.获取Dao
DeptDao deptDao = MapperFactory.getMapper(sqlSession,DeptDao.class);
//3.调用Dao层操作
deptDao.update(dept);
//4.提交事务
TransactionUtil.commit(sqlSession);
}catch (Exception e){
TransactionUtil.rollback(sqlSession);
throw new RuntimeException(e);
//记录日志
}finally {
try {
TransactionUtil.close(sqlSession);
}catch (Exception e){
e.printStackTrace();
}
}
}
@Override
public Dept findById(String id) {
SqlSession sqlSession = null;
try{
//1.获取SqlSession
sqlSession = MapperFactory.getSqlSession();
//2.获取Dao
DeptDao deptDao = MapperFactory.getMapper(sqlSession,DeptDao.class);
//3.调用Dao层操作
return deptDao.findById(id);
}catch (Exception e){
throw new RuntimeException(e);
//记录日志
}finally {
try {
TransactionUtil.close(sqlSession);
}catch (Exception e){
e.printStackTrace();
}
}
}
@Override
public List<Dept> findAll() {
SqlSession sqlSession = null;
try{
//1.获取SqlSession
sqlSession = MapperFactory.getSqlSession();
//2.获取Dao
DeptDao deptDao = MapperFactory.getMapper(sqlSession,DeptDao.class);
//3.调用Dao层操作
return deptDao.findAll();
}catch (Exception e){
throw new RuntimeException(e);
//记录日志
}finally {
try {
TransactionUtil.close(sqlSession);
}catch (Exception e){
e.printStackTrace();
}
}
}
@Override
public PageInfo findAll(int page, int size) {
SqlSession sqlSession = null;
try{
//1.获取SqlSession
sqlSession = MapperFactory.getSqlSession();
//2.获取Dao
DeptDao deptDao = MapperFactory.getMapper(sqlSession,DeptDao.class);
//3.调用Dao层操作
PageHelper.startPage(page,size);
List<Dept> all = deptDao.findAll();
PageInfo pageInfo = new PageInfo(all);
return pageInfo;
}catch (Exception e){
throw new RuntimeException(e);
//记录日志
}finally {
try {
TransactionUtil.close(sqlSession);
}catch (Exception e){
e.printStackTrace();
}
}
}
}
- 5.Controller层
package com.itheima.web.controller.system;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.system.Dept;
import com.itheima.utils.BeanUtil;
import com.itheima.web.controller.BaseServlet;
import org.apache.commons.lang3.StringUtils;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
// uri:/system/dept?operation=list
@WebServlet("/system/dept")
public class DeptServlet extends BaseServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String operation = request.getParameter("operation");
if("list".equals(operation)){
this.list(request,response);
}else if("toAdd".equals(operation)){
this.toAdd(request,response);
}else if("save".equals(operation)){
this.save(request, response);
}else if("toEdit".equals(operation)){
this.toEdit(request,response);
}else if("edit".equals(operation)){
this.edit(request,response);
}else if("delete".equals(operation)){
this.delete(request,response);
}
}
private void list(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
//进入列表页
//获取数据
int page = 1;
int size = 5;
if(StringUtils.isNotBlank(request.getParameter("page"))){
page = Integer.parseInt(request.getParameter("page"));
}
if(StringUtils.isNotBlank(request.getParameter("size"))){
size = Integer.parseInt(request.getParameter("size"));
}
PageInfo all = deptService.findAll(page, size);
//将数据保存到指定的位置
request.setAttribute("page",all);
//跳转页面
request.getRequestDispatcher("/WEB-INF/pages/system/dept/list.jsp").forward(request,response);
}
private void toAdd(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
//加载所有的部门信息放入到deptList
List<Dept> all = deptService.findAll();
request.setAttribute("deptList",all);
//跳转页面
request.getRequestDispatcher("/WEB-INF/pages/system/dept/add.jsp").forward(request,response);
}
private void save(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
//将数据获取到,封装成一个对象
Dept dept = BeanUtil.fillBean(request,Dept.class,"yyyy-MM-dd");
//调用业务层接口save
// DeptService deptService = new DeptServiceImpl();
deptService.save(dept);
//跳转回到页面list
//list(request,response);
response.sendRedirect(request.getContextPath()+"/system/dept?operation=list");
}
private void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//查询要修改的数据findById
String id = request.getParameter("id");
// DeptService deptService = new DeptServiceImpl();
Dept dept = deptService.findById(id);
//加载所有的部门信息放入到deptList
List<Dept> all = deptService.findAll();
request.setAttribute("deptList",all);
//将数据加载到指定区域,供页面获取
request.setAttribute("dept",dept);
//跳转页面
request.getRequestDispatcher("/WEB-INF/pages/system/dept/update.jsp").forward(request,response);
}
private void edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
//将数据获取到,封装成一个对象
Dept dept = BeanUtil.fillBean(request,Dept.class,"yyyy-MM-dd");
//调用业务层接口save
// DeptService deptService = new DeptServiceImpl();
deptService.update(dept);
//跳转回到页面list
//list(request,response);
response.sendRedirect(request.getContextPath()+"/system/dept?operation=list");
}
private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
//将数据获取到,封装成一个对象
Dept dept = BeanUtil.fillBean(request,Dept.class);
//调用业务层接口save
// DeptService deptService = new DeptServiceImpl();
deptService.delete(dept);
//跳转回到页面list
//list(request,response);
response.sendRedirect(request.getContextPath()+"/system/dept?operation=list");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}
}
- 6.表现层
<li id="sys-dept">
<a href="${ctx}/system/dept?operation=list" onclick="setSidebarActive(this)" target="iframe">
<i class="fa fa-circle-o"></i>部门管理
</a>
</li>
list.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="../../base.jsp"%>
<!DOCTYPE html>
<html>
<head>
<!-- 页面meta -->
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>黑马面面管理系统</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">
</head>
<script>
function deleteById() {
var id = getCheckId()
if(id) {
if(confirm("你确认要删除此条记录吗?")) {
location.href="${ctx}/system/dept?operation=delete&id="+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="${ctx}/system/dept?operation=toAdd"'><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="text-center">操作</th>
</tr>
</thead>
<tbody>
<c:forEach items="${page.list}" var="dept">
<tr>
<td><input type="checkbox" name="id" value="${dept.id }"/></td>
<td>${dept.deptName }</td>
<td>${dept.parent.deptName }</td>
<td>${dept.state ==0?'未启用':'使用中'}</td>
<th class="text-center">
<button type="button" class="btn bg-olive btn-xs" onclick='location.href="${ctx}/system/dept?operation=toEdit&id=${dept.id}"'>编辑</button>
</th>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
<div class="box-footer">
<jsp:include page="../../common/page.jsp">
<jsp:param value="${ctx}/system/dept?operation=list" name="pageUrl"/>
</jsp:include>
</div>
</div>
</section>
</div>
</body>
</html>
- add.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="../../base.jsp"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>黑马面面管理系统</title>
</head>
<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="${ctx}/system/dept?operation=save" 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">
<option value="">请选择</option>
<c:forEach items="${deptList}" var="item">
<option ${dept.parent.id == item.id ?'selected':''} value="${item.id}">${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" ${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>
</html>
update.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="../../base.jsp"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>黑马面面管理系统</title>
</head>
<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="${ctx}/system/dept?operation=edit" method="post">
<input type="hidden" name="id" value="${dept.id}">
<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="${deptList}" var="item">
<option ${dept.parent.id == item.id ?'selected':''} value="${item.id}">${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" ${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>
</html>