ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] # 步骤 1 : 准备数据 因为用户信息是前台注册的时候增加的,截至目前为止,还没有讲到前台的功能。所以需要手动在数据库中增加数据,以便于观察运行的效果 ~~~ insert into t_user values(null,'a1','123456'); insert into t_user values(null,'a2','123456'); insert into t_user values(null,'a3','123456'); insert into t_user values(null,'a4','123456'); insert into t_user values(null,'a5','123456'); ~~~ # 步骤 2 : 先运行,看到效果,再学习 先将完整的项目(向老师要相关资料),配置运行起来,确认可用之后,再学习做了哪些步骤以达到这样的效果。 # 步骤 3 : 模仿和排错 在确保可运行项目能够正确无误地运行之后,再严格照着教程的步骤,对代码模仿一遍。 模仿过程难免代码有出入,导致无法得到期望的运行结果,此时此刻通过比较**正确答案** ( 可运行项目 ) 和自己的代码,来定位问题所在。 采用这种方式,**学习有效果,排错有效率**,可以较为明显地提升学习速度,跨过学习路上的各个槛。 # 步骤 4 : 页面截图 用户这部分做的比较简单,只有查询,为什么呢? 1. 用户的增加,是交由前台注册行为产生的,后台不需要自己进行增加 2. 用户信息不能删除。 用户信息是最重要的业务信息,不可以删除 3. 用户资料的修改,也应该有前台用户自己进行,而不是后台操作。比如修改密码 ![](https://box.kancloud.cn/8883ecea8240134aa273ba463b427283_1814x426.png) # 步骤5:UserDao和UserDaoImpl UserDao ``` package com.dodoke.dao.inter; import java.util.List; import com.dodoke.bean.User; public interface UserDao { /** * 用户分页查询 * * @param start * @param count * @return */ public List<User> list(int start, int count); /** * 获取用户总数 * * @return */ public int getTotal(); public User get(int id); } ``` UserDaoImpl ``` package com.dodoke.dao.impl; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import com.dodoke.bean.User; import com.dodoke.dao.inter.UserDao; import com.dodoke.util.DBUtil; public class UserDaoImpl implements UserDao { private static UserDaoImpl daoImpl = new UserDaoImpl(); private UserDaoImpl() { } public static UserDaoImpl getInstance() { return daoImpl; } @Override public List<User> list(int start, int count) { List<User> beans = new ArrayList<User>(); String sql = "select * from t_user order by id desc limit ?,? "; try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) { ps.setInt(1, start); ps.setInt(2, count); ResultSet rs = ps.executeQuery(); while (rs.next()) { User bean = new User(); int id = rs.getInt("id"); String name = rs.getString("name"); String password = rs.getString("password"); bean.setName(name); bean.setPassword(password); bean.setId(id); beans.add(bean); } } catch (SQLException e) { e.printStackTrace(); } return beans; } @Override public int getTotal() { int total = 0; String sql = "select count(*) from t_user"; try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) { ResultSet rs = ps.executeQuery(); while (rs.next()) { total = rs.getInt(1); } } catch (SQLException e) { e.printStackTrace(); } return total; } @Override public User get(int id) { User bean = null; String sql = "select * from t_user where id = ?"; try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) { ps.setInt(1, id); ResultSet rs = ps.executeQuery(); if (rs.next()) { bean = new User(); String name = rs.getString("name"); String password = rs.getString("password"); bean.setName(name); bean.setPassword(password); bean.setId(id); } } catch (SQLException e) { e.printStackTrace(); } return bean; } } ``` # 步骤6:UserServlet ``` package com.dodoke.controller; import java.util.List; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.dodoke.bean.User; import com.dodoke.util.Page; /** * Servlet implementation class UserServlet */ @WebServlet("/UserServlet") public class UserServlet extends BaseBackServlet { private static final long serialVersionUID = 1L; @Override public String add(HttpServletRequest request, HttpServletResponse response) { // TODO Auto-generated method stub return null; } @Override public String delete(HttpServletRequest request, HttpServletResponse response) { // TODO Auto-generated method stub return null; } @Override public String edit(HttpServletRequest request, HttpServletResponse response) { // TODO Auto-generated method stub return null; } @Override public String update(HttpServletRequest request, HttpServletResponse response) { // TODO Auto-generated method stub return null; } @Override public String list(HttpServletRequest request, HttpServletResponse response, Page page) { List<User> us = userDao.list(page.getStart(), page.getCount()); int total = userDao.getTotal(); page.setTotal(total); request.setAttribute("us", us); request.setAttribute("page", page); return "admin/listUser.jsp"; } } ``` # 步骤7:listUser.jsp ``` <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.util.*"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@include file="../include/admin/adminHeader.jsp"%> <%@include file="../include/admin/adminNavigator.jsp"%> <script> </script> <title>用户管理</title> <div class="workingArea"> <h1 class="label label-info">用户管理</h1> <br> <br> <div class="listDataTableDiv"> <table class="table table-striped table-bordered table-hover table-condensed"> <thead> <tr class="success"> <th>ID</th> <th>用户名称</th> </tr> </thead> <tbody> <c:forEach items="${us}" var="u"> <tr> <td>${u.id}</td> <td>${u.name}</td> </tr> </c:forEach> </tbody> </table> </div> <div class="pageDiv"> <%@include file="../include/admin/adminPage.jsp"%> </div> </div> <%@include file="../include/admin/adminFooter.jsp"%> ``` # 增加,删除,修改功能 增加交由前台用户注册功能 删除不提供(用户信息是最重要的资料) 修改不提供,应该由前台用户自己完成