#####这是基于上一篇登录模块之上的通过id查询用户
开发工具:
IDE: IDEA2020
数据库:Mysql5.7
JDK1.8
项目结构:
由于是基于之前的登录模块,我就贴有改动的部分了。
UserMapper
package cn.fvti.Study.Dao;
import cn.fvti.Study.Bean.User;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface UserMapper {
// 查询所有
List<User>findAllUser();
// 通过ID查询用户
List<User> findUserByID(Integer id);
// 通过id删除用户
void deleteUserById(Integer id);
// 注册用户
int insertUser(User user);
// 登录用户
User findLogin(String name,String password);
//查询用户的所有记录数
int selectTotalCount();
}
UserService
package cn.fvti.Study.Service;
import cn.fvti.Study.Bean.User;
import java.util.List;
public interface UserService {
// 查询所有
List<User> findAllUser();
// 通过ID查询用户
List<User> findUserByID(Integer id);
// 注册用户
int insertUser(User user);
// 登录用户
boolean findLogin(String name,String password);
UserServiceImpl
package cn.fvti.Study.Service.Impl;
import cn.fvti.Study.Bean.User;
import cn.fvti.Study.Dao.UserMapper;
import cn.fvti.Study.Service.UserService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper mapper;
@Override
public List<User> findAllUser() {
return mapper.findAllUser();
}
@Override
public List<User> findUserByID(Integer id) {
return mapper.findUserByID(id);
}
@Override
public int insertUser(User user) {
return mapper.insertUser(user);
}
@Override
public boolean findLogin(String name,String password) {
System.out.println("输入的账号:" + name);
System.out.println("输入的密码:" + password);
User login = mapper.findLogin(name,password);
if (login != null) {
if (login.getName().equals(name) && login.getPassword().equals(password))
return true;
}
return false;
}
@Override
public void deleteUserById(Integer id) {
mapper.deleteUserById(id);
System.out.println("你删除的用户id是:"+id);
}
@Override
public int selectTotalCount() {
return mapper.selectTotalCount();
}
}
控制器
package cn.fvti.Study.Controller;
import cn.fvti.Study.Bean.User;
import cn.fvti.Study.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
@Controller
@RequestMapping("/user")
//@SessionAttributes 是用来在 controller 内部共享 model 属性的
public class UserController {
@Autowired
private UserService service;
/*
* 查询所有用户
* */
@RequestMapping("/login")
public ModelAndView login(User user, Model model, HttpServletRequest request, HttpSession session,HttpServletResponse response) {
List<User> allUser = service.findAllUser();
response.setCharacterEncoding("utf-8");
model.addAttribute("AllUser", allUser);
request.setAttribute("AllUser",allUser);
ModelAndView mv = new ModelAndView();
ModelAndView mv1 = new ModelAndView();
mv1.setViewName("welcome");
mv.setViewName("Error");
boolean login = service.findLogin(user.getName(),user.getPassword());
if (login) {
session.setAttribute("user", user);
return mv1;
} else {
mv.addObject("msg", "账号密码错误,请重新登录");
return mv;
}
}
// 根据id查询用户
@RequestMapping("/findUserById")
public String FindById(User user, Model model, HttpServletRequest request, Integer id){
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>");
String id2 = request.getParameter("id");
System.out.println(id2); //获取前端传过来的id
List<User> userByID = service.findUserByID(id);
request.setAttribute("userById",userByID);
model.addAttribute("userById", userByID); // 添加到会话里
return "welcome02";
}
welcome.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>欢迎</title>
<style>
h1{
text-align: center;
}
.search {
color: aqua;
width: 120px;
}
</style>
</head>
<body>
<h1>SSM简易系统</h1>
<form action="/user/findUserById" method="post">
搜索:<input type="search" name="id" class="search">
<input type="submit" value="提交" >
<br>
</form>
<form>
<div class="col-md-12 column">
<table bgcolor="red" align="center" border="2">
<tr>
<th>用户ID</th>
<th>用户性名</th>
<th>用户密码</th>
<th>用户性别</th>
<th>用户手机</th>
<th>用户地址</th>
<th>用户邮箱</th>
<th>用户PUID</th>
<th>用户操作</th>
</tr>
<c:forEach var="alluser" items="${AllUser}">
<tr>
<td>${alluser.id}</td>
<td>${alluser.name}</td>
<td>${alluser.password}</td>
<td>${alluser.sex}</td>
<td>${alluser.telephone}</td>
<td>${alluser.address}</td>
<td>${alluser.email}</td>
<td>${alluser.PUID}</td>
<td><a href="/user/add" input type="submit" value="新增" >新增</a>
<a href="/user/delete" input type="submit" value="删除">删除></a></td>
</tr>
</c:forEach>
</table>
</div>
</form>
</body>
</html>
welcome02.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="C" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<html>
<head>
<title>Title</title>
</head>
<body>
<div>
<form>
<div class="col-md-12 column">
<table bgcolor="red" align="center" border="2">
<tr>
<th>用户ID</th>
<th>用户性名</th>
<th>用户密码</th>
<th>用户性别</th>
<th>用户手机</th>
<th>用户地址</th>
<th>用户邮箱</th>
<th>用户PUID</th>
</tr>
<c:forEach items="${userById}" var="findId">
<tr>
<td>${findId.id}</td>
<td>${findId.name}</td>
<td>${findId.password}</td>
<td>${findId.sex}</td>
<td>${findId.telephone}</td>
<td>${findId.address}</td>
<td>${findId.email}</td>
<td>${findId.PUID}</td>
</tr>
</c:forEach>
</table>
</div>
</form>
</div>
</body>
</html>
项目运行结果