Java Code Examples for cn.hutool.core.convert.Convert#toStr()

The following examples show how to use cn.hutool.core.convert.Convert#toStr() . 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: MockLogServiceImpl.java    From v-mock with MIT License 6 votes vote down vote up
/**
 * 获取过滤数据的wrapper
 *
 * @param mockLog  参数实体
 * @param isExport 是否excel导出的过滤,excel导出查询全部字段。
 */
private LambdaQueryWrapper<MockLog> getFilterWrapper(MockLog mockLog, boolean isExport) {
    Long beginTime = Convert.toLong(mockLog.getParams().get("beginTime"));
    Long endTime = Convert.toLong(mockLog.getParams().get("endTime"));
    String from = Convert.toStr(mockLog.getParams().get("from"));
    LambdaQueryWrapper<MockLog> mockLogWrapper = Wrappers.<MockLog>lambdaQuery()
            // like ip
            .like(StrUtil.isNotBlank(mockLog.getRequestIp()), MockLog::getRequestIp, mockLog.getRequestIp())
            // like hit url
            .like(StrUtil.isNotBlank(mockLog.getHitUrl()) && !FROM_URL.equals(from), MockLog::getHitUrl, mockLog.getHitUrl())
            // equals hit url, in url log page
            .eq(StrUtil.isNotBlank(mockLog.getHitUrl()) && FROM_URL.equals(from), MockLog::getHitUrl, mockLog.getHitUrl())
            // equals method
            .eq(StrUtil.isNotBlank(mockLog.getRequestMethod()), MockLog::getRequestMethod, mockLog.getRequestMethod())
            // beginTime
            .ge(beginTime != null, MockLog::getCreateTime, beginTime)
            // endTime 加入当日的时间戳
            .le(endTime != null, MockLog::getCreateTime, endTime);
    // 非导出场景
    if (!isExport) {
        // 只查询需要字段,排除大json字段
        mockLogWrapper.select(MockLog::getLogId, MockLog::getHitUrl, MockLog::getRequestMethod, MockLog::getCreateTime, MockLog::getRequestIp);
    }
    return mockLogWrapper;
}
 
Example 2
Source File: HutoolController.java    From mall-learning with Apache License 2.0 6 votes vote down vote up
@ApiOperation("Convert使用:类型转换工具类")
@GetMapping(value = "/covert")
public CommonResult covert() {
    //转换为字符串
    int a = 1;
    String aStr = Convert.toStr(a);
    //转换为指定类型数组
    String[] b = {"1", "2", "3", "4"};
    Integer[] bArr = Convert.toIntArray(b);
    //转换为日期对象
    String dateStr = "2017-05-06";
    Date date = Convert.toDate(dateStr);
    //转换为列表
    String[] strArr = {"a", "b", "c", "d"};
    List<String> strList = Convert.toList(String.class, strArr);
    return CommonResult.success(null, "操作成功");
}
 
Example 3
Source File: TestUtil.java    From albedo with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static void appendUrl(StringBuilder sb, Object value, String key, Charset charset) {
	String valueStr = Convert.toStr(value);
	sb.append(URLUtil.encodeAll(key, charset)).append("=");
	if (StrUtil.isNotEmpty(valueStr)) {
		sb.append(URLUtil.encodeAll(valueStr, charset));
	}
}
 
Example 4
Source File: ServletUtils.java    From RuoYi with Apache License 2.0 4 votes vote down vote up
/**
 * 获取String参数
 */
public static String getParameter(String name, String defaultValue) {
    return Convert.toStr(getRequest().getParameter(name), defaultValue);
}
 
Example 5
Source File: ExcelUtil.java    From RuoYi with Apache License 2.0 4 votes vote down vote up
/**
 * 对excel表单指定表格索引名转换成list
 *
 * @param sheetName 表格索引名
 * @param input     输入流
 * @return 转换后集合
 */
