登入API

package com.mmall.pojo;

import java.util.Date;

public class User {
    private Integer id;

    private String username;

    private String password;

    private String email;

    private String phone;

    private String question;

    private String answer;

    private Integer role;

    private Date createTime;

    private Date updateTime;

    public User(Integer id, String username, String password, String email, String phone, String question, String answer, Integer role, Date createTime, Date updateTime) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.email = email;
        this.phone = phone;
        this.question = question;
        this.answer = answer;
        this.role = role;
        this.createTime = createTime;
        this.updateTime = updateTime;
    }

    public User() {
        super();
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username == null ? null : username.trim();
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password == null ? null : password.trim();
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email == null ? null : email.trim();
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone == null ? null : phone.trim();
    }

    public String getQuestion() {
        return question;
    }

    public void setQuestion(String question) {
        this.question = question == null ? null : question.trim();
    }

    public String getAnswer() {
        return answer;
    }

    public void setAnswer(String answer) {
        this.answer = answer == null ? null : answer.trim();
    }

    public Integer getRole() {
        return role;
    }

    public void setRole(Integer role) {
        this.role = role;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }
}

controller层

package com.mmall.controller.portal;

import com.mmall.common.Const;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.User;
import com.mmall.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.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user/")
public class UserController {

    @Autowired
    private IUserService iUserService;
    /**
     *用户登入
     * @param username
     * @param password
     * @param session
     * @return
     */
    @RequestMapping(value = "login.do",method = RequestMethod.POST)
    @ResponseBody
    public Object login(String username, String password, HttpSession session){
        //service --> mybatis -->dao
        ServerResponse<User> response = iUserService.login(username,password);
        if(response.isSuccess()){
            session.setAttribute(Const.CURRENT_USER,response.getData());
        }
        return  response;
    }

}

service接口

package com.mmall.service;

import com.mmall.common.ServerResponse;
import com.mmall.pojo.User;

public interface IUserService {
    ServerResponse<User> login(String username , String password);
}

service

package com.mmall.service.Impl;

import com.mmall.common.ServerResponse;
import com.mmall.dao.UserMapper;
import com.mmall.pojo.User;
import com.mmall.service.IUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("iUserService")
public class UserServiceImpl implements IUserService{

    @Autowired
    private UserMapper userMapper;

    @Override
    public ServerResponse<User> login(String username, String password) {
        int resultCount = userMapper.checkUsername(username);
        if(resultCount == 0){
            return ServerResponse.createByErrorMessage("用户名不存在");
        }
        //todo 密码MD5加密
        User user =userMapper.selectLogin(username, password);
        if(user == null){
            return  ServerResponse.createByErrorMessage("密码错误");
        }
        user.setPassword(StringUtils.EMPTY);
        return ServerResponse.createBySuccess("登入成功",user);
    }
}

dao层

package com.mmall.dao;

import com.mmall.pojo.User;
import org.apache.ibatis.annotations.Param;

public interface UserMapper {
    int deleteByPrimaryKey(Integer id);

    int insert(User record);

    int insertSelective(User record);

    User selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(User record);

    int updateByPrimaryKey(User record);

    int checkUsername(String name);

    User selectLogin(@Param("username") String username, @Param("password") String password);
}

mapper.xml

