User类
package com.lyc.pojo;
import lombok.Data;
@Data
public class User {
private int id;
private String count;
private String password;
}
UserMapper
public interface UserMapper {
//用户登录
User loginUser(User user);
}
UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lyc.mapper.UserMapper">
<select id="loginUser" resultType="com.lyc.pojo.User" parameterType="com.lyc.pojo.User">
select * from ssmbuild.user where count = #{count} and password = #{password}
</select>
</mapper>
UserService
public interface UserService {
//用户登录
User loginUser(User user);
}
UserServiceImpl
public class UserServiceImpl implements UserService{
private UserMapper userMapper;
public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}
@Override
public User loginUser(User user) {
return userMapper.loginUser(user);
}
Controller
@Controller
@RequestMapping("/user")
public class LoginController {
@Autowired
private UserService userService;
//跳转到登录页面
@RequestMapping("/jumplogin")
public String jumpLogin()throws Exception{
return "login";
}
//用户登录
@RequestMapping("/login")
public String login(User user, HttpServletRequest req, ModelAndView modelAndView, HttpSession session){
User login = userService.loginUser(user);
if (login!=null){
req.getSession().setAttribute("login", login);
return "success";
}else{
modelAndView.setViewName("login");
return "fail";
}
}
}
//退出登录
@RequestMapping("/logout")
public String logout(HttpSession session)throws Exception{
//session 过期
session.invalidate();
return "login";
}
}
jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="../../static/layui-v2.6.4/layui/css/layui.css" media="all">
<script src="../../static/layui-v2.6.4/layui/layui.js" charset="utf-8"></script>
<title>登录</title>
</head>
<style>
body{
background-color: #cdc1c1;
}
form{
background-color: #f1eeee;
width: 360px;
height: 200px;
padding: 10px;
margin: 10px auto;
}
</style>
<body>
<form class="layui-form" action="/user/login" onsubmit="return check()">
<div class="layui-form-item">
<label class="layui-form-label layui-icon layui-icon-user"></label>
<div class="layui-input-inline">
<input type="text" name="count" required lay-verify="required" placeholder="请输入用户名" autocomplete="off" class="layui-input ">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label layui-icon layui-icon-password"></label>
<div class="layui-input-inline">
<input type="password" name="password" required lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<div class="layui-input-block">
<button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
<button type="reset" class="layui-btn layui-btn-primary">重置</button>
</div>
</div>
</form>
</body>
</html>