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 com.mmall.util.MD5Util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.UUID;
@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("用户不存在");
}
String md5Password = MD5Util.MD5EncodeUtf8(password);
User user = userMapper.selectLogin(username, md5Password);
if (user == null) {
return ServerResponse.createByErrorMessage("密码错误");
}
// 处理返回值密码
user.setPassword(StringUtils.EMPTY);
return ServerResponse.createBySuccess("登录成功", user);
}
}
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);
}
<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>