登录和分页显示所有查询到的信息
本篇继之前的内容: 篇一
本篇只写本次登录和分页显示用到的方法,后面会在这个基础上补充的
实体类TbUser类()
bean包里的类
属性和数据库里的相对应
写好之后基本不变
package com.hcq.bean;
public class TbUser {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "TbUser{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
接口IUserDao
dao包里的接口
package com.hcq.dao;
import com.hcq.bean.TbUser;
import com.hcq.page.Page;
import java.util.List;
public interface IUserDao {
List<TbUser> findAll();
List<TbUser> list(Page page);
int total();
}
接口IUserService
service包里的接口
package com.hcq.service;
import com.hcq.bean.TbUser;
import com.hcq.page.Page;
import java.util.List;
public interface IUserService {
List<TbUser> findAll();
List<TbUser> list(Page page);
int total();
}
UserServiceImpl类
在service包下的impl包里
实现IUserService接口
package com.hcq.service.impl;
import com.hcq.bean.TbUser;
import com.hcq.dao.IUserDao;
import com.hcq.page.Page;
import com.hcq.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;
@Service
public class UserServiceImpl implements IUserService {
@Autowired
private IUserDao userDao;
@Override
public List<TbUser> findAll() {
return userDao.findAll();
}
@Override
public List<TbUser> list(Page page) {
return userDao.list(page);
}
@Override
public int total() {
return userDao.total();
}
}
Page类
page包里的类,里面是关于分页的一些信息和方法。
package com.hcq.page;
public class Page {
int start = 0;//开始位置
int count = 3;//每页的个数,默认设置为3
int last = 0;//最后一页的位置
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
public int getLast() {
return last;
}
public void setLast(int last) {
this.last = last;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
//通过总数total和每页的个数计算出最后一页的位置
public void calculateLast(int total) {
if (0 == total % count)
last = total - count;
else
last = total - total % count;
if (0 == last)
last = 1;
}
}
TbUserMapper.xml映射文件
根据id和dao类的方法名映射,实现具体的后台数据库的操作
<?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.hcq.dao.IUserDao" >
<!--查询所有-->
<select id="findAll" resultType="com.hcq.bean.TbUser">
select * from tb_user
</select>
<select id="list" resultType="com.hcq.bean.TbUser">
select * from tb_user
<if test="start!=null and count!=null">
limit #{start},#{count}
</if>
</select>
<select id="total" resultType="int">
select count(*) from tb_user
</select>
</mapper>
登录页面index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>登录</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- 引入 Bootstrap -->
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
基于SSM框架的管理系统:简单实现增、删、改、查。
</h1>
</div>
</div>
</div>
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
<small>用户登录</small>
</h1>
</div>
</div>
</div>
<form action="${pageContext.request.contextPath}/user/toList.do"
method="post">
用户姓名:<input type="text" name="username"><br><br><br>
用户密码:<input type="text" name="password"><br><br><br>
<div class="row">
<div class="col-md-4 column">
<input type="submit" class="btn btn-primary" value="登录" >
</div>
</div>
</form>
</div>
</body>
</html>
UserController类
通过@RequestMapping("*******")的映射实现页面跳转的方法
package com.hcq.controller;
import com.hcq.bean.TbUser;
import com.hcq.page.Page;
import com.hcq.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.persistence.Id;
import javax.servlet.http.HttpServletRequest;
import javax.swing.*;
import java.util.List;
@RequestMapping("user")
@Controller
public class UserController {
@Autowired
IUserService userService;
@RequestMapping("findAll.do")
public ModelAndView findAll(){
List<TbUser> users = userService.findAll();
ModelAndView mv = new ModelAndView();
mv.addObject("us",users);
mv.setViewName("allUser");
return mv;
}
@RequestMapping("list.do")
public ModelAndView list(Page page){
ModelAndView mv = new ModelAndView();
List<TbUser> users = userService.list(page);
int total = userService.total();
page.calculateLast(total);
mv.addObject("cs",users);
mv.setViewName("allUser");
return mv;
}
@RequestMapping("toList.do")
public String toList(HttpServletRequest request){
List<TbUser> list = userService.findAll();
for (TbUser user : list){
if (user.getUsername().equals(request.getParameter("username"))&&user.getPassword().equals(request.getParameter("password"))){
return "redirect:list.do";
}
}
JOptionPane.showMessageDialog(null, "用户名或密码错误!请重新登录", "认证失败!", JOptionPane.ERROR_MESSAGE);
return "../index";
}
}
显示所有页面allUser.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>user列表</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- 引入 Bootstrap -->
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
基于SSM框架的管理系统:简单实现增、删、改、查。
</h1>
</div>
</div>
</div>
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
<small>用户列表 —— 显示所有用户</small>
</h1>
</div>
</div>
</div>
<div class="row">
<div class="col-md-4 column">
<a class="btn btn-primary" href="${pageContext.request.contextPath}/jsp/addUser.jsp">新增</a>
</div>
</div>
<div class="row clearfix">
<div class="col-md-12 column">
<table class="table table-hover table-striped">
<thead>
<tr>
<th>id</th>
<th>用户名</th>
<th>密码</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach items="${cs}" var="c">
<tr>
<td>${c.id}</td>
<td>${c.username}</td>
<td>${c.password}</td>
<td>
<a href="${pageContext.request.contextPath}/user/toUpdate.do?id=${c.id}">更改</a> |
<a href="${pageContext.request.contextPath}/user/delete.do?id=${c.id}">删除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<div style="text-align: center">
<a href="?start=0">首 页</a>
<a href="?start=${page.start-page.count}">上一页</a>
<a href="?start=${page.start+page.count}">下一页</a>
<a href="?start=${page.last}">末 页</a>
</div>
</div>
</div>
</div>
</body>
</html>