• JavaWeb:用户信息管理系统总结


    当当当当~~~~欢迎大家阅读,今天我们总结用户信息管理系统的流程

    该系统主要分为两部分,一部分是简单功能(增删改查)的基础实现;

    一部分是复杂功能删除选中,分页查询,复杂条件查询的实现;

    简单功能

    首先我们来看一下登录界面

    一:登录

    (一):登录界面

    1:html登录界面

    2:html代码 

    1. <body>
    2. <div class="container" style="width: 400px;">
    3. <h3 style="text-align: center;">管理员登录</h3>
    4. <form action="login" method="post">
    5. <div class="form-group">
    6. <label for="user">用户名:</label>
    7. <input type="text" name="user" class="form-control" id="user" placeholder="请输入用户名"/>
    8. </div>
    9. <div class="form-group">
    10. <label for="password">密码:</label>
    11. <input type="password" name="password" class="form-control" id="password" placeholder="请输入密码"/>
    12. </div>
    13. <div class="form-inline">
    14. <label for="vcode">验证码:</label>
    15. <input type="text" name="verifycode" class="form-control" id="verifycode" placeholder="请输入验证码" style="width: 120px;"/>
    16. <a href="javascript:refreshCode()"><img src="vcode" title="看不清点击刷新" id="vcode"/></a>
    17. </div>
    18. <hr/>
    19. <div class="form-group" style="text-align: center;">
    20. <input class="btn btn btn-primary" type="submit" value="登录">
    21. </div>
    22. </form>
    23. <!-- 出错显示的信息框 -->
    24. <div class="alert alert-warning alert-dismissible" role="alert">
    25. <button type="button" class="close" data-dismiss="alert" >
    26. <span>&times;</span>
    27. </button>
    28. <strong>登录失败!</strong>
    29. </div>
    30. </div>
    31. </body>

    3:改进后的jsp登录界面

    4:页面代码的改进

    在html基础上添加了servlet与后端交互,并且在登录时插入了验证码的校验

    1. <form action="${pageContext.request.contextPath}/loginServlet" method="post">
    2. <div class="form-inline">
    3. <label for="vcode">验证码:</label>
    4. <input type="text" name="verifycode" class="form-control" id="verifycode" placeholder="请输入验证码" style="width: 120px;"/>
    5. <a href="javascript:refreshCode();">
    6. <img src="${pageContext.request.contextPath}/checkCodeServlet" title="看不清点击刷新" id="vcode"/>
    7. </a>
    8. </div>

     (二):后端代码实现

    1:LoginServlet

    1. //1.设置编码
    2. request.setCharacterEncoding("utf-8");
    3. //2.获取数据
    4. //2.1获取用户填写验证码
    5. String verifycode = request.getParameter("verifycode");
    6. //3.验证码校验
    7. HttpSession session = request.getSession();
    8. String checkcode_server = (String) session.getAttribute("CHECKCODE_SERVER");
    9. session.removeAttribute("CHECKCODE_SERVER");//确保验证码一次性
    10. if(!checkcode_server.equalsIgnoreCase(verifycode)){
    11. //验证码不正确
    12. //提示信息
    13. request.setAttribute("login_msg","验证码错误!");
    14. //跳转登录页面
    15. request.getRequestDispatcher("/login.jsp").forward(request,response);
    16. return;
    17. }
    18. Map<String, String[]> map = request.getParameterMap();
    19. //4.封装User对象
    20. User user = new User();
    21. try {
    22. BeanUtils.populate(user,map);
    23. } catch (IllegalAccessException e) {
    24. e.printStackTrace();
    25. } catch (InvocationTargetException e) {
    26. e.printStackTrace();
    27. }
    28. //5.调用Service查询
    29. UserService service = new UserServiceImpl();
    30. User loginUser = service.login(user);
    31. //6.判断是否登录成功
    32. if(loginUser != null){
    33. //登录成功
    34. //将用户存入session
    35. session.setAttribute("user",loginUser);
    36. //跳转页面
    37. response.sendRedirect(request.getContextPath()+"/index.jsp");
    38. }else{
    39. //登录失败
    40. //提示信息
    41. request.setAttribute("login_msg","用户名或密码错误!");
    42. //跳转登录页面
    43. request.getRequestDispatcher("/login.jsp").forward(request,response);
    44. }

    2:UserServiceImpl(login)

    1. @Override
    2. public User login(User user) {
    3. return dao.findUserByUsernameAndPassword(user.getUsername(),user.getPassword());
    4. }

    3:UserDaoImpl(findUserByUsernameAndPassword)

    1. @Override
    2. public User findUserByUsernameAndPassword(String username, String password) {
    3. try {
    4. String sql = "select * from user where username = ? and password = ?";
    5. User user = template.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), username, password);
    6. return user;
    7. } catch (Exception e) {
    8. e.printStackTrace();
    9. return null;
    10. }
    11. }

    登录界面完成并且登录成功后,接下来我们开始进行用户信息的列表查询!

    二:查询

    (一):查询界面

    1:点击查询所有用户信息进入用户信息列表界面

    1. <div align="center">
    2. <a
    3. href="${pageContext.request.contextPath}/findUserByPageServlet" style="text-decoration:none;font-size:33px">查询所有用户信息
    4. </a>
    5. </div>

     

    2:查询界面的jsp代码

    1. <table border="1" class="table table-bordered table-hover">
    2. <tr class="success">
    3. <th><input type="checkbox" id="firstCb"></th>
    4. <th>编号</th>
    5. <th>姓名</th>
    6. <th>性别</th>
    7. <th>年龄</th>
    8. <th>籍贯</th>
    9. <th>QQ</th>
    10. <th>邮箱</th>
    11. <th>操作</th>
    12. </tr>
    13. <c:forEach items="${pb.list}" var="user" varStatus="s">
    14. <tr>
    15. <td><input type="checkbox" name="uid" value="${user.id}"></td>
    16. <td>${s.count}</td>
    17. <td>${user.name}</td>
    18. <td>${user.gender}</td>
    19. <td>${user.age}</td>
    20. <td>${user.address}</td>
    21. <td>${user.qq}</td>
    22. <td>${user.email}</td>
    23. <td><a class="btn btn-default btn-sm"
    24. </c:forEach>
    25. </table>

     (二):后端代码实现

    1:UserListServlet

    1. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    2. //1.调用UserService完成查询
    3. UserService service = new UserServiceImpl();
    4. List<User> users = service.findAll();
    5. //2.将list存入request域
    6. request.setAttribute("users",users);
    7. //3.转发到list.jsp
    8. request.getRequestDispatcher("/list.jsp").forward(request,response);
    9. }

    2:UserServiceImpl(findAll) 

    1. @Override
    2. public List<User> findAll() {
    3. //调用Dao完成查询
    4. return dao.findAll();
    5. }

    3:UserDaoImpl(findAll) 

    1. @Override
    2. public List<User> findAll() {
    3. //使用JDBC操作数据库...
    4. //1.定义sql
    5. String sql = "select * from user";
    6. List<User> users = template.query(sql, new BeanPropertyRowMapper<User>(User.class));
    7. return users;
    8. }

     三:添加用户信息

    (一):添加界面 

    1:在list页面上补充一个添加联系人的按钮

    1. <div style="float: right;margin: 5px;">
    2. <a class="btn btn-primary" href="${pageContext.request.contextPath}/add.jsp">添加联系人</a>
    3. </div>

    2:点击按钮进入添加联系人界面

    3:Add 界面的jsp代码

    1. <body>
    2. <div class="container">
    3. <center><h3>添加联系人页面</h3></center>
    4. <form action="${pageContext.request.contextPath}/addUserServlet" method="post">
    5. <div class="form-group">
    6. <label for="name">姓名:</label>
    7. <input type="text" class="form-control" id="name" name="name" placeholder="请输入姓名">
    8. </div>
    9. <div class="form-group">
    10. <label>性别:</label>
    11. <input type="radio" name="gender" value="男" checked="checked"/>
    12. <input type="radio" name="gender" value="女"/>
    13. </div>
    14. <div class="form-group">
    15. <label for="age">年龄:</label>
    16. <input type="text" class="form-control" id="age" name="age" placeholder="请输入年龄">
    17. </div>
    18. <div class="form-group">
    19. <label for="address">籍贯:</label>
    20. <select name="address" class="form-control" id="address">
    21. <option value="陕西">陕西</option>
    22. <option value="北京">北京</option>
    23. <option value="上海">上海</option>
    24. </select>
    25. </div>
    26. <div class="form-group">
    27. <label for="qq">QQ:</label>
    28. <input type="text" class="form-control" id="qq" name="qq" placeholder="请输入QQ号码"/>
    29. </div>
    30. <div class="form-group">
    31. <label for="email">Email:</label>
    32. <input type="text" class="form-control" id="email" name="email" placeholder="请输入邮箱地址"/>
    33. </div>
    34. <div class="form-group" style="text-align: center">
    35. <input class="btn btn-primary" type="submit" value="提交" />
    36. <input class="btn btn-default" type="reset" value="重置" />
    37. <input class="btn btn-default" type="button" value="返回" />
    38. </div>
    39. </form>
    40. </div>
    41. </body>

    (二):后端代码实现

    1:添加功能的思路分析 

    2:AddUserServlet

    1. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    2. //1.设置编码
    3. request.setCharacterEncoding("utf-8");
    4. //2.获取参数
    5. Map<String, String[]> map = request.getParameterMap();
    6. //3.封装对象
    7. User user = new User();
    8. try {
    9. BeanUtils.populate(user,map);
    10. } catch (IllegalAccessException e) {
    11. e.printStackTrace();
    12. } catch (InvocationTargetException e) {
    13. e.printStackTrace();
    14. }
    15. //4.调用Service保存
    16. UserService service = new UserServiceImpl();
    17. service.addUser(user);
    18. //5.跳转到userListServlet
    19. response.sendRedirect(request.getContextPath()+"/findUserByPageServlet");
    20. }

    3:UserServiceImpl(addUser)

    1. @Override
    2. public void addUser(User user) {
    3. dao.add(user);
    4. }

    4:UserDaoImpl(add)

    1. @Override
    2. public void add(User user) {
    3. //1.定义sql
    4. String sql = "insert into user values(null,?,?,?,?,?,?,null,null)";
    5. //2.执行sql
    6. template.update(sql, user.getName(), user.getGender(), user.getAge(), user.getAddress(), user.getQq(), user.getEmail());
    7. }

    四:删除单个用户信息

    (一):添加删除按钮 

     <a class="btn btn-default btn-sm" href="javascript:deleteUser(${user.id});">删除</a></td>

    (二):后端代码实现

    1:删除功能的思路分析

    2:DelUserServlet(先获取id,通过id,删除用户信息)

    1. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    2. //1.获取id
    3. String id = request.getParameter("id");
    4. //2.调用service删除
    5. UserService service = new UserServiceImpl();
    6. service.deleteUser(id);
    7. //3.跳转到查询所有Servlet
    8. response.sendRedirect(request.getContextPath()+"/findUserByPageServlet");
    9. }

    3:UserServiceImpl(deleteUser)

    1. @Override
    2. public void deleteUser(String id) {
    3. dao.delete(Integer.parseInt(id));
    4. }

    4:UserDaoImpl(delete) 

    1. @Override
    2. public void delete(int id) {
    3. //1.定义sql
    4. String sql = "delete from user where id = ?";
    5. //2.执行sql
    6. template.update(sql, id);
    7. }

     五:修改用户信息

    (一):修改用户信息界面

     1:html界面

    2:html代码

    1. <body>
    2. <div class="container" style="width: 400px;">
    3. <h3 style="text-align: center;">修改联系人</h3>
    4. <form action="" method="post">
    5. <div class="form-group">
    6. <label for="name">姓名:</label>
    7. <input type="text" class="form-control" id="name" name="name" readonly="readonly" placeholder="请输入姓名" />
    8. </div>
    9. <div class="form-group">
    10. <label>性别:</label>
    11. <input type="radio" name="sex" value="男" />
    12. <input type="radio" name="sex" value="女" />
    13. </div>
    14. <div class="form-group">
    15. <label for="age">年龄:</label>
    16. <input type="text" class="form-control" id="age" name="age" placeholder="请输入年龄" />
    17. </div>
    18. <div class="form-group">
    19. <label for="address">籍贯:</label>
    20. <select name="address" id="address" class="form-control" >
    21. <option value="陕西">陕西</option>
    22. <option value="北京">北京</option>
    23. <option value="上海">上海</option>
    24. </select>
    25. </div>
    26. <div class="form-group">
    27. <label for="qq">QQ:</label>
    28. <input type="text" id="qq" class="form-control" name="qq" placeholder="请输入QQ号码"/>
    29. </div>
    30. <div class="form-group">
    31. <label for="email">Email:</label>
    32. <input type="text" id="email" class="form-control" name="email" placeholder="请输入邮箱地址"/>
    33. </div>
    34. <div class="form-group" style="text-align: center">
    35. <input class="btn btn-primary" type="submit" value="提交" />
    36. <input class="btn btn-default" type="reset" value="重置" />
    37. <input class="btn btn-default" type="button" value="返回"/>
    38. </div>
    39. </form>
    40. </div>
    41. </body>

    3:改进后的jsp修改界面

     4:页面改进后的代码

    添加可以自由选择性别和籍贯的判断

    1. <body>
    2. <div class="container" style="width: 400px;">
    3. <h3 style="text-align: center;">修改联系人</h3>
    4. <form action="${pageContext.request.contextPath}/updateUserServlet" method="post">
    5. <!-- 隐藏域提交id-->
    6. <input type="hidden" name="id" value="${user.id}">
    7. <div class="form-group">
    8. <label for="name">姓名:</label>
    9. <input type="text" class="form-control" id="name" name="name" value="${user.name}" readonly="readonly" placeholder="请输入姓名" />
    10. </div>
    11. <div class="form-group">
    12. <label>性别:</label>
    13. <c:if test="${user.gender == '男'}">
    14. <input type="radio" name="gender" value="男" checked />
    15. <input type="radio" name="gender" value="女" />
    16. </c:if>
    17. <c:if test="${user.gender == '女'}">
    18. <input type="radio" name="gender" value="男" />
    19. <input type="radio" name="gender" value="女" checked />
    20. </c:if>
    21. </div>
    22. <div class="form-group">
    23. <label for="age">年龄:</label>
    24. <input type="text" class="form-control" value="${user.age}" id="age" name="age" placeholder="请输入年龄" />
    25. </div>
    26. <div class="form-group">
    27. <label for="address">籍贯:</label>
    28. <select name="address" id="address" class="form-control" >
    29. <c:if test="${user.address == '陕西'}">
    30. <option value="陕西" selected>陕西</option>
    31. <option value="北京">北京</option>
    32. <option value="上海">上海</option>
    33. </c:if>
    34. <c:if test="${user.address == '北京'}">
    35. <option value="陕西" >陕西</option>
    36. <option value="北京" selected>北京</option>
    37. <option value="上海">上海</option>
    38. </c:if>
    39. <c:if test="${user.address == '上海'}">
    40. <option value="陕西" >陕西</option>
    41. <option value="北京">北京</option>
    42. <option value="上海" selected>上海</option>
    43. </c:if>
    44. </select>
    45. </div>
    46. </form>
    47. </div>
    48. </body>

    (二):后端代码实现

    1:修改功能的思路分析

    2:FindUserServlet

    1. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    2. //1.获取id
    3. String id = request.getParameter("id");
    4. //2.调用Service查询
    5. UserService service = new UserServiceImpl();
    6. User user = service.findUserById(id);
    7. //3.将user存入request
    8. request.setAttribute("user",user);
    9. //4.转发到update.jsp
    10. request.getRequestDispatcher("/update.jsp").forward(request,response);
    11. }

    3:UpdateUserServlet

    1. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    2. //1.设置编码
    3. request.setCharacterEncoding("utf-8");
    4. //2.获取map
    5. Map<String, String[]> map = request.getParameterMap();
    6. //3.封装对象
    7. User user = new User();
    8. try {
    9. BeanUtils.populate(user,map);
    10. } catch (IllegalAccessException e) {
    11. e.printStackTrace();
    12. } catch (InvocationTargetException e) {
    13. e.printStackTrace();
    14. }
    15. //4.调用Service修改
    16. UserService service = new UserServiceImpl();
    17. service.updateUser(user);
    18. //5.跳转到查询所有Servlet
    19. response.sendRedirect(request.getContextPath()+"/findUserByPageServlet");
    20. }

    4:UserServiceImpl(updateUser)

    1. @Override
    2. public void updateUser(User user) {
    3. dao.update(user);
    4. }

    5:UserDaoImpl(update)

    1. @Override
    2. public void update(User user) {
    3. String sql = "update user set name = ?,gender = ? ,age = ? , address = ? , qq = ?, email = ? where id = ?";
    4. template.update(sql, user.getName(), user.getGender(), user.getAge(), user.getAddress(), user.getQq(), user.getEmail(), user.getId());
    5. }

    复杂功能

    一:删除选中

     (一):删除选中界面

    1:在list页面添加删除选中的按钮,并且给删除选中添加单击事件

    1. <div style="float: right;margin: 5px;">
    2. <a class="btn btn-primary" href="javascript:void(0);" id="delSelected">删除选中
    3. </a>
    4. </div>
    5. window.onload = function(){
    6. //给删除选中按钮添加单击事件
    7. document.getElementById("delSelected").onclick = function(){
    8. if(confirm("您确定要删除选中条目吗?")){
    9. var flag = false;
    10. //判断是否有选中条目
    11. var cbs = document.getElementsByName("uid");
    12. for (var i = 0; i < cbs.length; i++) {
    13. if(cbs[i].checked){
    14. //有一个条目选中了
    15. flag = true;
    16. break;
    17. }
    18. }
    19. if(flag){//有条目被选中
    20. //表单提交
    21. document.getElementById("form").submit();
    22. }
    23. }
    24. }
    25. }

    2:删除选中时可以添加全部选中

    1. //1.获取第一个cb
    2. document.getElementById("firstCb").onclick = function(){
    3. //2.获取下边列表中所有的cb
    4. var cbs = document.getElementsByName("uid");
    5. //3.遍历
    6. for (var i = 0; i < cbs.length; i++) {
    7. //4.设置这些cbs[i]的checked状态 = firstCb.checked
    8. cbs[i].checked = this.checked;
    9. }
    10. }

    (二):后端代码实现

    1:删除选中的思路分析

    2:DelSelectedServlet

    1. public class DelSelectedServlet extends HttpServlet {
    2. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    3. //1.获取所有id
    4. String[] ids = request.getParameterValues("uid");
    5. //2.调用service删除
    6. UserService service = new UserServiceImpl();
    7. service.delSelectedUser(ids);
    8. //3.跳转查询所有Servlet
    9. response.sendRedirect(request.getContextPath()+"/findUserByPageServlet");
    10. }

    3:UserService(delSelectedUser)

    1. //删除选中用户
    2. @Override
    3. public void delSelectedUser(String[] ids) {
    4. if(ids != null && ids.length > 0){
    5. //1.遍历数组
    6. for (String id : ids) {
    7. //2.调用dao删除
    8. dao.delete(Integer.parseInt(id));
    9. }
    10. }
    11. }

    二:分页查询

    (一):分页查询页面 

    1:在list.jsp中添加组件分页栏

    1. <div>
    2. <nav aria-label="Page navigation">
    3. <ul class="pagination">
    4. <c:if test="${pb.currentPage == 1}">
    5. <li class="disabled">
    6. </c:if>
    7. <c:if test="${pb.currentPage != 1}">
    8. <li>
    9. </c:if>
    10. <a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${pb.currentPage - 1}&rows=5&name=${condition.name[0]}&address=${condition.address[0]}&email=${condition.email[0]}" aria-label="Previous">
    11. <span aria-hidden="true">&laquo;</span>
    12. </a>
    13. </li>
    14. <c:forEach begin="1" end="${pb.totalPage}" var="i" >
    15. <c:if test="${pb.currentPage == i}">
    16. <li class="active"><a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${i}&rows=5&name=${condition.name[0]}&address=${condition.address[0]}&email=${condition.email[0]}">${i}</a></li>
    17. </c:if>
    18. <c:if test="${pb.currentPage != i}">
    19. <li><a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${i}&rows=5&name=${condition.name[0]}&address=${condition.address[0]}&email=${condition.email[0]}">${i}</a></li>
    20. </c:if>
    21. </c:forEach>
    22. <li>
    23. <a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${pb.currentPage + 1}&rows=5&name=${condition.name[0]}&address=${condition.address[0]}&email=${condition.email[0]}" aria-label="Next">
    24. <span aria-hidden="true">&raquo;</span>
    25. </a>
    26. </li>
    27. <span style="font-size: 25px;margin-left: 5px;">
    28. ${pb.totalCount}条记录,共${pb.totalPage}
    29. </span>
    30. </ul>
    31. </nav>
    32. </div>

    (二):后端代码实现

    1:分页查询实体类的思路分析

    1:添加一个分页查询相关的类

    1. public class PageBean<T> {
    2. private int totalCount; // 总记录数
    3. private int totalPage ; // 总页码
    4. private List<T> list ; // 每页的数据
    5. private int currentPage ; //当前页码
    6. private int rows;//每页显示的记录数

    2:分页查询的逻辑思路分析

    3:FindUserByPageServlet

    1. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    2. request.setCharacterEncoding("utf-8");
    3. //1.获取参数
    4. String currentPage = request.getParameter("currentPage");//当前页码
    5. String rows = request.getParameter("rows");//每页显示条数
    6. if(currentPage == null || "".equals(currentPage)){
    7. currentPage = "1";
    8. }
    9. if(rows == null || "".equals(rows)){
    10. rows = "5";
    11. }
    12. //获取条件查询参数
    13. Map<String, String[]> condition = request.getParameterMap();
    14. //2.调用service查询
    15. UserService service = new UserServiceImpl();
    16. PageBean<User> pb = service.findUserByPage(currentPage,rows,condition);
    17. System.out.println(pb);
    18. //3.将PageBean存入request
    19. request.setAttribute("pb",pb);
    20. request.setAttribute("condition",condition);//将查询条件存入request
    21. //4.转发到list.jsp
    22. request.getRequestDispatcher("/list.jsp").forward(request,response);
    23. }

    4:UserService(findUserByPage)

    1. //分页条件查询
    2. @Override
    3. public PageBean<User> findUserByPage(String _currentPage, String _rows, Map<String, String[]> condition) {
    4. int currentPage = Integer.parseInt(_currentPage);
    5. int rows = Integer.parseInt(_rows);
    6. //第1页的上一页永远返回1
    7. if(currentPage <=0) {
    8. currentPage = 1;
    9. }
    10. //1.创建空的PageBean对象
    11. PageBean<User> pb = new PageBean<User>();
    12. //2.设置参数
    13. pb.setCurrentPage(currentPage);
    14. pb.setRows(rows);
    15. //3.调用dao查询总记录数
    16. int totalCount = dao.findTotalCount(condition);
    17. pb.setTotalCount(totalCount);
    18. //4.调用dao查询List集合
    19. //计算开始的记录索引
    20. int start = (currentPage - 1) * rows;
    21. List<User> list = dao.findByPage(start,rows,condition);
    22. pb.setList(list);
    23. //5.计算总页码
    24. int totalPage = (totalCount % rows) == 0 ? totalCount/rows : (totalCount/rows) + 1;
    25. pb.setTotalPage(totalPage);
    26. return pb;
    27. }

    5:UserDaoImpl(findTotalCount)

    1. //查询总记录数
    2. @Override
    3. public int findTotalCount() {
    4. //1.定义模板初始化sql
    5. String sql = "select count(*) from user";
    6. return template.queryForObject(sql,Integer.class);
    7. }

    6:UserDaoImpl(findByPage) 

    1. //分页条件查询
    2. @Override
    3. public List<User> findByPage(int start, int rows) {
    4. String sql = "select * from user limit?,? ;
    5. return template.query(sql,new BeanPropertyRowMapper<User>,start,rows);
    6. }

    三:复杂条件查询

    (一):复杂条件查询界面

    1:复杂条件查询的界面展示 

     2:复杂条件查询的jsp代码

    1. <div style="float: left;">
    2. <form class="form-inline" action="${pageContext.request.contextPath}/findUserByPageServlet" method="post">
    3. <div class="form-group">
    4. <label for="exampleInputName2">姓名</label>
    5. <input type="text" name="name" value="${condition.name[0]}" class="form-control" id="exampleInputName2" >
    6. </div>
    7. <div class="form-group">
    8. <label for="exampleInputName3">籍贯</label>
    9. <input type="text" name="address" value="${condition.address[0]}" class="form-control" id="exampleInputName3" >
    10. </div>
    11. <div class="form-group">
    12. <label for="exampleInputEmail2">邮箱</label>
    13. <input type="text" name="email" value="${condition.email[0]}" class="form-control" id="exampleInputEmail2" >
    14. </div>
    15. <button type="submit" class="btn btn-default">查询</button>
    16. </form>
    17. </div>

    (二):后端代码实现

    1:复杂条件查询的思路分析

    2:在分页查询的FindUserByPageServlet基础上查询复杂条件的参数集合并传参

    1. //获取条件查询参数
    2. Map<String, String[]> condition = request.getParameterMap();
    3. //2.调用service查询
    4. UserService service = new UserServiceImpl();
    5. PageBean<User> pb = service.findUserByPage(currentPage,rows,condition);
    6. System.out.println(pb);

    3:在分页查询的UserService(findUserByPage) 的基础上传参

    1. //3.调用dao查询总记录数
    2. int totalCount = dao.findTotalCount(condition);
    3. pb.setTotalCount(totalCount);
    4. //4.调用dao查询List集合
    5. //计算开始的记录索引
    6. int start = (currentPage - 1) * rows;
    7. List<User> list = dao.findByPage(start,rows,condition);
    8. pb.setList(list);

    4:在分页查询的UserDaoImpl(findTotalCount)基础上判断复杂条件查询是否有值并且获取值

    1. //查询总记录数
    2. @Override
    3. public int findTotalCount(Map<String, String[]> condition) {
    4. //1.定义模板初始化sql
    5. String sql = "select count(*) from user where 1 = 1 ";
    6. StringBuilder sb = new StringBuilder(sql);
    7. //2.遍历map
    8. Set<String> keySet = condition.keySet();
    9. //定义参数的集合
    10. List<Object> params = new ArrayList<Object>();
    11. for (String key : keySet) {
    12. //排除分页条件参数
    13. if("currentPage".equals(key) || "rows".equals(key)){
    14. continue;
    15. }
    16. //获取value
    17. String value = condition.get(key)[0];
    18. //判断value是否有值
    19. if(value != null && !"".equals(value)){
    20. //有值
    21. sb.append(" and "+key+" like ? ");
    22. params.add("%"+value+"%");//?条件的值
    23. }
    24. }
    25. System.out.println(sb.toString());
    26. System.out.println(params);
    27. return template.queryForObject(sb.toString(),Integer.class,params.toArray());
    28. }

    5:在分页查询的UserDaoImpl(findByPage) 基础上遍历复杂条件查询的集合并为查询到的值做分页处理

    1. //分页条件查询
    2. @Override
    3. public List<User> findByPage(int start, int rows, Map<String, String[]> condition) {
    4. String sql = "select * from user where 1 = 1 ";
    5. StringBuilder sb = new StringBuilder(sql);
    6. //2.遍历map
    7. Set<String> keySet = condition.keySet();
    8. //定义参数的集合
    9. List<Object> params = new ArrayList<Object>();
    10. for (String key : keySet) {
    11. //排除分页条件参数
    12. if("currentPage".equals(key) || "rows".equals(key)){
    13. continue;
    14. }
    15. //获取value
    16. String value = condition.get(key)[0];
    17. //判断value是否有值
    18. if(value != null && !"".equals(value)){
    19. //有值
    20. sb.append(" and "+key+" like ? ");
    21. params.add("%"+value+"%");//?条件的值//模糊查询
    22. }
    23. }
    24. //添加分页查询
    25. sb.append(" limit ?,? ");
    26. //添加分页查询参数值
    27. params.add(start);
    28. params.add(rows);
    29. sql = sb.toString();
    30. System.out.println(sql);
    31. System.out.println(params);
    32. return template.query(sql,new BeanPropertyRowMapper<User>(User.class),params.toArray());
    33. }

    以上就是用户信息管理系统的内容啦,希望我的文章对你有所帮助,如果有错误的地方还望大家批评指正,谢谢大家阅读!  

  • 相关阅读:
    HTTP请求参数的区别-- Body、Query、Params的区别
    gittee启动器
    第1章 C语言高级的枚举、typedef、位域(三)
    [100天算法】-最短无序连续子数组(day 66)
    linux内核管理
    【uiautomation】获取微信好友名单,可指定标签 & 全部
    AppWeb认证绕过漏洞(CVE-2018-8715)
    基于or-tools的护士排班问题建模求解
    CodeForces每日好题10.14
    Linux:安装minio并设置开机自启
  • 原文地址:https://blog.csdn.net/Z2658172512/article/details/124960165