源代码:
//控制层
@ResponseBody
@PostMapping("/doRegister")
public JSONObject register(String userName,String password,String email,String code,HttpServletRequest request,HttpServletResponse response){
JSONObject result = new JSONObject();
result.put("err", 1);
HttpSession session = request.getSession(true);
String verCode = (String)session.getAttribute(CODE_NAME);
if(!verCode.equalsIgnoreCase(code)){
result.put("msg", "验证码输入错误");
//怀疑是这里的判断导致了重复数据问题
}else if(bbsUserService.hasUser(userName)){
result.put("msg", "用户已经存在");
}else{
BbsUser user = new BbsUser();
user.setBalance(10);
user.setLevel(1);
user.setScore(10);
user = bbsUserService.setUserAccount(user);
WebUtils.loginUser(request, response, user, true);
result.put("err", 0);
result.put("msg", "/bbs/index");
}
return result;
}
//服务层
@Service
public class BbsUserServiceImpl implements BbsUserService {
@Override
@Transactional
public BbsUser setUserAccount(BbsUser user) {
String pwd = HashUtil.generatePwd(password);
user.setPassword(pwd);
return bbsUserRepository.save(user);
}
//试过不同的写法
@Override
public boolean hasUser(String userName) {
// return bbsUserRepository.existsByUserName(userName);
BbsUser byUserName = bbsUserRepository.findByUserName(userName);
return byUserName == null ? false:true;
}
}
但是有时候运行的时候会有两条插入语句,偶尔又会没有。不知道为什么。
Hibernate: insert into bbs_user (balance, corp, email, level, password, score, user_name) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into bbs_user (balance, corp, email, level, password, score, user_name) values (?, ?, ?, ?, ?, ?, ?)
有没有遇到过同样问题的,或者有什么思路。。求解/(ㄒoㄒ)/~~
湖上湖
喵喔喔
幕布斯7119047
相关分类