为了账号安全,请及时绑定邮箱和手机立即绑定

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

http://img1.sycdn.imooc.com//5dd3fc0e0001fd5708990256.jpg

http://img1.sycdn.imooc.com//5dd3fd350001b34c04840169.jpg

http://img1.sycdn.imooc.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 回答

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

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

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

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


0 回复 有任何疑惑可以回复我~

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

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

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

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信