用户注册功能部分代码:
实体类:
package com.baizhi.entity; public class User { private Integer id; private String username; private String realname; private String password; private Boolean gender; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getRealname() { return realname; } public void setRealname(String realname) { this.realname = realname; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Boolean getGender() { return gender; } public void setGender(Boolean gender) { this.gender = gender; } public User() { } public User(Integer id, String username, String realname, String password, Boolean gender) { this.id = id; this.username = username; this.realname = realname; this.password = password; this.gender = gender; } }
service:
package com.baizhi.service; import com.baizhi.dao.UserDao; import com.baizhi.entity.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.DigestUtils; import org.springframework.util.ObjectUtils; import java.nio.charset.StandardCharsets; @Service @Transactional public class UserServiceImpl implements UserService{ private UserDao userDao; @Autowired public UserServiceImpl(UserDao userDao) { this.userDao = userDao; } @Override public void register(User user) { //1.根据用户名查询数据库是否存在该用户名 User userDB = userDao.findByUserName(user.getUsername()); if(!ObjectUtils.isEmpty(userDB)) throw new RuntimeException("用户名已存在!"); //进行注册 注册之前给明文密码加密 String passwordSecret = DigestUtils.md5DigestAsHex(user.getPassword().getBytes(StandardCharsets.UTF_8)); user.setPassword(passwordSecret); userDao.save(user); } }
controller: 注册功能:
/* * 用户注册 * * */ @RequestMapping("register") public String register(User user,String code,HttpSession session) throws UnsupportedEncodingException { log.debug("接收到验证码:{}",code); log.debug("用户名:{},真实姓名:{},密码:{},性别:{}",user.getUsername(),user.getRealname(),user.getPassword(),user.getGender()); try { //1.比较验证码输入是否一致 String sessionCode = session.getAttribute("code").toString(); if(!sessionCode.equalsIgnoreCase(code)) throw new RuntimeException("验证码输入错误!"); //2.注册用户 userService.register(user); } catch (RuntimeException e) { e.printStackTrace(); return "redirect:/regist.jsp?msg="+ URLEncoder.encode(e.getMessage(),"UTF-8"); } return "redirect:/login.jsp"; }
实现展示:
标签:String,用户注册,spring,boot,user,gender,password,public,realname From: https://www.cnblogs.com/sxwgzx23/p/18048804