本文参加了「思否技术征文」,欢送正在浏览的你也退出。
一、前言在Java开发过程中,实现用户的注册性能是最根本的,用户通过手机号或者邮箱作为注册账号也是十分常见的操作形式,不论是通过手机号注册或者邮箱注册,原理都差不多,那么本文就来分享一下在Java开发过程中的用户注册账号的性能实现。二、筹备工作1、通过Java语言来实现用户注册登录的后盾性能;2、应用环境有JDK6、Eclipse、Oracle10G、Tomcat等;
三、具体实现思路及外围步骤1、数据库设计①数据库的表名称以及要求:表名:users 主键:id 字段名称:id:用户id,username:用户名称,password:明码,group_id:用户类型id ②创立数据表,创立主、外键,创立序列,新加测试数据2、应用Eclipse创立web我的项目UserDemo3、给我的项目工程增加Spring、Hibernate等反对,并且正确引入集成到我的项目中,以及配置4、创立数据长久化类,以及对应的映射文件,让用户类型和用户之间建设双向一对多的关系5、新建接口以及实现类,应用spring数据库对象实现对应数据库的操作6、创立service接口以及实现类,并且实现对应的业务逻辑7、创立action类,并引入接口和拜访器,实现配置文件8、新建spring配置文件,实现对应的对象申明和配置9、前端局部的界面搭建,以及接口联调10、测试环节:调试运行胜利之后将对应的相干数据库对象导出sql文件,以及用户注册数据的备份机制解决,实现测试,实现用户注册登录的性能。
四、外围代码1、UserService.java文件的外围代码public interface UserService { /** * 用户注册 * * @param userId * @param dto * @throws Exception */ void userRegister(Long userId, UserRegisterDTO dto) throws Exception; /** * 遗记明码 * * @param userId * @param dto * @throws Exception */ void updatePassword(Long userId, UpdatePasswordDTO dto) throws Exception; /** * 通过邮箱发送验证码 * * @param userId * @param email * @throws BusinessException */ void sendVerificationCode(Long userId, String email) throws BusinessException; /** * 通过用户名明码获取用户 * * @param loginName * @param loginPwd * @return * @throws BusinessException */ User getUser(String loginName, String loginPwd) throws BusinessException;}2、UserController.java文件的外围代码@RestController@Slf4jpublic class UserController extends BaseController { private final UserService userService; @Autowired public UserController(UserService userService) { this.userService = userService; } /** * 会员注册 * * @param dto * @param request * @return * @throws Exception */ @ApiOperation(value = "会员注册", produces = "application/json") @ApiResponses({@ApiResponse(code = AjaxReturn.SUCCESS, message = "注冊胜利", response = AjaxReturn.class)}) @PostMapping(path = {"/user-save"}) public AjaxReturn userRegister(@ModelAttribute UserRegisterDTO dto, HttpServletRequest request) throws Exception { log.info(dto.toString()); Long userId = getAuthentication(request); if (StringUtils.isBlank(dto.getMobile()) && StringUtils.isBlank(dto.getEmail())) { throw new BusinessException("请输出手机号或邮箱"); } if (StringUtils.isNotBlank(dto.getMobile()) && !StringUtils.isNumeric(dto.getMobile())) { throw new BusinessException("请输出正确的手机号"); } if (StringUtils.isNotBlank(dto.getEmail()) && !StringUtils.isEmail(dto.getEmail())) { throw new BusinessException("请输出正确的邮箱"); } if (StringUtils.isBlank(dto.getLoginPwd())) { throw new BusinessException("password must not be null"); } // 明码MD5加密 dto.setLoginPwd(DigestUtils.md5Hex(dto.getLoginPwd())); if (StringUtils.isBlank(dto.getVerificationCode())) { throw new BusinessException("verification code must not be null"); } userService.userRegister(userId, dto); return AjaxReturn.builder().build(); } /** * 遗记明码 * * @param dto * @param request * @return * @throws Exception */ @ApiOperation(value = "遗记明码", produces = "application/json") @ApiResponses({@ApiResponse(code = AjaxReturn.SUCCESS, message = "更新明码胜利", response = AjaxReturn.class)}) @PostMapping(path = {"/user-password-forget"}) public AjaxReturn updatePassword(@ModelAttribute UpdatePasswordDTO dto, HttpServletRequest request) throws Exception { Long userId = getAuthentication(request); if (StringUtils.isBlank(dto.getMobile()) && StringUtils.isBlank(dto.getEmail())) { throw new BusinessException("请输出手机号或邮箱"); } if (StringUtils.isNotBlank(dto.getMobile()) && !StringUtils.isNumeric(dto.getMobile())) { throw new BusinessException("请输出正确的手机号"); } if (StringUtils.isNotBlank(dto.getEmail()) && !StringUtils.isEmail(dto.getMobile())) { throw new BusinessException("请输出正确的邮箱"); } if (StringUtils.isBlank(dto.getLoginPwd())) { throw new BusinessException("password must not be null"); } // 明码MD5加密 dto.setLoginPwd(DigestUtils.md5Hex(dto.getLoginPwd())); if (StringUtils.isBlank(dto.getVerificationCode())) { throw new BusinessException("verification code must not be null"); } userService.updatePassword(userId, dto); return AjaxReturn.builder().build(); } /** * 通过邮件发送验证码 * * @param email * @param request * @return * @throws BusinessException */ @ApiOperation(value = "通过邮件发送验证码", produces = "application/json") @ApiResponses({@ApiResponse(code = AjaxReturn.SUCCESS, message = "通过邮件发送验证码胜利", response = AjaxReturn.class)}) @PostMapping(path = {"/verification-code-send"}) public AjaxReturn sendVerificationCode(@ApiParam(name = "email", value = "邮箱", required = true) @RequestParam String email, HttpServletRequest request) throws BusinessException { Long userId = getAuthentication(request); userService.sendVerificationCode(userId, email); return AjaxReturn.builder().build(); }}3、LoginController文件
...