在执行保存添加操作时判断是否有id【以区分编辑操作】
Service
public void addLcsx(LcsxVo lcsxVo) {
Lcsx lcsx = lcsxVo.getLcsx();
if (StringUtils.isNotEmpty(lcsx.getName()) && lcsx.getSxtz() != null && lcsx.getLcyy() != null) {
lcsx.setSxtz(HtmlUtils.htmlUnescape(lcsx.getSxtz()));
if (StringUtils.isEmpty(lcsx.getId())) {
lcsx.setId(IdUtil.getShortUUID());
}
lcsxRepository.save(lcsx);
} else {
throw new ServiceException("插入失败,数据不能为空!");
}
Util写一个UUID
public class IdUtil {
public static String getShortUUID() {
UUID uuid = UUID.randomUUID();
Base32 base32 = new Base32();
ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
bb.putLong(uuid.getMostSignificantBits());
bb.putLong(uuid.getLeastSignificantBits());
return base32.encodeToString(bb.array()).replace("=", "");
}
}