4-1 Add and update user interface

Create a form view object

@Getter
@Setter
public class UserParam {

    private Integer id;

    @NotBlank(message = "Username cannot be empty")
    @Length(min = 1, max = 20, message = "Username length needs to be within 20 characters")
    private String username;

    @NotBlank(message = "Phone cannot be blank")
    @Length(min = 1, max = 13, message = "The length of the phone needs to be within 13 characters")
    private String telephone;

    @NotBlank(message = "Mailbox is not allowed to be empty")
    @Length(min = 5, max = 50, message = "The length of the mailbox needs to be within 50 characters")
    private String mail;

    @NotNull(message = "The user's department must be provided")
    private Integer deptId;

    @NotNull(message = "User's status must be specified")
    @Min(value = 0, message = "User status is invalid")
    @Max(value = 2, message = "User status is invalid")
    private Integer ststus;

    @Length(min = 0, max = 200, message = "The length of the note needs to be within 200 words")
    private String remark = "";
}

Add user service

@Service
public class SysUserService {
	
	@Autowired
	private SysUserMapper sysUserMapper;
	
	public void save(@Valid UserParam param) {
		if(checkTelephoneExist(param.getTelephone(), param.getId())) {
			throw new ParamException("The phone is already in use");
		}
		if(checkEmailExist(param.getMail(), param.getId())) {
			throw new ParamException("The mailbox is already occupied");
		}
		
		String password = "123456";
		String encryptedPassword = MD5Util.encrypt(password);
		
		SysUser sysUser = SysUser.builder().username(param.getUsername())
								  .telephone(param.getTelephone())
								  .mail(param.getMail())
								  .password(encryptedPassword)
								  .deptId(param.getDeptId())
								  .stasus(param.getStstus())
								  .remark(param.getRemark())
								  .build();
		sysUser.setOperator(RequestHolder.getCurrentUser().getUsername()); // Get the username of the current thread
		sysUser.setOperateIp("127.0.0.1"); // TODO
		sysUser.setOperateTime(new Date());
		
		// TODO:send email
		
		sysUserMapper.insert(sysUser);
	}
	
}

Attachment: MD5 encryption tool class

@ Slf4j
public class MD5Util {

    public final static String encrypt(String s) {
        char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
        try {
            byte[] btInput = s.getBytes();
            // Get the MessageDigest object of the MD5 digest algorithm
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            // Update the digest with the specified bytes
            mdInst.update(btInput);
            // get ciphertext
            byte[] md = mdInst.digest();
            // Convert ciphertext to hexadecimal string form
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            log.error("generate md5 error, {}", s, e);
            return null;
        }
    }
}

update user service

public void update(@Valid UserParam param) {
	if(checkTelephoneExist(param.getTelephone(), param.getId())) {
		throw new ParamException("The phone is already in use");
	}
	if(checkEmailExist(param.getMail(), param.getId())) {
		throw new ParamException("The mailbox is already occupied");
	}
	
	SysUser before = sysUserMapper.selectByPrimaryKey(param.getId());
	Preconditions.checkNotNull(before, "The user to be updated does not exist");
	SysUser after = SysUser.builder().id(param.getId())
									 .username(param.getUsername())
									 .telephone(param.getTelephone())
									 .mail(param.getMail())
									 .deptId(param.getDeptId())
									 .stasus(param.getStstus())
									 .remark(param.getRemark())
									 .build();
	after.setOperator(RequestHolder.getCurrentUser().getUsername()); // Get the username of the current thread
	after.setOperateIp("127.0.0.1"); // TODO
	after.setOperateTime(new Date());
	
	sysUserMapper.updateByPrimaryKeySelective(after); // The field is empty and not updated
}

Add User, Update User Controller

@Controller
@RequestMapping("/sys/user")
public class SysUserController {
	
	@Autowired
	private SysUserService sysUserService;
	
	@RequestMapping("/save")
	@ResponseBody
	public JsonData saveDept (UserParam param)
		sysUserService.save(param);
		return JsonData.success();
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public JsonData updateDept(UserParam param) {
		sysUserService.update(param);
		return JsonData.success();
	}
	
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324760377&siteId=291194637