Java Code Examples for cn.hutool.core.util.StrUtil#join()

The following examples show how to use cn.hutool.core.util.StrUtil#join() . 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: BaseDao.java    From spring-boot-demo with MIT License 6 votes vote down vote up
/**
 * 根据对象查询
 *
 * @param t 查询条件
 * @return 对象列表
 */
public List<T> findByExample(T t) {
	String tableName = getTableName(t);
	List<Field> filterField = getField(t, true);
	List<String> columnList = getColumns(filterField);

	List<String> columns = columnList.stream().map(s -> " and " + s + " = ? ").collect(Collectors.toList());

	String where = StrUtil.join(" ", columns);
	// 构造值
	Object[] values = filterField.stream().map(field -> ReflectUtil.getFieldValue(t, field)).toArray();

	String sql = StrUtil.format("SELECT * FROM {table} where 1=1 {where}", Dict.create().set("table", tableName).set("where", StrUtil.isBlank(where) ? "" : where));
	log.debug("【执行SQL】SQL:{}", sql);
	log.debug("【执行SQL】参数:{}", JSONUtil.toJsonStr(values));
	List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql, values);
	List<T> ret = CollUtil.newArrayList();
	maps.forEach(map -> ret.add(BeanUtil.fillBeanWithMap(map, ReflectUtil.newInstance(clazz), true, false)));
	return ret;
}
 
Example 2
Source File: BaseDao.java    From spring-boot-demo with MIT License 6 votes vote down vote up
/**
 * 通用根据主键更新,自增列需要添加 {@link Pk} 注解
 *
 * @param t          对象
 * @param pk         主键
 * @param ignoreNull 是否忽略 null 值
 * @return 操作的行数
 */
protected Integer updateById(T t, P pk, Boolean ignoreNull) {
	String tableName = getTableName(t);

	List<Field> filterField = getField(t, ignoreNull);

	List<String> columnList = getColumns(filterField);

	List<String> columns = columnList.stream().map(s -> StrUtil.appendIfMissing(s, " = ?")).collect(Collectors.toList());
	String params = StrUtil.join(Const.SEPARATOR_COMMA, columns);

	// 构造值
	List<Object> valueList = filterField.stream().map(field -> ReflectUtil.getFieldValue(t, field)).collect(Collectors.toList());
	valueList.add(pk);

	Object[] values = ArrayUtil.toArray(valueList, Object.class);

	String sql = StrUtil.format("UPDATE {table} SET {params} where id = ?", Dict.create().set("table", tableName).set("params", params));
	log.debug("【执行SQL】SQL:{}", sql);
	log.debug("【执行SQL】参数:{}", JSONUtil.toJsonStr(values));
	return jdbcTemplate.update(sql, values);
}
 
Example 3
Source File: BaseDao.java    From spring-boot-demo with MIT License 6 votes vote down vote up
/**
 * 通用插入,自增列需要添加 {@link Pk} 注解
 *
 * @param t          对象
 * @param ignoreNull 是否忽略 null 值
 * @return 操作的行数
 */
protected Integer insert(T t, Boolean ignoreNull) {
	String table = getTableName(t);

	List<Field> filterField = getField(t, ignoreNull);

	List<String> columnList = getColumns(filterField);

	String columns = StrUtil.join(Const.SEPARATOR_COMMA, columnList);

	// 构造占位符
	String params = StrUtil.repeatAndJoin("?", columnList.size(), Const.SEPARATOR_COMMA);

	// 构造值
	Object[] values = filterField.stream().map(field -> ReflectUtil.getFieldValue(t, field)).toArray();

	String sql = StrUtil.format("INSERT INTO {table} ({columns}) VALUES ({params})", Dict.create().set("table", table).set("columns", columns).set("params", params));
	log.debug("【执行SQL】SQL:{}", sql);
	log.debug("【执行SQL】参数:{}", JSONUtil.toJsonStr(values));
	return jdbcTemplate.update(sql, values);
}
 
Example 4
Source File: BaseDao.java    From spring-boot-demo with MIT License 6 votes vote down vote up
/**
 * 通用插入,自增列需要添加 {@link Pk} 注解
 *
 * @param t          对象
 * @param ignoreNull 是否忽略 null 值
 * @return 操作的行数
 */
