登录功能

UserMapper

  /**
     * 登录
     * @param user
     * @return
     */
    public User login(User user);

UserMapper.xml

<!--登录-->
    <select id="login" resultType="com.itcast.entity.User">
      select * from user where username=#{username} and password =#{password} and userrole=#{userrole}
    </select>

枚举类utils

package com.itcast.utils;

public class GlobalResult {
    private int code;
    private String msg;
    private Object data;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    private GlobalResult(int code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static GlobalResult of(int code, String msg, Object data) {
        return new GlobalResult(code, msg, data);
    }

    public static GlobalResult of(int code, String msg) {
        return of(code, msg, null);
    }

    public static GlobalResult of(int code) {
        return of(code, "");
    }

    public static GlobalResult ok() {
        return ok("ok");
    }


    public static GlobalResult ok(String msg) {
        return ok(msg, null);
    }

    public static GlobalResult ok(String msg, Object data) {
        return of(200, msg, data);
    }


    public static GlobalResult error(String msg, Object data) {
        return of(500, msg, data);
    }

    public static GlobalResult error(String msg) {
        return error(msg, null);
    }

    public static GlobalResult error() {
        return error("error");
    }

}

package com.itcast.controller;

import com.itcast.entity.User;
import com.itcast.service.UserService;
import com.itcast.utils.GlobalResult;
import com.itcast.utils.LogUtil;
import com.itcast.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    @ResponseBody
    public GlobalResult tologin(@RequestBody User user, HttpServletRequest request) {
   
        String s = user.getUsername();
        String s1 = user.getPassword();
        String s2 = user.getUserrole();
        user.setUsername(s);
        user.setPassword(s1);
        user.setUserrole(s2);
        User login = userService.login(user);
         if (null != login) {
            request.getSession().setAttribute("user",user);
             LogUtil.log("登录成功");
            return GlobalResult.ok();
        } else {
             LogUtil.log("登录失败,信息错误!!!");
             return GlobalResult.error("登录失败,信息错误!!!");
        }
    }}

前端

  <script src="${pageContext.request.contextPath}/js/jquery-3.4.1.min.js"></script>
    <script>
        $(function () {
            $("#btn").click(function () {

                var pram ={
                    username:$("#username").val(),
                    password:$("#password").val(),
                    userrole:$("input[name='userrole']:checked").val()
                }
                $.ajax({
                    url:"login",
                    data:JSON.stringify(pram),
                    contentType:"application/json;charset:UTF-8",
                    type:"post",
                    success:function (data) {
                        if (data.code=="200"){
                            $("#tips").text("登录成功");
                            setTimeout(function(){
                                location.href='http://localhost:8080/menu.jsp'
                            },1000)
                        }  {
                            $("#tips").text(data.msg);

                        }
                    },
                })
            })
        })

    </script>
<body>
<div class="login layui-anim layui-anim-up">
    <div class="message">用户登录</div>
    <div id="darkbannerwrap"></div>

    <form method="post" class="layui-form" >
        <input name="username" placeholder="用户名" type="text" lay-verify="required" class="layui-input" id="username">
        <hr class="hr15">
        <input name="password" lay-verify="required" placeholder="密码"  type="password" class="layui-input" id="password">
        <hr class="hr15">
        <div>
            <label></label>
            <label>管理员</label>
            <input type="radio" name="userrole" value="管理员"  checked="checked">
            <label>用户</label>
            <input type="radio" name="userrole"  value="用户">
        </div>
        <br>
        <input value="登录" lay-submit lay-filter="login" style="width:100%;" type="button" id="btn">

        <label id="tips" style="margin-left: 170px;color: red;"></label>

        <hr class="hr20" >
    </form>
</div>
</body>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值