用户模块技术要点:
用户模块功能:
1.登录
2.用户名校验
3.注册
4.忘记密码
5.提交问题答案
6.重置密码
7.获取用户信息
8.更新用户信息
9.退出登录
一、登录:
1、用户名是否存在
2、如果存在则将密码转换为MD5加密形式
3、校验用户名和密码是否正确
4、正确则将用户放入到Session中
具体实现代码如下:
Controller层:
@Controller //WEB层(用于标注类本身的用途)
@RequestMapping("/user/") //将请求地址的前面加上/user
public class UserController {
//按类型进行注入
//将iUserService注入进来
@Autowired
private IUserService iUserService;
//登陆功能
//访问地址为login.do 访问方式为POST
@RequestMapping(value = "login.do",method = RequestMethod.POST)
@ResponseBody //自动通过SpingMvc的json插件将返回值转换成json
public ServerResponse<User> login(String username, String password, HttpSession session) {
ServerResponse<User> response = iUserService.login(username, password);
if (response.isSuccess()) //如果登陆成功,将用户放入到Session中
session.setAttribute(Const.CURRENT_USER, response.getData());
return response; //将响应信息返回到前端
}
}
interface IUserService :
public interface IUserService {
ServerResponse<User> login(String username, String password);
}
Service层:
//Service表示业务层
//创建iUserService对象,放入到容器中
@Service("iUserService")
public class UserServiceImpl implements IUserService {
@Autowired //注入userMapper
private UserMapper userMapper;
@Override
public ServerResponse<User> login(String username, String password) {
int resultCount = userMapper.checkUsername(username); //先查询用户名,看用户名是否存在
if (resultCount == 0) //如果查不到的话,用户不存在
return ServerResponse.createByErrorMessage("用户名不存在");
String md5Password = MD5Util.MD5EncodeUtf8(password); //将密码转化为MD5
User user = userMapper.selectLogin(username, md5Password); //通过用户名和密码进行查询
if (user == null)
return ServerResponse.createByErrorMessage("密码错误");
//将密码设置为空
user.setPassword(org.apache.commons.lang3.StringUtils.EMPTY);
return ServerResponse.createBySuccess("登录成功", user);
}
}
Dao层:
UserMapper:
public interface UserMapper {
int checkUsername(String username); //查询用户名,看此用户名是否存在
//@Param为传入的参数起名字,这样在Dao层可以获得数据
User selectLogin(@Param("username") String username, @Param("password")String password);
}
<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>
二、退出登录:
将用户从Session中 移除
具体实现代码如下:
Controller层:
@RequestMapping(value = "log