protected Integer insert(T t, Boolean ignoreNull) {
	String table = getTableName(t);

	List<Field> filterField = getField(t, ignoreNull);

	List<String> columnList = getColumns(filterField);

	String columns = StrUtil.join(Const.SEPARATOR_COMMA, columnList);

	// 构造占位符
	String params = StrUtil.repeatAndJoin("?", columnList.size(), Const.SEPARATOR_COMMA);

	// 构造值
	Object[] values = filterField.stream().map(field -> ReflectUtil.getFieldValue(t, field)).toArray();

	String sql = StrUtil.format("INSERT INTO {table} ({columns}) VALUES ({params})", Dict.create().set("table", table).set("columns", columns).set("params", params));
	log.debug("【执行SQL】SQL:{}", sql);
	log.debug("【执行SQL】参数:{}", JSONUtil.toJsonStr(values));
	return jdbcTemplate.update(sql, values);
}
 
Example 5
Source File: BaseDao.java    From spring-boot-demo with MIT License 6 votes vote down vote up
/**
 * 通用根据主键更新,自增列需要添加 {@link Pk} 注解
 *
 * @param t          对象
 * @param pk         主键
 * @param ignoreNull 是否忽略 null 值
 * @return 操作的行数
 */
protected Integer updateById(T t, P pk, Boolean ignoreNull) {
	String tableName = getTableName(t);

	List<Field> filterField = getField(t, ignoreNull);

	List<String> columnList = getColumns(filterField);

	List<String> columns = columnList.stream().map(s -> StrUtil.appendIfMissing(s, " = ?")).collect(Collectors.toList());
	String params = StrUtil.join(Const.SEPARATOR_COMMA, columns);

	// 构造值
	List<Object> valueList = filterField.stream().map(field -> ReflectUtil.getFieldValue(t, field)).collect(Collectors.toList());
	valueList.add(pk);

	Object[] values = ArrayUtil.toArray(valueList, Object.class);

	String sql = StrUtil.format("UPDATE {table} SET {params} where id = ?", Dict.create().set("table", tableName).set("params", params));
	log.debug("【执行SQL】SQL:{}", sql);
	log.debug("【执行SQL】参数:{}", JSONUtil.toJsonStr(values));
	return jdbcTemplate.update(sql, values);
}
 
Example 6
Source File: BaseDao.java    From spring-boot-demo with MIT License 6 votes vote down vote up
/**
 * 根据对象查询
 *
 * @param t 查询条件
 * @return 对象列表
 */
public List<T> findByExample(T t) {
	String tableName = getTableName(t);
	List<Field> filterField = getField(t, true);
	List<String> columnList = getColumns(filterField);

	List<String> columns = columnList.stream().map(s -> " and " + s + " = ? ").collect(Collectors.toList());

	String where = StrUtil.join(" ", columns);
	// 构造值
	Object[] values = filterField.stream().map(field -> ReflectUtil.getFieldValue(t, field)).toArray();

	String sql = StrUtil.format("SELECT * FROM {table} where 1=1 {where}", Dict.create().set("table", tableName).set("where", StrUtil.isBlank(where) ? "" : where));
	log.debug("【执行SQL】SQL:{}", sql);
	log.debug("【执行SQL】参数:{}", JSONUtil.toJsonStr(values));
	List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql, values);
	List<T> ret = CollUtil.newArrayList();
	maps.forEach(map -> ret.add(BeanUtil.fillBeanWithMap(map, ReflectUtil.newInstance(clazz), true, false)));
	return ret;
}
 
Example 7
Source File: BaseDao.java    From spring-boot-demo with MIT License 6 votes vote down vote up
/**
 * 根据对象查询
 *
 * @param t 查询条件
 * @return 对象列表
 */
