com.baomidou.mybatisplus.core.toolkit.IdWorker Java Examples

The following examples show how to use com.baomidou.mybatisplus.core.toolkit.IdWorker. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CustomMybatisPlusParameterHandler.java    From sqlhelper with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static Object populateKeys(MetaObjectHandler metaObjectHandler, TableInfo tableInfo, MappedStatement ms, Object parameterObject, boolean isInsert) {
    if (null == tableInfo) {
        return parameterObject;
    } else {
        MetaObject metaObject = ms.getConfiguration().newMetaObject(parameterObject);
        if (isInsert && !StringUtils.isEmpty(tableInfo.getKeyProperty()) && null != tableInfo.getIdType() && tableInfo.getIdType().getKey() >= 3) {
            Object idValue = metaObject.getValue(tableInfo.getKeyProperty());
            if (StringUtils.checkValNull(idValue)) {
                if (tableInfo.getIdType() == IdType.ID_WORKER) {
                    metaObject.setValue(tableInfo.getKeyProperty(), IdWorker.getId());
                } else if (tableInfo.getIdType() == IdType.ID_WORKER_STR) {
                    metaObject.setValue(tableInfo.getKeyProperty(), IdWorker.getIdStr());
                } else if (tableInfo.getIdType() == IdType.UUID) {
                    metaObject.setValue(tableInfo.getKeyProperty(), IdWorker.get32UUID());
                }
            }
        }

        if (metaObjectHandler != null) {
            if (isInsert && metaObjectHandler.openInsertFill()) {
                metaObjectHandler.insertFill(metaObject);
            } else if (!isInsert) {
                metaObjectHandler.updateFill(metaObject);
            }
        }

        return metaObject.getOriginalObject();
    }
}
 
Example #2
Source File: BaseLogAop.java    From spring-boot-plus with Apache License 2.0 5 votes vote down vote up
/**
 * 处理请求ID
 *
 * @param requestInfo
 */
protected void handleRequestId(RequestInfo requestInfo) {
    if (!enableRequestId) {
        return;
    }
    String requestId = null;
    if (SpringBootPlusAopProperties.RequestIdType.IDWORK == requestIdType) {
        requestId = IdWorker.getIdStr();
    } else if (SpringBootPlusAopProperties.RequestIdType.UUID == requestIdType) {
        requestId = UUIDUtil.getUuid();
    }
    // 设置请求ID
    MDC.put(REQUEST_ID, requestId);
    requestInfo.setRequestId(requestId);
}
 
Example #3
Source File: RestFileInfoService.java    From Guns with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * 上传文件
 *
 * @author fengshuonan
 * @Date 2019-05-04 17:18
 */
public String uploadFile(MultipartFile file) {

    //生成文件的唯一id
    String fileId = IdWorker.getIdStr();

    //获取文件后缀
    String fileSuffix = ToolUtil.getFileSuffix(file.getOriginalFilename());

    //获取文件原始名称
    String originalFilename = file.getOriginalFilename();

    //生成文件的最终名称
    String finalName = fileId + "." + ToolUtil.getFileSuffix(originalFilename);

    try {
        //保存文件到指定目录
        String fileSavePath = ConstantsContext.getFileUploadPath();
        File newFile = new File(fileSavePath + finalName);
        file.transferTo(newFile);

        //保存文件信息
        RestFileInfo fileInfo = new RestFileInfo();
        fileInfo.setFileId(fileId);
        fileInfo.setFileName(originalFilename);
        fileInfo.setFileSuffix(fileSuffix);
        fileInfo.setFilePath(fileSavePath + finalName);
        fileInfo.setFinalName(finalName);

        //计算文件大小kb
        long kb = new BigDecimal(file.getSize())
                .divide(BigDecimal.valueOf(1024))
                .setScale(0, BigDecimal.ROUND_HALF_UP).longValue();
        fileInfo.setFileSizeKb(kb);
        this.save(fileInfo);
    } catch (Exception e) {
        log.error("上传文件错误!", e);
        throw new ServiceException(BizExceptionEnum.UPLOAD_ERROR);
    }

    return fileId;

}
 
