userModel变量的id为空,为什么userDO的id有值?userModel的id为空怎么可一复制给userPassword?

来源:3-10 用户模型管理--用户注册功能实现01

慕虎9169884

2019-11-19 22:34

http://img4.mukewang.com/5dd3fc0e0001fd5708990256.jpg

http://img3.mukewang.com/5dd3fd350001b34c04840169.jpg

http://img4.mukewang.com/5dd3fd35000182e909800263.jpg

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;	}}


写回答 关注

2回答

  • 慕慕1568843
    2021-01-28 23:30:56

    视频作者,少写了步骤,这块需要你自己添加上。

    1. 执行userDOmapper.insertSelective(userDO)之后,userDO这个对象的id就有了

    2. 你需要使用userDO的id,然后再设置给userpasswordDO.

    3. userModel里面是没有user id的值的。


  • 慕瓜8413892
    2019-12-19 18:04:46

    insert到sql字段id是会自己自增的,你基础有点问题

SpringBoot构建电商基础秒杀项目

应用SpringBoot快速搭建拥有用户、商品、交易及秒杀活动的电商秒杀应用。

49065 学习 · 954 问题

查看课程

相似问题