JAVA——登錄接口

1、用generator生成好User


2、定義接口

  • com.二級包名.controller.portal下創建UserController java類
package com.mymmall.controller.portal;


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 {
    /**
     * 用戶登錄接口
     * @param username 用戶名
     * @param password 密碼
     * @param session
     * @return
     */
    @RequestMapping(value= "login.do", method = RequestMethod.POST) // 接口名,和使用的方法
    @ResponseBody // 使用json格式
    public Object login(String username, String password, HttpSession session){

        return null;
    }

}


3、實現登錄邏輯

  • com.mymmall.service.impl下創建UserServiceImpl java類
package com.mymmall.service.impl;

import com.mymmall.common.ServerResponse;
import com.mymmall.dao.UserMapper;
import com.mymmall.pojo.User;
import com.mymmall.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

    @Autowired
    // 引用dao的mysql接口
    private UserMapper userMapper;

    @Override
    public ServerResponse<User> login(String username, String password) {
    	// checkUsername 在dao中實現
        int resultCount = userMapper.checkUsername(username);
        if(resultCount == 0){
            return ServerResponse.createByErrorMessage("用戶名不存在");
        }
		
		// selectLogin 在dao中實現
        User user = userMapper.selectLogin(username, password);
        if(user == null){
            return ServerResponse.createByErrorMessage("用戶不存在");
        }

		// 把密碼設置爲空,並把user返回出去給接口使用
        user.setPassword(org.apache.commons.lang3.StringUtils.EMPTY);
        return ServerResponse.createBySuccess("登錄成功", user);
    }
}

  • com.mymmall.service下創建IUserService 接口類
package com.mymmall.service;

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

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


4、與mysql的交互

  • com.mymmall.dao.UserMapper
package com.mymmall.dao;

import com.mymmall.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);
	
	// 定義 checkUsername 函數用於查詢是否有這個用戶
    int checkUsername(String username);

	// 定義 selectLogin 函數用於查詢用戶輸入的這個用戶和密碼是否正確
	// 多個參數時,要用 @Param("一般與參數一樣")
    User selectLogin(@Param("username") String username,@Param("password") String password);
}
  • mappers/UserMapper.xml
  • 加上下面兩個sql
  • id:對應的函數名
  • resultType:返回的數據類型
  • parameterType:使用的參數的數據類型
  • #{} 可以防sql注入,並傳入變量
  <select id="checkUsername" resultType="int" parameterType="string">
    select count(1) from mmall_user
    where username = #{username}
  </select>
  • resultMap="BaseResultMap":返回的類型爲generator定義好的User類型
  • <include refid="Base_Column_List" />:generator定義好的所有此表的字段
  <select id="selectLogin" resultMap="BaseResultMap" parameterType="map">
    select <include refid="Base_Column_List" /> from mmall_user
    where username = ${username} and
    password = #{password}
  </select>

5、實現接口

  • com.二級包名.controller.portal下創建UserController java類
package com.mymmall.controller.portal;


import com.mymmall.common.ServerResponse;
import com.mymmall.pojo.User;
import com.mymmall.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 // 使用json格式
    public ServerResponse<User> login(String username, String password, HttpSession session){
        ServerResponse<User> response = iUserService.login(username, password);
        if(response.isSuccess()){
        	// 如果成功,則把用戶放進session中
            session.setAttribute(“currentUser”, response.getData());
        }
        return response;
    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章