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