<?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.mmall.dao.UserMapper" >
  <resultMap id="BaseResultMap" type="com.mmall.pojo.User" >
    <constructor >
      <idArg column="id" jdbcType="INTEGER" javaType="java.lang.Integer" />
      <arg column="username" jdbcType="VARCHAR" javaType="java.lang.String" />
      <arg column="password" jdbcType="VARCHAR" javaType="java.lang.String" />
      <arg column="email" jdbcType="VARCHAR" javaType="java.lang.String" />
      <arg column="phone" jdbcType="VARCHAR" javaType="java.lang.String" />
      <arg column="question" jdbcType="VARCHAR" javaType="java.lang.String" />
      <arg column="answer" jdbcType="VARCHAR" javaType="java.lang.String" />
      <arg column="role" jdbcType="INTEGER" javaType="java.lang.Integer" />
      <arg column="create_time" jdbcType="TIMESTAMP" javaType="java.util.Date" />
      <arg column="update_time" jdbcType="TIMESTAMP" javaType="java.util.Date" />
    </constructor>
  </resultMap>
  <sql id="Base_Column_List" >
    id, username, password, email, phone, question, answer, role, create_time, update_time
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select
    <include refid="Base_Column_List" />
    from mmall_user
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from mmall_user
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.mmall.pojo.User" >
    insert into mmall_user (id, username, password,
      email, phone, question,
      answer, role, create_time,
      update_time)
    values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
      #{email,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR}, #{question,jdbcType=VARCHAR},
      #{answer,jdbcType=VARCHAR}, #{role,jdbcType=INTEGER},now(),
      now())
  </insert>
  <insert id="insertSelective" parameterType="com.mmall.pojo.User" >
    insert into mmall_user
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="username != null" >
        username,
      </if>
      <if test="password != null" >
        password,
      </if>
      <if test="email != null" >
        email,
      </if>
      <if test="phone != null" >
        phone,
      </if>
      <if test="question != null" >
        question,
      </if>
      <if test="answer != null" >
        answer,
      </if>
      <if test="role != null" >
        role,
      </if>
      <if test="createTime != null" >
        create_time,
      </if>
      <if test="updateTime != null" >
        update_time,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=INTEGER},
      </if>
      <if test="username != null" >
        #{username,jdbcType=VARCHAR},
      </if>
      <if test="password != null" >
        #{password,jdbcType=VARCHAR},
      </if>
      <if test="email != null" >
        #{email,jdbcType=VARCHAR},
      </if>
      <if test="phone != null" >
        #{phone,jdbcType=VARCHAR},
      </if>
      <if test="question != null" >
        #{question,jdbcType=VARCHAR},
      </if>
      <if test="answer != null" >
        #{answer,jdbcType=VARCHAR},
      </if>
      <if test="role != null" >
        #{role,jdbcType=INTEGER},
      </if>
      <if test="createTime != null" >
        now(),
      </if>
      <if test="updateTime != null" >
        now(),
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.mmall.pojo.User" >
    update mmall_user
    <set >
      <if test="username != null" >
        username = #{username,jdbcType=VARCHAR},
      </if>
      <if test="password != null" >
        password = #{password,jdbcType=VARCHAR},
      </if>
      <if test="email != null" >
        email = #{email,jdbcType=VARCHAR},
      </if>
      <if test="phone != null" >
        phone = #{phone,jdbcType=VARCHAR},
      </if>
      <if test="question != null" >
        question = #{question,jdbcType=VARCHAR},
      </if>
      <if test="answer != null" >
        answer = #{answer,jdbcType=VARCHAR},
      </if>
      <if test="role != null" >
        role = #{role,jdbcType=INTEGER},
      </if>
      <if test="createTime != null" >
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null" >
        update_time = now(),
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.mmall.pojo.User" >
    update mmall_user
    set username = #{username,jdbcType=VARCHAR},
      password = #{password,jdbcType=VARCHAR},
      email = #{email,jdbcType=VARCHAR},
      phone = #{phone,jdbcType=VARCHAR},
      question = #{question,jdbcType=VARCHAR},
      answer = #{answer,jdbcType=VARCHAR},
      role = #{role,jdbcType=INTEGER},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      update_time = now()
    where id = #{id,jdbcType=INTEGER}
  </update>
  <select id="checkUsername" resultType="int" parameterType="string" >
      select count(1) from mmall_user
      where username = #{username}
  </select>

  <select id="selectLogin" resultMap="BaseResultMap" parameterType="map">
    select
    <include refid="Base_Column_List" />
    FROM  mmall_user
    where username =#{username}
    and password = # {password}

  </select>

</mapper>

commen包

package com.mmall.common;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.map.annotate.JsonSerialize;

import java.io.Serializable;

@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
//保证序列化json的时候,如果是null的对象,key会消失
public class ServerResponse<T> implements Serializable {

    private  int status;
    private String msg;
    private  T data;

    //构造方法
    private ServerResponse(int status){
        this.status = status;
    }
    private ServerResponse(int status,T data){
        this.status = status;
        this.data = data;
    }

    private ServerResponse(int status,String msg,T data){
        this.status = status;
        this.msg = msg;
        this.data =  data;
    }
    //思考当泛型是 String时候 调用哪个?
    private ServerResponse(int status,String msg){
        this.status = status;
        this.msg = msg;
    }
    /*
    //当第二个参数为String时调用 String 这个参数构造器 否则就是泛型的那个
    //问题二 就是泛型放String 类型 然后 调用哪个 后面规避这个问题
    public static void main(String[] args) {
        ServerResponse sr1 = new ServerResponse(1,new Object());
        ServerResponse sr2 = new ServerResponse(1,"abc");
        System.out.println("console");
    }
    */
    //如果返回的是 ResponseCode.SUCCESS.getCode()是0 就是成功的
    @JsonIgnore
    //使之不在json序列化结果当中
    public boolean isSuccess(){
        return  this.status == ResponseCode.SUCCESS.getCode();
    }
    //快捷键alt + insert 获得set get 方法
    public int getStatus() {
        return status;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return data;
    }

    public  static  <T> ServerResponse<T> createBySuccess(){
        return  new ServerResponse<T>(ResponseCode.SUCCESS.getCode());
    }

    public  static  <T> ServerResponse<T> createBySuccessMessage(String msg){
        return  new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),msg);
    }

    public  static  <T> ServerResponse<T> createBySuccess(T date){
        return  new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),date);
    }

    public  static  <T> ServerResponse<T> createBySuccess(String msg,T date){
        return  new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),msg,date);
    }

    public  static  <T> ServerResponse<T> createByError(){
        return  new ServerResponse<T>(ResponseCode.ERROR.getCode(),ResponseCode.ERROR.getDesc());
    }

    public  static  <T> ServerResponse<T> createByErrorMessage(String errorMessage){
        return  new ServerResponse<T>(ResponseCode.ERROR.getCode(),errorMessage);
    }

    public  static  <T> ServerResponse<T> createByErrorCodeMessage(int errorCode,String errorMessage){
        return  new ServerResponse<T>(errorCode,errorMessage);
    }
}
package com.mmall.common;

