代码
UserService接口
//用户登录
boolean isLogin(String username,String password);
UserServiceImpl实现类
/**
* 用户登录
*与映射文件中查出的对象进行比较,如果正确,返回true
*控制层接受到为true时,跳转到user/data.action后台首页
*接受到false时,留在首页
*/
@Override
public boolean isLogin(String username,String password) {
SUser user=new SUser();
user.setLoginAccount(username);
user.setPwd(password);
List<SUser> users = mapper.selectUser(user);
if(users.size()>0 ){
return true;
}
return false;
}
SUserMapper接口
List<SUser> selectUser(@Param("user")SUser user);
SUserMapper.xml
<resultMap id="BaseResultMap" type="com.hf.pojo.SUser">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Fri Aug 14 10:50:09 CST 2020.
-->
<id column="userId" jdbcType="INTEGER" property="userid" />
<result column="realname" jdbcType="VARCHAR" property="realname" />
<result column="sex" jdbcType="VARCHAR" property="sex" />
<result column="age" jdbcType="INTEGER" property="age" />
<result column="login_account" jdbcType="VARCHAR" property="loginAccount" />
<result column="pwd" jdbcType="VARCHAR" property="pwd" />
<result column="deptId" jdbcType="INTEGER" property="deptid" />
<result column="roleId" jdbcType="INTEGER" property="roleid" />
<result column="status" jdbcType="INTEGER" property="status" />
</resultMap>
<select id="selectUser" resultMap="BaseResultMap">
select * from s_user where login_account = #{user.loginAccount} and pwd = #{user.pwd}
</select>
SystemController
/**
* @author 86188
* 跳转到登录页面
*/
@RequestMapping("/toLogin")
@ResponseBody
public ModelAndView login() {
ModelAndView mav=new ModelAndView("login");
return mav;
}
@RequestMapping("/login")
public String toLogin(String username,String password) {
if(userService.isLogin(username, password)) {
return "redirect:/system/index.action";
}
return "forward:/login.action";
}
login.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%String path=request.getContextPath(); %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>登录--layui后台管理模板</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="format-detection" content="telephone=no">
<link rel="stylesheet" href="<%=path%>/layui/layui/css/layui.css" media="all" />
<link rel="stylesheet" href="<%=path%>/layui/css/login.css" media="all" />
</head>
<body>
<div class="video_mask"></div>
<div class="login">
<h1>B2C电子商务平台登录</h1>
<form class="layui-form" action="login.action" method="post" id="formLogin">
<div class="layui-form-item">
<input class="layui-input" name="username" placeholder="用户名" lay-verify="required" type="text" autocomplete="off">
</div>
<div class="layui-form-item">
<input class="layui-input" name="password" placeholder="密码" lay-verify="required" type="password" autocomplete="off">
</div>
<button class="layui-btn login_btn" lay-submit="" lay-filter="login">登录</button>
</form>
</div>
<script type="text/javascript" src="<%=path%>/layui/layui/layui.js"></script>
</body>
<script type="text/javascript">
$('.login_btn').on('click',function () {
//alert("进入ajax")
$.ajax({
type:"POST",
url:"<%=path%>/user/login",
data:{username:$("username").val(),password:$("password").val()},
success:function (data) {
//alert("ajax成功")
if(data){
layer.msg("登陆成功")
window.location.href="<%=path%>/system/index";
}else{
layer.msg("登录失败,用户名或密码错误")
}
}
});
})
</script>
</html>