用户模块
Controller
package com.atguigu.atcrowdfunding.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
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 org.springframework.web.bind.annotation.ResponseBody;
import com.atguigu.atcrowdfunding.bean.TAdmin;
import com.atguigu.atcrowdfunding.bean.TRole;
import com.atguigu.atcrowdfunding.service.TAdminService;
import com.atguigu.atcrowdfunding.service.TRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
@Controller
public class TAdminController {
@Autowired
TAdminService adminService ;
@Autowired
TRoleService roleService ;
Logger log = LoggerFactory.getLogger(TAdminController.class);
@ResponseBody
@RequestMapping("/admin/doUnAssign")
public String doUnAssign(Integer[] roleId,Integer adminId) {
log.debug("adminId={}",adminId);
for (Integer rId : roleId) {
log.debug("roleId={}",rId);
}
roleService.deleteAdminAndRoleRelationship(roleId,adminId);
return "ok";
}
//"id=5&id=7&id=8&adminId=1"
@ResponseBody
@RequestMapping("/admin/doAssign")
public String doAssign(Integer[] roleId,Integer adminId) {
log.debug("adminId={}",adminId);
for (Integer rId : roleId) {
log.debug("roleId={}",rId);
}
roleService.saveAdminAndRoleRelationship(roleId,adminId);
return "ok";
}
@RequestMapping("/admin/toAssign")
public String toAssign(String id,Model model) {
//1.查询所有角色
List<TRole> allList = roleService.listAllRole();
//2.根据用户id查询已经拥有的角色id
List<Integer> roleIdList = roleService.getRoleIdByAdminId(id);
//3.将所有角色,进行划分:
List<TRole> assignList = new ArrayList<TRole>();
List<TRole> unAssignList = new ArrayList<TRole>();
model.addAttribute("assignList", assignList);
model.addAttribute("unAssignList", unAssignList);
for(TRole role:allList) {
if(roleIdList.contains(role.getId())) {//4.已分配角色集合
assignList.add(role);
}else {//5.未分配角色集合
unAssignList.add(role);
}
}
return "admin/assignRole";
}
@RequestMapping("/admin/doDeleteBatch") // ids = "1,2,3,4,5";
public String doDelete(String ids,Integer pageNum) {
List<Integer> idList = new ArrayList<Integer>();
String[] split = ids.split(",");
for (String idStr : split) {
int id = Integer.parseInt(idStr);
idList.add(id);
}
adminService.deleteBatch(idList);
return "redirect:/admin/index?pageNum="+pageNum;
}
@RequestMapping("/admin/doDelete")
public String doDelete(Integer id,Integer pageNum) {
adminService.deleteTAdmin(id);
return "redirect:/admin/index?pageNum="+pageNum;
}
@RequestMapping("/admin/doUpdate")
public String doUpdate(TAdmin admin,Integer pageNum) {
adminService.updateTAdmin(admin);
return "redirect:/admin/index?pageNum="+pageNum;
}
@RequestMapping("/admin/toUpdate")
public String toUpdate(Integer id,Model model) {
TAdmin admin = adminService.getTAdminById(id);
model.addAttribute("admin", admin);
return "admin/update";
}
@PreAuthorize("hasRole('PM - 项目经理')")
@RequestMapping("/admin/doAdd")
public String doAdd(TAdmin admin) {
adminService.saveTAdmin(admin);
//return "redirect:/admin/index";
return "redirect:/admin/index?pageNum="+Integer.MAX_VALUE;
}
@RequestMapping("/admin/toAdd")
public String toAdd() {
return "admin/add";
}
@RequestMapping("/admin/index")
public String index(@RequestParam(value="pageNum",required=false,defaultValue="1") Integer pageNum,
@RequestParam(value="pageSize",required=false,defaultValue="2") Integer pageSize,
Model model,
@RequestParam(value="condition",required=false,defaultValue="") String condition) {
log.debug("pageNum={}",pageNum);
log.debug("pageSize={}",pageSize);
log.debug("condition={}",condition);
PageHelper.startPage(pageNum, pageSize); //线程绑定
Map<String,Object> paramMap = new HashMap<String,Object>();
paramMap.put("condition", condition);
PageInfo<TAdmin> page = adminService.listAdminPage(paramMap);
model.addAttribute("page", page);
return "admin/index";
}
}
service层
package com.atguigu.atcrowdfunding.service.impl;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.atguigu.atcrowdfunding.bean.TAdmin;
import com.atguigu.atcrowdfunding.bean.TAdminExample;
import com.atguigu.atcrowdfunding.bean.TAdminExample.Criteria;
import com.atguigu.atcrowdfunding.exception.LoginException;
import com.atguigu.atcrowdfunding.mapper.TAdminMapper;
import com.atguigu.atcrowdfunding.service.TAdminService;
import com.atguigu.atcrowdfunding.util.AppDateUtils;
import com.atguigu.atcrowdfunding.util.Const;
import com.atguigu.atcrowdfunding.util.MD5Util;
import com.github.pagehelper.PageInfo;
@Service
public class TAdminServiceImpl implements TAdminService {
@Autowired
TAdminMapper adminMapper;
@Override
public TAdmin getTAdminByLogin(Map<String, Object> paramMap) {
//密码加密
//查询用户
String loginacct = (String) paramMap.get("loginacct");
String userpswd = (String) paramMap.get("userpswd");
//判断用户是否为null
TAdminExample example = new TAdminExample();
example.createCriteria().andLoginacctEqualTo(loginacct);
List<TAdmin> list = adminMapper.selectByExample(example);
if (list != null && list.size() == 1) {
TAdmin admin = list.get(0);
if (admin.getUserpswd().equals(MD5Util.digest(userpswd))) {
return admin;
}else {
throw new LoginException(Const.LOGIN_USERPSWD_ERROR);
}
}else {
throw new LoginException(Const.LOGIN_LOGINACCT_ERROR);
}
}
@Override
public PageInfo<TAdmin> listAdminPage(Map<String, Object> paraMap) {
String condition = (String) paraMap.get("condition");
TAdminExample examle = new TAdminExample();
if (!"".equals(condition)) {
examle.createCriteria().andLoginacctLike("%"+condition+"%");
Criteria criteria2 = examle.createCriteria();
criteria2.andUsernameLike("%+condition+%");
Criteria criteria3 = examle.createCriteria();
criteria3.andUsernameLike("%+condition+%");
examle.or(criteria2);
examle.or(criteria3);
}
TAdminExample example = new TAdminExample();
/* example.setOrderByClause("createtime desc"); */
List<TAdmin> list = adminMapper.selectByExample(example);
PageInfo<TAdmin> page = new PageInfo<TAdmin>(list,5);
return page;
}
@Override
public void saveTAdmin(TAdmin admin) {
admin.setUserpswd(MD5Util.digest(Const.DEFAULT_USERPSWD));
admin.setCreatetime(AppDateUtils.getFormatTime());
adminMapper.insertSelective(admin);//有选择性的保存
}
@Override
public TAdmin getTAdminById(Integer id) {
return adminMapper.selectByPrimaryKey(id);
}
@Override
public void updateTAdmin(TAdmin admin) {
adminMapper.updateByPrimaryKeySelective(admin);
}
@Override
public void deleteTAdmin(Integer id) {
adminMapper.deleteByPrimaryKey(id);
}
@Override
public void deleteBatch(List<Integer> idList) {
adminMapper.deleteBatch(idList);
}
}
页面
<%@ page language="java" contentType="text/html;
charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c"
uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="zh_CN">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<%@ include file="/WEB-INF/jsp/common/css.jsp" %>
<style>
.tree li {
list-style-type: none;
cursor:pointer;
}
table tbody tr:nth-child(odd){background:#F4F4F4;}
table tbody td:nth-child(even){color:#C00;}
</style>
</head>
<body>
<jsp:include
page="/WEB-INF/jsp/common/top.jsp"></jsp:include>
<div class="container-fluid">
<div class="row">
<jsp:include
page="/WEB-INF/jsp/common/sidebar.jsp"></jsp:include>
<div class="col-sm-9 col-sm-offset-3 col-md-10
col-md-offset-2 main">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"><i
class="glyphicon glyphicon-th"></i> 数据列表</h3>
</div>
<div class="panel-body">
<form id="queryForm" class="form-inline" role="form"
style="float:left;" action="${PATH}/admin/index"
method="post">
<div class="form-group has-feedback">
<div class="input-group">
<div class="input-group-addon">查询条件</div>
<input class="form-control has-success"
name="condition" value="${param.condition}" type="text"
placeholder="请输入查询条件">
</div>
</div>
<button type="button" class="btn btn-warning"
onclick="$('#queryForm').submit()"><i class="glyphicon
glyphicon-search"></i> 查询</button>
</form>
<button id="deleteBatchBtn" type="button" class="btn
btn-danger" style="float:right;margin-left:10px;" ><i
class=" glyphicon glyphicon-remove"></i> 删除</button>
<button type="button" class="btn btn-primary"
style="float:right;"
onclick="window.location.href='${PATH}/admin/toAdd'"><i
class="glyphicon glyphicon-plus"></i> 新增</button>
<br>
<hr style="clear:both;">
<div class="table-responsive">
<table class="table table-bordered">
<thead>
<tr >
<th width="30">#</th>
<th width="30"><input id="selectAll"
type="checkbox"></th>
<th>账号</th>
<th>名称</th>
<th>邮箱地址</th>
<th width="100">操作</th>
</tr>
</thead>
<tbody>
<c:forEach items="${page.list}"
var="admin">
<tr>
<td>${status.count}</td>
<td><input type="checkbox"
<u>adminId</u>="${admin.id}"></td>
<td>${admin.loginacct}</td>
<td>${admin.username}</td>
<td>${admin.email}</td>
<td>
<button type="button"
class="btn btn-success btn-xs"
onclick="window.location.href='${PATH}/admin/toAssign?id=${admin.id}'"><i class=" glyphicon
glyphicon-check"></i></button>
<button type="button"
class="btn btn-primary btn-xs"
onclick="window.location.href='${PATH}/admin/toUpdate?pageNum=${page.pageNum}&id=${admin.id}'"><i class="
glyphicon glyphicon-pencil"></i></button>
<button type="button"
<u>adminId</u>="${admin.id}" class="deleteBtnClass btn
btn-danger btn-xs"><i class=" glyphicon
glyphicon-remove"></i></button>
</td>
</tr>
</c:forEach>
</tbody>
<tfoot>
<tr >
<td colspan="6" align="center">
<ul class="pagination">
<c:if
test="${page.isFirstPage }">
<li
class="disabled"><a href="#">上一页</a></li>
</c:if>
<c:if
test="${!page.isFirstPage }">
<li><a
href="${PATH}/admin/index?condition=${param.condition}&pageNum=${page.pageNum-1}">上一页</a></li>
</c:if>
<c:forEach
items="${page.navigatepageNums}" var="num">
<c:if test="${num
== page.pageNum }">
<li
class="active"><a
href="${PATH}/admin/index?condition=${param.condition}&pageNum=${num}">${num} <span
class="sr-only">(current)</span></a></li>
</c:if>
<c:if test="${num
!= page.pageNum }">
<li><a
href="${PATH}/admin/index?condition=${param.condition}&pageNum=${num}">${num}</a></li>
</c:if>
</c:forEach>
<c:if
test="${page.isLastPage }">
<li
class="disabled"><a href="#">下一页</a></li>
</c:if>
<c:if
test="${!page.isLastPage }">
<li><a
href="${PATH}/admin/index?condition=${param.condition}&pageNum=${page.pageNum+1}">下一页</a></li>
</c:if>
</ul>
</td>
</tr>
</tfoot>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<%@ include file="/WEB-INF/jsp/common/js.jsp" %>
<script type="text/javascript">
$(function () {
$(".list-group-item").click(function(){
if ( $(this).find("ul") ) {
$(this).toggleClass("tree-closed");
if (
$(this).hasClass("tree-closed") ) {
$("ul",
this).hide("fast");
} else {
$("ul",
this).show("fast");
}
}
});
});
$(".deleteBtnClass").click(function(){
var id =$(this).attr("adminId");
layer.confirm('您是否确定删除该条信息?',{btn:['确定','取消']},function(index){
window.location.href="${PATH}/admin/doDelete?pageNum=${page.pageNum}&id="+id;
layer.close(index);
},function(){
layer.close(index);
});
});
$("#selectAll").click(function(){
$("tbody
input[type='checkbox']").prop("checked",this.checked);
});
$("#deleteBatchBtn").click(function(){
var checkedBoxList = $("tbody
input[type='checkbox']:checked");
console.log(checkedBoxList);
if(checkedBoxList.length==0){
layer.msg("请选中再删除!");
return false ;
}
//var ids = '1,2,3,4,5';
var ids = '';
var array = new Array();
$.each(checkedBoxList,function(i,e){
var adminId = $(e).attr("adminId");
//获取自定义属性
array.push(adminId);
});
ids = array.join(",");
console.log(ids);
layer.confirm('您是否确定删除该些数据?',{btn:['确定','取消']},function(index){
window.location.href="${PATH}/admin/doDeleteBatch?pageNum=${page.pageNum}&ids="+ids;
layer.close(index);
},function(index){
layer.close(index);
});
});
</script>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="zh_CN">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<%@ include file="/WEB-INF/jsp/common/css.jsp" %>
<style>
.tree li {
list-style-type: none;
cursor:pointer;
}
</style>
</head>
<body>
<jsp:include page="/WEB-INF/jsp/common/top.jsp"></jsp:include>
<div class="container-fluid">
<div class="row">
<jsp:include page="/WEB-INF/jsp/common/sidebar.jsp"></jsp:include>
<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
<ol class="breadcrumb">
<li><a href="#">首页</a></li>
<li><a href="#">数据列表</a></li>
<li class="active">新增</li>
</ol>
<div class="panel panel-default">
<div class="panel-heading">表单数据<div style="float:right;cursor:pointer;" data-toggle="modal" data-target="#myModal"><i class="glyphicon glyphicon-question-sign"></i></div></div>
<div class="panel-body">
<form id="addForm" action="${PATH}/admin/doAdd" method="post">
<div class="form-group">
<label for="exampleInputPassword1">登陆账号</label>
<input type="text" class="form-control" id="loginacct" name="loginacct" placeholder="请输入登陆账号">
</div>
<div class="form-group">
<label for="exampleInputPassword1">用户名称</label>
<input type="text" class="form-control" id="username" name="username" placeholder="请输入用户名称">
</div>
<div class="form-group">
<label for="exampleInputEmail1">邮箱地址</label>
<input type="email" class="form-control" id="email" name="email" placeholder="请输入邮箱地址">
<p class="help-block label label-warning">请输入合法的邮箱地址, 格式为: xxxx@xxxx.com</p>
</div>
<button id="saveBtn" type="button" class="btn btn-success"><i class="glyphicon glyphicon-plus"></i> 新增</button>
<button type="button" class="btn btn-danger"><i class="glyphicon glyphicon-refresh"></i> 重置</button>
</form>
</div>
</div>
</div>
</div>
</div>
<%@ include file="/WEB-INF/jsp/common/js.jsp" %>
<script type="text/javascript">
$(function () {
$(".list-group-item").click(function(){
if ( $(this).find("ul") ) {
$(this).toggleClass("tree-closed");
if ( $(this).hasClass("tree-closed") ) {
$("ul", this).hide("fast");
} else {
$("ul", this).show("fast");
}
}
});
});
$("#saveBtn").click(function(){
$("#addForm").submit();
});
</script>
</body>
</html>
<%@ page language="java" contentType="text/html;
charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="zh_CN">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<%@ include file="/WEB-INF/jsp/common/css.jsp" %>
<style>
.tree li {
list-style-type: none;
cursor:pointer;
}
</style>
</head>
<body>
<jsp:include
page="/WEB-INF/jsp/common/top.jsp"></jsp:include>
<div class="container-fluid">
<div class="row">
<jsp:include
page="/WEB-INF/jsp/common/sidebar.jsp"></jsp:include>
<div class="col-sm-9 col-sm-offset-3 col-md-10
col-md-offset-2 main">
<ol class="breadcrumb">
<li><a href="#">首页</a></li>
<li><a href="#">数据列表</a></li>
<li class="active">修改</li>
</ol>
<div class="panel panel-default">
<div class="panel-heading">表单数据<div
style="float:right;cursor:pointer;" data-toggle="modal"
data-target="#myModal"><i class="glyphicon
glyphicon-question-sign"></i></div></div>
<div class="panel-body">
<form id="updateForm"
action="${PATH}/admin/doUpdate" method="post">
<div class="form-group">
<label
for="exampleInputPassword1">登陆账号</label>
<input type="hidden"
name="pageNum" value="${param.pageNum}">
<input type="hidden" name="id"
value="${admin.id }">
<input type="text"
class="form-control" id="loginacct" name="loginacct"
value="${admin.loginacct }" placeholder="请输入登陆账号">
</div>
<div class="form-group">
<label
for="exampleInputPassword1">用户名称</label>
<input type="text"
class="form-control" id="username" name="username"
value="${admin.username }" placeholder="请输入用户名称">
</div>
<div class="form-group">
<label
for="exampleInputEmail1">邮箱地址</label>
<input type="email"
class="form-control" id="email" name="email"
value="${admin.email }" placeholder="请输入邮箱地址">
<p class="help-block label
label-warning">请输入合法的邮箱地址, 格式为:
<u>xxxx@xxxx.com</u></p>
</div>
<button id="updateBtn" type="button"
class="btn btn-success"><i class="glyphicon
glyphicon-plus"></i> 修改</button>
<button type="button" class="btn
btn-danger"><i class="glyphicon glyphicon-refresh"></i>
重置</button>
</form>
</div>
</div>
</div>
</div>
</div>
<%@ include file="/WEB-INF/jsp/common/js.jsp" %>
<script type="text/javascript">
$(function () {
$(".list-group-item").click(function(){
if ( $(this).find("ul") ) {
$(this).toggleClass("tree-closed");
if (
$(this).hasClass("tree-closed") ) {
$("ul",
this).hide("fast");
} else {
$("ul",
this).show("fast");
}
}
});
});
$("#updateBtn").click(function(){
$("#updateForm").submit();
});
</script>
</body>
</html>
mapper
<mapper
namespace="com.atguigu.atcrowdfunding.mapper.TAdminMapper">
<resultMap id="BaseResultMap"
type="com.atguigu.atcrowdfunding.bean.TAdmin">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="loginacct" jdbcType="VARCHAR"
property="loginacct" />
<result column="userpswd" jdbcType="CHAR"
property="userpswd" />
<result column="username" jdbcType="VARCHAR"
property="username" />
<result column="email" jdbcType="VARCHAR"
property="email" />
<result column="createtime" jdbcType="CHAR"
property="createtime" />
</resultMap>
<delete id="deleteBatch">
delete from t_admin where id in
<foreach collection="idList" item="adminId"
open="(" separator="," close=")">
${adminId}
</foreach>
</delete>