private List<T> importExcel(String sheetName, InputStream input) {
    this.type = Excel.Type.IMPORT;
    List<T> list = new ArrayList<>();

    try (Workbook workbook = WorkbookFactory.create(input)) {
        this.workbook = workbook;
        Sheet sheet;
        if (StrUtil.isNotEmpty(sheetName)) {
            // 如果指定sheet名,则取指定sheet中的内容.
            sheet = workbook.getSheet(sheetName);
        } else {
            // 如果传入的sheet名不存在则默认指向第1个sheet.
            sheet = workbook.getSheetAt(0);
        }

        if (sheet == null) {
            throw new IOException("文件sheet不存在");
        }

        int rows = sheet.getPhysicalNumberOfRows();

        if (rows > 0) {
            // 定义一个map用于存放excel列的序号和field.
            Map<String, Integer> cellMap = new HashMap<>();
            //获取表头
            Row heard = sheet.getRow(0);
            for (int i = 0; i < heard.getPhysicalNumberOfCells(); i++) {
                Cell cell = heard.getCell(i);
                if (ObjectUtil.isNotNull(cell)) {
                    String value = Convert.toStr(this.getCellValue(heard, i));
                    cellMap.put(value, i);
                } else {
                    cellMap.put(null, i);
                }
            }

            // 有数据时才处理 得到类的所有field.
            Field[] allFields = clazz.getDeclaredFields();
            // 定义一个map用于存放列的序号和field.
            Map<Integer, Field> fieldsMap = new HashMap<>();
            for (Field field : allFields) {
                // 将有注解的field存放到map中.
                Excel attr = field.getAnnotation(Excel.class);
                boolean annotation = attr != null && (attr.type() == Excel.Type.ALL || attr.type() == type);
                if(annotation){
                    // 设置类的私有字段属性可访问.
                    field.setAccessible(true);
                    Integer column = cellMap.get(attr.name());
                    fieldsMap.put(column, field);
                }
            }
            this.getImportData(rows,fieldsMap);
        }
    } catch (InvalidFormatException | IOException | IllegalAccessException | InstantiationException e) {
        log.error(e.getMessage(), e);
    }
    return list;
}
 
Example 6
Source File: Global.java    From RuoYi with Apache License 2.0 4 votes vote down vote up
/**
 * 获取项目名称
 */
public static String getName() {
    return Convert.toStr(getConfig("ruoyi.name"), "RuoYi");
}
 
Example 7
Source File: Global.java    From RuoYi with Apache License 2.0 4 votes vote down vote up
/**
 * 获取项目版本
 */
public static String getVersion() {
    return Convert.toStr(getConfig("ruoyi.version"), "3.4.0");
}
 
Example 8
Source File: Global.java    From RuoYi with Apache License 2.0 4 votes vote down vote up
/**
 * 获取作者
 */
public static String getAuthor() {
    return Convert.toStr(getConfig("gen.author"), "ruoyi");
}
 
Example 9
Source File: Global.java    From RuoYi with Apache License 2.0 4 votes vote down vote up
/**
 * 生成包路径
 */
public static String getPackageName() {
    return Convert.toStr(getConfig("gen.packageName"), "com.ruoyi.project.module");
}
 
Example 10
Source File: Global.java    From RuoYi with Apache License 2.0 4 votes vote down vote up
/**
 * 是否自动去除表前缀
 */
public static String getAutoRemovePre() {
    return Convert.toStr(getConfig("gen.autoRemovePre"), "true");
}
 
Example 11
Source File: Global.java    From RuoYi with Apache License 2.0 4 votes vote down vote up
/**
 * 表前缀(类名不会包含表前缀)
 */
public static String getTablePrefix() {
    return Convert.toStr(getConfig("gen.tablePrefix"), "sys_");
}
 
Example 12
Source File: Kv.java    From magic-starter with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * 获得特定类型值
 *
 * @param attr 字段名
 * @return 字段值
 */
public String getStr(String attr) {
	return Convert.toStr(get(attr), null);
}