(SSM练习)网上商城项目(九)——用户管理

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40788630/article/details/85567312

在后台管理页面中点击用户管理,如图所示:

可以看到和订单管理一样,也是只用删除用户这一个功能,

功能实现顺序:持久层——dao层——service层——controller层——jsp层

一、持久层实现

创建持久化类Buser

package com.po;

public class Buser {
	private Integer id;
	private String bemail;
	private String bpwd;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getBemail() {
		return bemail;
	}
	public void setBemail(String bemail) {
		this.bemail = bemail;
	}
	public String getBpwd() {
		return bpwd;
	}
	public void setBpwd(String bpwd) {
		this.bpwd = bpwd;
	}
}

二、dao层实现

AdminUserDao接口详细代码如下所示:

package com.dao;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import com.po.Buser;

@Repository("adminUserDao")
@Mapper
public interface AdminUserDao {
    public List<Buser> userInfo();
    public int deleteuserManager(Integer id);
}

其映射文件详细代码如下所示

<?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.dao.AdminUserDao">
    <select id="userInfo" resultType="Buser">
        select * from busertable
    </select>
    <delete id="deleteuserManager" parameterType="Integer">
        delete from busertable where id = #{id}
    </delete>
</mapper>

三、实现service层代码

此层由接口及其实现类组成

在com.service.admin包中创建AdminUserService接口,其详细代码如下所示:

package com.service.admin;

import org.springframework.ui.Model;

public interface AdminUserService {
    public String userInfo(Model model);
    public String deleteuserManager(Integer id,Model model);
}

其接口实现类详细代码如下所示:

package com.service.admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;

import com.dao.AdminUserDao;
import com.dao.CartDao;
import com.dao.UserCenterDao;
@Service("adminUserService")
@Transactional
public class AdminUserServiceImpl implements AdminUserService{
	@Autowired
	private AdminUserDao adminUserDao;
	@Autowired
	private CartDao cartDao;
	@Autowired
	private UserCenterDao userCenterDao;
	@Override
	public String userInfo(Model model) {
		model.addAttribute("userList", adminUserDao.userInfo());
		return "admin/userManager";
	}
	@Override
	public String deleteuserManager(Integer id, Model model) {
		//用户有关联
		if(cartDao.selectCart(id).size() > 0 ||
				userCenterDao.myFocus(id).size() > 0||
				userCenterDao.myOrder(id).size() > 0) {
			model.addAttribute("msg", "用户有关联,不能删除!");
			return "forward:/adminUser/userInfo";
		}
		if(adminUserDao.deleteuserManager(id) > 0) 
			model.addAttribute("msg", "成功删除用户!");
		return "forward:/adminUser/userInfo";
	}

}

四、实现controller层

在com.controller.admin包中创建AdminUserController类,其详细代码如下所示:

package com.controller.admin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.service.admin.AdminUserService;

@Controller
@RequestMapping("/adminUser")
public class AdminUserController extends BaseController{
    @Autowired
    private AdminUserService adminUserService;
    @RequestMapping("/userInfo")
    public String userInfo(Model model) {
    	return adminUserService.userInfo(model);
    }
    @RequestMapping("/deleteuserManager")
    public String deleteuserManager(Integer id,Model model) {
    	return adminUserService.deleteuserManager(id, model);
    }
}

五、jsp层实现

在jsp.admin包中创建usermanager.jsp,其详细代码如下所示:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>用户管理</title>
	<link href="css/admin/common.css" type="text/css" rel="stylesheet">
	<style type="text/css">
		table{
			text-align: center;
			border-collapse: collapse
		}
		.bgcolor{
		  	background-color: #F08080;
		}
	</style>
	<script type="text/javascript">
		function changeColor(obj){
			obj.className = "bgcolor";
		}
		function changeColor1(obj){
			obj.className = "";
		}
		function checkDel(id){
  			if(window.confirm("是否删除该用户?")){
  				window.location.href="/ch20/adminUser/deleteuserManager?id="+id;
  			}
  		}
	</script>
</head>
<body>
	<br>
	<table border="1" bordercolor="PaleGreen">
		<tr>
			<th width="200px">用户ID</th>
			<th width="200px">用户E-mail</th>
			<th width="200px">用户密码</th>
			<th width="250px">删除</th>
		</tr>
		<c:forEach var="n" items="${userList}">
		<tr onmousemove="changeColor(this)" onmouseout="changeColor1(this)">
			<td>${n.id}</td>
			<td>${n.bemail}</td>
			<td><input type="password" value="${n.bpwd}" readonly></td>
			<td>
				<a href="javaScript:checkDel('${n.id}')">删除</a>
			</td>
		</tr>
		</c:forEach>
		<tr>
			<td colspan="4">${msg }</td>
		</tr>
	</table>
</body>
</html>

将项目发送发tomcat,重启tomcat,访问后台后查看用户管理,如下所示:

从图片中我们可以看到,有一个有邮箱为[email protected]的用户,若点击后面的删除按钮,此用户的信息将被删除,必须重新注册,才能再次使用此账号进行登录

猜你喜欢

转载自blog.csdn.net/qq_40788630/article/details/85567312