public List<T> findByExample(T t) {
	String tableName = getTableName(t);
	List<Field> filterField = getField(t, true);
	List<String> columnList = getColumns(filterField);

	List<String> columns = columnList.stream().map(s -> " and " + s + " = ? ").collect(Collectors.toList());

	String where = StrUtil.join(" ", columns);
	// 构造值
	Object[] values = filterField.stream().map(field -> ReflectUtil.getFieldValue(t, field)).toArray();

	String sql = StrUtil.format("SELECT * FROM {table} where 1=1 {where}", Dict.create().set("table", tableName).set("where", StrUtil.isBlank(where) ? "" : where));
	log.debug("【执行SQL】SQL:{}", sql);
	log.debug("【执行SQL】参数:{}", JSONUtil.toJsonStr(values));
	List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql, values);
	List<T> ret = CollUtil.newArrayList();
	maps.forEach(map -> ret.add(BeanUtil.fillBeanWithMap(map, ReflectUtil.newInstance(clazz), true, false)));
	return ret;
}
 
Example 8
Source File: BaseDao.java    From spring-boot-demo with MIT License 6 votes vote down vote up
/**
 * 通用根据主键更新,自增列需要添加 {@link Pk} 注解
 *
 * @param t          对象
 * @param pk         主键
 * @param ignoreNull 是否忽略 null 值
 * @return 操作的行数
 */
protected Integer updateById(T t, P pk, Boolean ignoreNull) {
	String tableName = getTableName(t);

	List<Field> filterField = getField(t, ignoreNull);

	List<String> columnList = getColumns(filterField);

	List<String> columns = columnList.stream().map(s -> StrUtil.appendIfMissing(s, " = ?")).collect(Collectors.toList());
	String params = StrUtil.join(Const.SEPARATOR_COMMA, columns);

	// 构造值
	List<Object> valueList = filterField.stream().map(field -> ReflectUtil.getFieldValue(t, field)).collect(Collectors.toList());
	valueList.add(pk);

	Object[] values = ArrayUtil.toArray(valueList, Object.class);

	String sql = StrUtil.format("UPDATE {table} SET {params} where id = ?", Dict.create().set("table", tableName).set("params", params));
	log.debug("【执行SQL】SQL:{}", sql);
	log.debug("【执行SQL】参数:{}", JSONUtil.toJsonStr(values));
	return jdbcTemplate.update(sql, values);
}
 
Example 9
Source File: BaseDao.java    From spring-boot-demo with MIT License 6 votes vote down vote up
/**
 * 通用插入,自增列需要添加 {@link Pk} 注解
 *
 * @param t          对象
 * @param ignoreNull 是否忽略 null 值
 * @return 操作的行数
 */
protected Integer insert(T t, Boolean ignoreNull) {
	String table = getTableName(t);

	List<Field> filterField = getField(t, ignoreNull);

	List<String> columnList = getColumns(filterField);

	String columns = StrUtil.join(Const.SEPARATOR_COMMA, columnList);

	// 构造占位符
	String params = StrUtil.repeatAndJoin("?", columnList.size(), Const.SEPARATOR_COMMA);

	// 构造值
	Object[] values = filterField.stream().map(field -> ReflectUtil.getFieldValue(t, field)).toArray();

	String sql = StrUtil.format("INSERT INTO {table} ({columns}) VALUES ({params})", Dict.create().set("table", table).set("columns", columns).set("params", params));
	log.debug("【执行SQL】SQL:{}", sql);
	log.debug("【执行SQL】参数:{}", JSONUtil.toJsonStr(values));
	return jdbcTemplate.update(sql, values);
}
 
Example 10
Source File: CacheKey.java    From zuihou-admin-cloud with Apache License 2.0 5 votes vote down vote up
/**
 * 构建没有租户信息的key
 *
 * @param args
 * @return
 */
static String buildKey(Object... args) {
    if (args.length == 1) {
        return String.valueOf(args[0]);
    } else if (args.length > 0) {
        return StrUtil.join(StrPool.COLON, args);
    } else {
        return "";
    }
}
 
