登录功能开发
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了登录功能开发,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含11012字,纯文字阅读大概需要16分钟。
内容图文
package com.mmall.common; /** * Created by think 2018/1/8 */ public class Const { public static final String CURRENT_USER = "currentUser"; }
package com.mmall.common; /** * Created by think on 2018/1/8. */ public enum ResponseCode { SUCCESS(0, "SUCCESS"), ERROR(1, "ERROR"), NEED_LOGIN(10, "NEED_LOGIN"), ILLEGAL_ARGUMENT(2, "ILLEGAL_ARGUMENT"); privatefinalint code; privatefinal String desc; ResponseCode(int code, String desc) { this.code = code; this.desc = desc; } publicint getCode() { return code; } public String getDesc() { return desc; } }
package com.mmall.common; import org.codehaus.jackson.annotate.JsonIgnore; import org.codehaus.jackson.map.annotate.JsonSerialize; import java.io.Serializable; /** * Created by think on 2018/1/8. */ @JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL) //保证序列化json的时候,如果是null的对象,key也会消失publicclass ServerResponse<T> implements Serializable { privateint 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; } private ServerResponse(int status,String msg){ this.status = status; this.msg = msg; } @JsonIgnore //使之不在json序列化结果当中publicboolean isSuccess(){ returnthis.status == ResponseCode.SUCCESS.getCode(); } publicint getStatus(){ return status; } public T getData(){ return data; } public String getMsg(){ return msg; } publicstatic <T> ServerResponse<T> createBySuccess(){ returnnew ServerResponse<T>(ResponseCode.SUCCESS.getCode()); } publicstatic <T> ServerResponse<T> createBySuccessMessage(String msg){ returnnew ServerResponse<T>(ResponseCode.SUCCESS.getCode(),msg); } publicstatic <T> ServerResponse<T> createBySuccess(T data){ returnnew ServerResponse<T>(ResponseCode.SUCCESS.getCode(),data); } publicstatic <T> ServerResponse<T> createBySuccess(String msg,T data){ returnnew ServerResponse<T>(ResponseCode.SUCCESS.getCode(),msg,data); } publicstatic <T> ServerResponse<T> createByError(){ returnnew ServerResponse<T>(ResponseCode.ERROR.getCode(),ResponseCode.ERROR.getDesc()); } publicstatic <T> ServerResponse<T> createByErrorMessage(String errorMessage){ returnnew ServerResponse<T>(ResponseCode.ERROR.getCode(),errorMessage); } publicstatic <T> ServerResponse<T> createByErrorCodeMessage(int errorCode,String errorMessage){ returnnew ServerResponse<T>(errorCode,errorMessage); } }
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; /** * Created by think on 2018/1/8. */ @Controller @RequestMapping("/user/") publicclass UserController { @Autowired private IUserService iUserService; /** * * @param username * @param password * @param session * @return*/ @RequestMapping(value="login.do",method= RequestMethod.POST) @ResponseBody public ServerResponse<User> login(String username, String password, HttpSession session){ ServerResponse<User> response = iUserService.login(username,password); if(response.isSuccess()){ session.setAttribute(Const.CURRENT_USER,response.getData()); } return response; } }
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 username); User selectLogin(@Param("username") String username, @Param("password")String password); }
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.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * Created by think on 2018/1/8. */ @Service("iUserService") publicclass 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("用户名不存在"); } User user = userMapper.selectLogin(username,password); if(user == null){ return ServerResponse.createByErrorMessage("密码错误"); } user.setPassword(org.apache.commons.lang3.StringUtils.EMPTY); return ServerResponse.createBySuccess("登录成功",user); } }
package com.mmall.service; import com.mmall.common.ServerResponse; import com.mmall.pojo.User; /** * Created by think on 2018/1/8. */ public interface IUserService { ServerResponse<User> login(String username, String password); }
<? 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 >
原文:https://www.cnblogs.com/XJJD/p/8244618.html
内容总结
以上是互联网集市为您收集整理的登录功能开发全部内容,希望文章能够帮你解决登录功能开发所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。