public enum  ResponseCode {
    SUCCESS(0,"success"),
    ERROR(1,"error"),
    NEED_LOGIN(10,"NEED_LOGIN"),
    ILLEGAL_ARGUMENT(2,"ILLEGAL_ARGUMENT"); //参数错误

    private  final  int code;   //两个属性
    private  final String desc;

    ResponseCode(int code , String desc){
        this.code = code;
        this.desc = desc;
    }

    public int getCode(){
        return  code;
    }

    public  String  getDesc(){
        return  desc;
    }
}
package com.mmall.common;

public class Const {
    public  static  final  String CURRENT_USER= "currentUser";
}
时间: 2024-08-08 01:07:36

登入API的相关文章

利用未公开API获取终端会话闲置时间(Idle Time)和登入时间(Logon Time)

利用未公开API获取终端会话闲置时间(Idle Time)和登入时间(Logon Time)作者:Tuuzed(土仔)   发表于:2008年3月3日23:12:38 版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本声明.http://www.cppblog.com/tuuzed/archive/2008/03/03/43631.html 可能很多人都知道NT系统的query user命令,命令返回“使用者名称 工作阶段名称 识别码 状态 闲置时间 登入时间”.如图

Asp.Net Identity学习笔记+MVC5默认项目解析_第三方登入&授权总结

Identity学习笔记 Asp.Net Identity学习笔记+MVC5默认项目解析_基础用法 Asp.Net Identity学习笔记+MVC5默认项目解析_授权&Claim Asp.Net Identity学习笔记+MVC5默认项目解析_第三方登入&授权总结 Identity学习笔记第三方登入配置登入案例登入技术总结本地,已登入本地,未登入第三方登入 第三方登入 本文介绍Identity的第三方登入技术.到目前为止只介绍了CookieAuthentication这种授权方式,即浏览

discuz之同步登入

前言 首先感谢dozer学长吧UCenter翻译成C# 博客地址----------->http://www.dozer.cc/ 其次感谢群友快乐々止境同学的热心指导,虽然萍水相逢但让我感受到了世界的温暖 Discuz搭建 同步登入 Discuz是康盛公司下的一套通用的社区论坛软件系统 康盛公司还有其他产品,为了让多个网站一个账号登入就能畅通无阻,于是有了UCenter 本篇介绍Asp.Net网站与Discuz实现同步登入 建议去看看dozer自己写教程http://www.dozer.cc/2

微信小程序登入流程

微信小程序登入流程 前言:openid与unionid openid:是用单个微信应用表示用户的唯一标识.亚洲:饼哥小程序上openid :123,那该用户再张成的小程序上他的opendid不是123,是其他任意一个值,上面的意思:同一用户再不用不同应用上的openid不同,但是再同一应用上唯一. 场景: 假设你们公司有2个小程序.但是你们老板想把用户做统一处理.比如新用户登入任意一个小程序,就发送发送礼包.但是只要再一个小程序上另过了,就不能再另一个上面领取. unionnid:一个用户在多个

linux下查看用户登入系统相关命令及编写脚本(七)

查看用户登入系统日志 1.w 显示谁登入并正在做什么事 2.who 显示谁登入 -r:显示系统运行级别 3.sleep 睡眠命令(sleep 5 睡眠5秒后醒来) 4.whoami 显示用户名或UID 5.last:显示/var/log/wtmp文件,显示用户登录历史及系统重启历史 -n 3: 显示最近3次的相关信息 6.lastb:显示/var/log/btmp文件,显示用户错误的登录尝试 -n 3: 显示最近3次的错误的登录尝试 7.lastlog: 显示每一个用户最近一次的成功登录信息:

Citrix Receiver登入的时候报找不到ICAWebWrapper插件

其实这个问题我在一开始做Citrix项目的时候就遇到很多客户端PC安装Receiver的时候登入云桌面的时就提示"Citrix Receiver提示找不到ICAWebWrapper.msi插件"有时候连卸载都无法卸载掉Citrix Receiver,用clear工具也不行,最后只能从新安装操作系统,这样导致客户和实施方带来了很多不便,特别是财务和一些特殊的办公电脑是不给重新安装操作系统. 最近我在上海实施的时候用户方就提出全院大概30%用户都报这个错误,要求必须解决这个问题,刚开始我们

连接数据库:登入与处理

登入就是提取数据库中的信息 先建立一个登入页面: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta

模拟操作银行登入页面。。。未连接数据库

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <

第七十五天上课 php注册登入审核和文件上传

文件上传 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>文件上传</title> <?php if(!(empty($_FILES['file']) || empty($_POST['submit']))) { $urls="./my-img/".$_FILES['file']['name']; /