慕虎9169884
2019-11-19 22:34
package com.miaoshaproject.service.impl;import org.springframework.beans.BeanUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;import com.alibaba.druid.util.StringUtils;import com.miaoshaproject.dao.UserDOMapper;import com.miaoshaproject.dao.UserPasswordDOMapper;import com.miaoshaproject.dataobject.UserDO;import com.miaoshaproject.dataobject.UserPasswordDO;import com.miaoshaproject.error.BusinessException;import com.miaoshaproject.error.EmBusinessError;import com.miaoshaproject.model.UserModel;import com.miaoshaproject.service.UserService;@Servicepublic class UserServiceImpl implements UserService { @Autowired private UserDOMapper userDoMapper; @Autowired private UserPasswordDOMapper userPasswordDOMapper; @Override public UserModel getUserById(Integer id) { // 调用userdomapper获取到对应的用户dataobject UserDO userDO = userDoMapper.selectByPrimaryKey(id); if (userDO == null) { return null; } // 通过用户id获取对应的用户加密密码信息 UserPasswordDO userPasswordDO = userPasswordDOMapper.selectByUserId(userDO.getId()); return convertFromDataObject(userDO, userPasswordDO); } private UserModel convertFromDataObject(UserDO userDO, UserPasswordDO userPasswordDO) { if (userDO == null) { return null; } UserModel userModel = new UserModel(); BeanUtils.copyProperties(userDO, userModel); if (userPasswordDO != null) { userModel.setEncrptPassword(userPasswordDO.getEncrptPassword()); } return userModel; } @Override @Transactional public void register(UserModel userModel) throws BusinessException { if (userModel == null) { throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR); } // 校验是否为空 if (StringUtils.isEmpty(userModel.getName()) || userModel.getGender() == null || userModel.getAge() == null || StringUtils.isEmpty(userModel.getTelphone())) { throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR); } UserDO userDO=converFromModel(userModel); userDoMapper.insertSelective(userDO); UserPasswordDO userPasswordDO=convertPasswordFromModel(userModel); userPasswordDOMapper.insertSelective(userPasswordDO); } // 实现Model->dataobject方法 private UserDO converFromModel(UserModel userModel) { if (userModel == null) { return null; } UserDO userDO = new UserDO(); BeanUtils.copyProperties(userModel, userDO); return userDO; } private UserPasswordDO convertPasswordFromModel(UserModel userModel) { if (userModel==null) { return null; } UserPasswordDO userPasswordDO=new UserPasswordDO(); userPasswordDO.setEncrptPassword(userModel.getEncrptPassword()); userPasswordDO.setUserId(userModel.getId()); return userPasswordDO; }}
视频作者,少写了步骤,这块需要你自己添加上。
执行userDOmapper.insertSelective(userDO)之后,userDO这个对象的id就有了
你需要使用userDO的id,然后再设置给userpasswordDO.
userModel里面是没有user id的值的。
insert到sql字段id是会自己自增的,你基础有点问题
SpringBoot构建电商基础秒杀项目
49065 学习 · 954 问题
相似问题