Example 11
Source File: AliOssAutoConfigure.java    From zuihou-admin-cloud with Apache License 2.0 5 votes vote down vote up
@Override
protected void uploadFile(File file, MultipartFile multipartFile) throws Exception {
    FileServerProperties.Ali ali = fileProperties.getAli();
    OSS ossClient = new OSSClientBuilder().build(ali.getEndpoint(), ali.getAccessKeyId(),
            ali.getAccessKeySecret());
    String bucketName = ali.getBucketName();
    if (!ossClient.doesBucketExist(bucketName)) {
        ossClient.createBucket(bucketName);
    }

    //生成文件名
    String fileName = StrUtil.join(StrPool.EMPTY, UUID.randomUUID().toString(), StrPool.DOT, file.getExt());
    //日期文件夹
    String tenant = BaseContextHandler.getTenant();
    String relativePath = tenant + StrPool.SLASH + LocalDate.now().format(DateTimeFormatter.ofPattern(DEFAULT_MONTH_FORMAT_SLASH));
    // web服务器存放的绝对路径
    String relativeFileName = relativePath + StrPool.SLASH + fileName;

    ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentDisposition("attachment;fileName=" + file.getSubmittedFileName());
    metadata.setContentType(file.getContextType());
    PutObjectRequest request = new PutObjectRequest(bucketName, relativeFileName, multipartFile.getInputStream(), metadata);
    PutObjectResult result = ossClient.putObject(request);

    log.info("result={}", JSONObject.toJSONString(result));

    String url = ali.getUriPrefix() + relativeFileName;
    file.setUrl(StrUtil.replace(url, "\\\\", StrPool.SLASH));
    file.setFilename(fileName);
    file.setRelativePath(relativePath);

    file.setGroup(result.getETag());
    file.setPath(result.getRequestId());

    ossClient.shutdown();
}
 
Example 12
Source File: CacheKey.java    From zuihou-admin-boot with Apache License 2.0 5 votes vote down vote up
/**
 * 构建key
 *
 * @param args
 * @return
 */
static String buildTenantKey(Object... args) {
    if (args.length > 0) {
        return StrUtil.join(StrPool.COLON, BaseContextHandler.getTenant(), args);
    } else {
        return BaseContextHandler.getTenant();
    }
}
 
Example 13
Source File: CacheKey.java    From zuihou-admin-cloud with Apache License 2.0 5 votes vote down vote up
/**
 * 构建key
 *
 * @param args
 * @return
 */
static String buildTenantKey(Object... args) {
    if (args.length > 0) {
        return StrUtil.join(StrPool.COLON, BaseContextHandler.getTenant(), args);
    } else {
        return BaseContextHandler.getTenant();
    }
}
 
Example 14
Source File: AliOssAutoConfigure.java    From zuihou-admin-boot with Apache License 2.0 5 votes vote down vote up
@Override
protected void uploadFile(File file, MultipartFile multipartFile) throws Exception {
    FileServerProperties.Ali ali = fileProperties.getAli();
    OSS ossClient = new OSSClientBuilder().build(ali.getEndpoint(), ali.getAccessKeyId(),
            ali.getAccessKeySecret());
    String bucketName = ali.getBucketName();
    if (!ossClient.doesBucketExist(bucketName)) {
        ossClient.createBucket(bucketName);
    }

    //生成文件名
    String fileName = StrUtil.join(StrPool.EMPTY, UUID.randomUUID().toString(), StrPool.DOT, file.getExt());
    //日期文件夹
    String tenant = BaseContextHandler.getTenant();
    String relativePath = tenant + StrPool.SLASH + LocalDate.now().format(DateTimeFormatter.ofPattern(DEFAULT_MONTH_FORMAT_SLASH));
    // web服务器存放的绝对路径
    String relativeFileName = relativePath + StrPool.SLASH + fileName;

    ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentDisposition("attachment;fileName=" + file.getSubmittedFileName());
    metadata.setContentType(file.getContextType());
    PutObjectRequest request = new PutObjectRequest(bucketName, relativeFileName, multipartFile.getInputStream(), metadata);
    PutObjectResult result = ossClient.putObject(request);

    log.info("result={}", JSONObject.toJSONString(result));

    String url = ali.getUriPrefix() + relativeFileName;
    file.setUrl(StrUtil.replace(url, "\\\\", StrPool.SLASH));
    file.setFilename(fileName);
    file.setRelativePath(relativePath);

    file.setGroup(result.getETag());
    file.setPath(result.getRequestId());

    ossClient.shutdown();
}
 