Example #4
Source File: FileInfoService.java    From Guns with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * 上传文件(指定上传路径)
 *
 * @author fengshuonan
 * @Date 2019-05-04 17:18
 */
public UploadResult uploadFile(MultipartFile file, String fileSavePath) {

    UploadResult uploadResult = new UploadResult();

    //生成文件的唯一id
    String fileId = IdWorker.getIdStr();
    uploadResult.setFileId(fileId);

    //获取文件后缀
    String fileSuffix = ToolUtil.getFileSuffix(file.getOriginalFilename());
    uploadResult.setFileSuffix(fileSuffix);

    //获取文件原始名称
    String originalFilename = file.getOriginalFilename();
    uploadResult.setOriginalFilename(originalFilename);

    //生成文件的最终名称
    String finalName = fileId + "." + ToolUtil.getFileSuffix(originalFilename);
    uploadResult.setFinalName(finalName);
    uploadResult.setFileSavePath(fileSavePath + finalName);

    try {
        //保存文件到指定目录
        File newFile = new File(fileSavePath + finalName);
        file.transferTo(newFile);

        //保存文件信息
        FileInfo fileInfo = new FileInfo();
        fileInfo.setFileId(fileId);
        fileInfo.setFileName(originalFilename);
        fileInfo.setFileSuffix(fileSuffix);
        fileInfo.setFilePath(fileSavePath + finalName);
        fileInfo.setFinalName(finalName);

        //计算文件大小kb
        long kb = new BigDecimal(file.getSize())
                .divide(BigDecimal.valueOf(1024))
                .setScale(0, BigDecimal.ROUND_HALF_UP).longValue();
        fileInfo.setFileSizeKb(kb);
        this.save(fileInfo);
    } catch (Exception e) {
        log.error("上传文件错误!", e);
        throw new ServiceException(BizExceptionEnum.UPLOAD_ERROR);
    }

    return uploadResult;

}
 
Example #5
Source File: UeditorUtil.java    From Guns with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * ue上传文件逻辑
 *
 * @author fengshuonan
 * @Date 2019-08-27 12:54
 */
public static UeditorFileResult uploadFile(MultipartFile upfile, FileType fileType) {
    if (upfile.isEmpty()) {
        throw new ServiceException(UE_FILE_NULL_ERROR);
    }

    // 获取文件名,后缀名
    String oldFileName = upfile.getOriginalFilename();
    String suffixName = ToolUtil.getFileSuffix(oldFileName);

    // 重新命名图片
    String newFileName = IdWorker.getIdStr() + "." + suffixName;

    UeditorFileResult ueditorFileResult = new UeditorFileResult();
    String path = null;

    // 如果是上传图片
    if (fileType.equals(FileType.IMG)) {

        ueditorFileResult.setUrl(UeditorUtil.getImageRelativeUrl(newFileName));
        ueditorFileResult.setTitle(newFileName);
        ueditorFileResult.setOriginal(newFileName);

        //文件用原始文件
        path = ConstantsContext.getFileUploadPath() + newFileName;


    } else if (fileType.equals(FileType.FILE)) {

        // 如果是上传文件
        ueditorFileResult.setUrl(UeditorUtil.getFileRelativeUrl(newFileName) + "/" + oldFileName);
        ueditorFileResult.setTitle(oldFileName);
        ueditorFileResult.setOriginal(oldFileName);

        //文件用原始文件
        path = ConstantsContext.getFileUploadPath() + newFileName;

    } else {

        // 如果是上传视频
        ueditorFileResult.setUrl(UeditorUtil.getVideoRelativeUrl(newFileName));
        ueditorFileResult.setTitle(newFileName);
        ueditorFileResult.setOriginal(newFileName);

        //文件用原始文件
        path = ConstantsContext.getFileUploadPath() + newFileName;

    }

    try {

        File dest = new File(path);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }

        upfile.transferTo(dest);
        return ueditorFileResult;
    } catch (IOException e) {
        log.error("保存ue的上传文件出错!", e);
        throw new ServiceException(UE_FILE_SAVE_ERROR);
    }
}