up:: 判断邮箱是否已经注册

说明: 本篇博客继续上一篇判断邮箱是否已经注册,继续开发

邮箱是否注册

1. userService新增方法checkEmailRegisted()

 
@Override
public boolean checkEmailRegistered(String emailAddress) {
    User user = userMapper.selectOneByEmailAddress(emailAddress);
    if (user != null) {
        return false;
    }
    return true;
}

本方法主要作用是检查邮件是否注册

2.反向ALT+ENTER生成接口

boolean checkEmailRegistered(String emailAddress);

3.继续开发mapper层,查询邮箱是否注册

在xml文件中:

<select id="selectOneByEmailAddress" parameterType="java.lang.String" resultMap="BaseResultMap">
  select
  <include refid="Base_Column_List"/>
  from imooc_mall_user
  where email_address = #{email_address,jdbcType=VARCHAR}
  limit 1
</select>

记得在java文件生成查询接口

 
package com.imooc.mall.model.dao;
 
import com.imooc.mall.model.pojo.User;
import org.springframework.data.repository.query.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);
 
 
 
    /**
     * 根据UserName,查询User对象
     * @param userName
     * @return
     */
    User selectByName(String userName);
    /**
     * 登录时使用:根据用户名和密码两个条件,查询User
     * @param userName
     * @param password
     * @return
     */
    User selectLogin(@Param("userName") String userName, @Param("password")String password);
 
    User selectOneByEmailAddress(String emailAddress);
}

4.回到controller层,开发检查注册格式是否存在问题

/**
* 发送邮件
*/
@PostMapping("/user/sendEmail")
@ResponseBody
public ApiRestResponse sendEmail(@RequestParam("emailAddress") String emailAddress)
       throws ImoocMallException {
   //检查邮件地址是否有效,检查是否已注册
   boolean validEmailAddress = EmailUtil.isValidEmailAddress(emailAddress);
   if (validEmailAddress) {
       boolean emailPassed = userService.checkEmailRegistered(emailAddress);
       if (!emailPassed) {
           return ApiRestResponse.error(ImoocMallExceptionEnum.EMAIL_ALREADY_BEEN_REGISTERED);
       } else {
           emailService.sendSimpleMessage(emailAddress, Constant.EMAIL_SUBJECT, "欢迎注册,您的验证码是");
       }
       return ApiRestResponse.success();
   }
 
}

说明: 这里需要进行异常拦截处理

上面邮箱注册检查问题通过后,就可以开始邮箱验证码注册啦

正式开发邮箱注册

1.新建EmailServiceImpl.java文件,去实现邮箱发送验证码

 
/**
 * 描述:     EmailService实现类
 */
@Service
public class EmailServiceImpl implements EmailService {
 
    @Autowired
    private JavaMailSender mailSender;
 
    @Override
    public void sendSimpleMessage(String to, String subject, String text) {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setFrom(Constant.EMAIL_FROM);
        simpleMailMessage.setTo(to);
        simpleMailMessage.setSubject(subject);
        simpleMailMessage.setText(text);
        mailSender.send(simpleMailMessage);
    }
 
}

说明:

为了发送邮件,我们需要在配置文件进行配置,主要配置和邮件相关内容

2.反向生成service的接口

/**
* 描述:     邮件Service
*/
public interface EmailService {
 
   void sendSimpleMessage(String to, String subject, String text);
 
   Boolean saveEmailToRedis(String emailAddress, String verificationCode);
 
   Boolean checkEmailAndCode(String emailAddress, String verificationCode);
}

3.开发Controller层,进行开发邮箱注册的验证码邮件接收

/**
* 发送邮件
*/
@PostMapping("/user/sendEmail")
@ResponseBody
public ApiRestResponse sendEmail(@RequestParam("emailAddress") String emailAddress)
       throws ImoocMallException {
   //检查邮件地址是否有效,检查是否已注册
   boolean validEmailAddress = EmailUtil.isValidEmailAddress(emailAddress);
   if (validEmailAddress) {
       boolean emailPassed = userService.checkEmailRegistered(emailAddress);
       if (!emailPassed) {
           return ApiRestResponse.error(ImoocMallExceptionEnum.EMAIL_ALREADY_BEEN_REGISTERED);
       } else {
           emailService.sendSimpleMessage(emailAddress, Constant.EMAIL_SUBJECT, "欢迎注册,您的验证码是");
       }
       return ApiRestResponse.success();
   }  else {
   		    return ApiRestResponse.error(ImoocMallExceptionEnum.WRONG_EMAIL);
   			}
 
}

说明: 记得回到Constant.java文件加上静态变量

上面的代码不是特别完善,比如说这个验证码没加,但是我们有一个良好的习惯,就是不要等到所有的代码都全部写完再去验证,我们最好的是能边写边验证,避免写了很多后发生报错,一头雾水。

结果测试

1.打开postman

2.qq邮箱信息查看

说明: SpringBoot 里的配置文件的配置如何起作用,底层原理不清楚,待解决。。。