首页 > 数据库 >runsql

runsql

时间:2023-12-04 16:48:29浏览次数:29  
标签:Map runsql String List value entry sqlStr

CustomSqlMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.runshi.runshi.cms.mapper.CustomSqlMapper">
	<select id="runPageSql" resultType="java.util.Map"  parameterType="java.lang.String">
		${sqlStr}
	</select>
	<select id="runListSql" resultType="java.util.Map"  parameterType="java.lang.String">
		${sqlStr}
	</select>
	<select id="runSingleSql" resultType="java.util.Map"  parameterType="java.lang.String">
		${sqlStr}
	</select>
</mapper>

CustomSqlMapper

@Mapper
public interface CustomSqlMapper extends RunshiBaseMapper<CmsArticleCategory> {

	IPage<Map<String, Object>> runPageSql(Page page, @Param(value="sqlStr") String sqlStr);

	List<Map<String, Object>> runListSql(@Param(value="sqlStr") String sqlStr);

	Map<String, Object> runSingleSql(@Param(value="sqlStr") String sqlStr);
}

CustomSqlService

public interface CustomSqlService extends IService<CmsArticleCategory> {

	IPage<Map<String, Object>> runPageSql(Page page, String sqlStr);

	List<Map<String, Object>> runListSql(String sqlStr);

	Map<String, Object> runSingleSql(String sqlStr);
}

CustomSqlServiceImpl

@Service
public class CustomSqlServiceImpl extends ServiceImpl<CustomSqlMapper, CmsArticleCategory> implements CustomSqlService {

	@Override
	public IPage runPageSql(Page page, String sqlStr) {
		IPage dataList = baseMapper.runPageSql(page, sqlStr);

		//dataList.getRecords()
		List<Map<String,Object>> lst = new ArrayList<>();
		lst =dataList.getRecords();

		dataList.setRecords(lst.stream().map(MapUtil::toCamelCaseMap).collect(Collectors.toList()));
		return dataList;
	}

	@Override
	public List<Map<String, Object>> runListSql(String sqlStr) {
		return GetListMap(baseMapper.runListSql(sqlStr));
	}

	@Override
	public Map<String, Object> runSingleSql(String sqlStr) {
		if (sqlStr.indexOf(" limit ") == -1) {
			sqlStr += " limit 1 ";
		}
		return getUnderToHump(baseMapper.runSingleSql(sqlStr));
	}

	private List<Map<String, Object>> GetListMap(List<Map<String, Object>> lst){
		List<Map<String, Object>> lstMap = new ArrayList<>();

		for(int i=0;i<lst.size();i++){
			lstMap.add(getUnderToHump(lst.get(i)));
		}
		return  lstMap;
	}

	private Map<String, Object> getUnderToHump(Map<String, Object> map) {
		if (Objects.isNull(map)) {
			return map;
		}
		List<Map.Entry<String, Object>> list = map.entrySet().stream().collect(Collectors.toList());
		Map<String, Object> paramMap = new HashMap<>();
		for (Map.Entry<String, Object> entry : list) {
			if (entry.getKey().indexOf("_") == -1) {
				Object value = getValue(entry);
				paramMap.put(entry.getKey(), value);
				continue;
			}
			String[] split = entry.getKey().split("_");
			StringBuilder result = new StringBuilder();
			result.append(split[0]);
			for (int i=1;i<split.length;i++) {
				result.append(split[i].substring(0,1).toUpperCase()).append(split[i].substring(1));
			}
			paramMap.put(result.toString(), getValue(entry));
		}
		return paramMap;
	}

	private Object getValue(Map.Entry<String, Object> entry) {
		Object value = entry.getValue();
		if (entry.getValue() instanceof List) {
			List listValue =(List) entry.getValue();
			List result = new ArrayList();
			for (Object o: listValue) {
				Object convert = null;
				if (o instanceof Map) {
					Map mapList =(Map) o;
					convert = getUnderToHump(mapList);
				}
				if (o instanceof Long) {
					convert = String.valueOf(o);
				}
				result.add(convert);
			}
			value = result;
		}
		if (entry.getValue() instanceof Map) {
			Map linkedHashMap =(Map) entry.getValue();
			value = getUnderToHump(linkedHashMap);
		}
		if (entry.getValue() instanceof JsonArray) {
			JsonArray value1 = (JsonArray) entry.getValue();
			JsonArray transValue  = new JsonArray();
			for (JsonElement o:value1) {
				if (o instanceof JsonObject) {
					JsonObject jsonObject =  o.getAsJsonObject();
					Map underToHump = getUnderToHump(new Gson().fromJson(jsonObject.toString(), Map.class));
					transValue.add(JsonParser.parseString(new Gson().toJson(underToHump)));
				} else {
					transValue.add(o.toString());
				}
			}
			value = transValue;
		}
		if (entry.getValue() instanceof JsonObject) {
			JsonObject jsonObject  = (JsonObject) entry.getValue();
			value = getUnderToHump( new Gson().fromJson(jsonObject.toString(),Map.class));
		}
		return value;
	}

}

标签:Map,runsql,String,List,value,entry,sqlStr
From: https://www.cnblogs.com/linhan8888/p/17875294.html

相关文章