Example 15
Source File: CacheKey.java    From zuihou-admin-boot with Apache License 2.0 5 votes vote down vote up
/**
 * 构建没有租户信息的key
 *
 * @param args
 * @return
 */
static String buildKey(Object... args) {
    if (args.length == 1) {
        return String.valueOf(args[0]);
    } else if (args.length > 0) {
        return StrUtil.join(StrPool.COLON, args);
    } else {
        return "";
    }
}
 
Example 16
Source File: SchemaInitSystemStrategy.java    From zuihou-admin-cloud with Apache License 2.0 4 votes vote down vote up
public String useDb(String tenant, ScriptRunner runner, String database) {
    String db = StrUtil.join(StrUtil.UNDERLINE, database, tenant);
    Reader reader = new StringReader("use " + db + ";");
    runner.runScript(reader);
    return db;
}
 
Example 17
Source File: YxStoreProductServiceImpl.java    From yshopmall with Apache License 2.0 4 votes vote down vote up
/**
 * 组合规则属性算法
 * @param jsonStr
 * @return
 */
public DetailDto attrFormat(String jsonStr){
    JSONObject jsonObject = JSON.parseObject(jsonStr);
    List<FromatDetailDto> fromatDetailDTOList = JSON.parseArray(jsonObject.get("items").toString(),
            FromatDetailDto.class);
    List<String> data = new ArrayList<>();
    List<Map<String,Map<String,String>>> res =new ArrayList<>();
    if(fromatDetailDTOList.size() > 1){
        for (int i=0; i < fromatDetailDTOList.size() - 1;i++){
            if(i == 0) data = fromatDetailDTOList.get(i).getDetail();
            List<String> tmp = new LinkedList<>();
            for (String v : data) {
                for (String g : fromatDetailDTOList.get(i+1).getDetail()) {
                    String rep2 = "";
                    if(i == 0){
                        rep2 = fromatDetailDTOList.get(i).getValue() + "_" + v + "-"
                                + fromatDetailDTOList.get(i+1).getValue() + "_" + g;
                    }else{
                        rep2 = v + "-"
                                + fromatDetailDTOList.get(i+1).getValue() + "_" + g;
                    }
                    tmp.add(rep2);
                    if(i == fromatDetailDTOList.size() - 2){
                        Map<String,Map<String,String>> rep4 = new LinkedHashMap<>();
                        Map<String,String> reptemp = new LinkedHashMap<>();
                        for (String h : Arrays.asList(rep2.split("-"))) {
                            List<String> rep3 = Arrays.asList(h.split("_"));

                            if(rep3.size() > 1){
                                reptemp.put(rep3.get(0),rep3.get(1));
                            }else{
                                reptemp.put(rep3.get(0),"");
                            }
                        }
                        rep4.put("detail",reptemp);
                        res.add(rep4);
                    }
                }
            }
            //System.out.println("tmp:"+tmp);
            if(!tmp.isEmpty()){
                data = tmp;
            }
        }
    }else{
        List<String> dataArr = new ArrayList<>();

        for (FromatDetailDto fromatDetailDTO : fromatDetailDTOList) {

            for (String str : fromatDetailDTO.getDetail()) {
                Map<String,Map<String,String>> map2 = new LinkedHashMap<>();
                //List<Map<String,String>> list1 = new ArrayList<>();
                dataArr.add(fromatDetailDTO.getValue()+"_"+str);
                Map<String,String> map1 = new LinkedHashMap<>();
                map1.put(fromatDetailDTO.getValue(),str);
                //list1.add(map1);
                map2.put("detail",map1);
                res.add(map2);
            }
        }
        String s = StrUtil.join("-",dataArr);
        data.add(s);
    }
    DetailDto detailDTO = new DetailDto();
    detailDTO.setData(data);
    detailDTO.setRes(res);
    return detailDTO;
}
 
Example 18
Source File: SchemaInitSystemStrategy.java    From zuihou-admin-boot with Apache License 2.0 4 votes vote down vote up
public String useDb(String tenant, ScriptRunner runner, String database) {
    String db = StrUtil.join(StrUtil.UNDERLINE, database, tenant);
    Reader reader = new StringReader("use " + db + ";");
    runner.runScript(reader);
    return db;
}